From f60a2c5c3784d924b6fdc28b2dae02a9f70b1965 Mon Sep 17 00:00:00 2001 From: Squibid Date: Thu, 5 Mar 2026 09:30:01 -0500 Subject: [PATCH] turns out passing in the -u flag didn't actually work before... Additionally, in the case that the directory doesn't exist mezzaluna will default back to the config at ~/.config/mez/ --- runtime/share/mezzaluna/init.lua | 26 ++++++++++--------- src/lua/Lua.zig | 44 +++++++++----------------------- src/main.zig | 10 ++++++-- 3 files changed, 34 insertions(+), 46 deletions(-) diff --git a/runtime/share/mezzaluna/init.lua b/runtime/share/mezzaluna/init.lua index da046b8..689f431 100644 --- a/runtime/share/mezzaluna/init.lua +++ b/runtime/share/mezzaluna/init.lua @@ -1,13 +1,3 @@ -local env_conf = os.getenv("XDG_CONFIG_HOME") - -if not env_conf then - env_conf = os.getenv("HOME") - if not env_conf then - error("Couldn't determine potential config directory is $HOME set?") - end - env_conf = mez.fs.joinpath(env_conf, ".config") -end - -- allow loading files in the runtime directory package.path = package.path..";"..mez.fs.joinpath(mez.path.runtime, "?.lua") mez.inspect = require("inspect").inspect @@ -15,6 +5,18 @@ mez.inspect = require("inspect").inspect mez.path.base_config = mez.fs.joinpath(mez.path.runtime, "master.lua") if not mez.path.config then - mez.path.config = mez.fs.joinpath(env_conf, "mez", "init.lua") - package.path = package.path..";"..mez.fs.joinpath(env_conf, "mez", "lua", "?.lua") + local env_conf = os.getenv("XDG_CONFIG_HOME") + + if not env_conf then + env_conf = os.getenv("HOME") + if not env_conf then + error("Couldn't determine potential config directory is $HOME set?") + end + env_conf = mez.fs.joinpath(env_conf, ".config") + end + + mez.path.config = mez.fs.joinpath(env_conf, "mez") end + +package.path = package.path..";"..mez.fs.joinpath(mez.path.config, "lua", "?.lua") +mez.path.config = mez.fs.joinpath(mez.path.config, "init.lua") diff --git a/src/lua/Lua.zig b/src/lua/Lua.zig index 5642c9d..b68ce05 100644 --- a/src/lua/Lua.zig +++ b/src/lua/Lua.zig @@ -48,33 +48,12 @@ pub fn loadRuntimeDir(self: *zlua.Lua) !void { } pub fn setBaseConfig(self: *zlua.Lua, path: []const u8) !void { - { - _ = try self.getGlobal("mez"); - defer self.pop(1); - _ = self.getField(-1, "path"); - defer self.pop(1); - const new_path = try std.fs.path.join(gpa, &[_][]const u8{ path, "init.lua" }); - defer gpa.free(new_path); - _ = self.pushString(new_path); - self.setField(-2, "config"); - } - { - _ = try self.getGlobal("mez"); - defer self.pop(1); - _ = self.getField(-1, "path"); - defer self.pop(1); - const cur_path = self.toString(-1) catch ""; - - const unsentinel: []const u8 = std.mem.span(cur_path.ptr); - const new_path = try std.mem.concat(gpa, u8, &[_][]const u8{ - unsentinel, - ";", - path, - }); - defer gpa.free(new_path); - _ = self.pushString(new_path); - _ = self.setField(-2, "path"); - } + _ = try self.getGlobal("mez"); + defer self.pop(1); + _ = self.getField(-1, "path"); + defer self.pop(1); + _ = self.pushString(path); + self.setField(-2, "config"); } fn loadBaseConfig(self: *zlua.Lua) !void { @@ -160,7 +139,12 @@ pub fn init(self: *Lua, cfg: Config) !void { openMezLibs(self.state); - if (!cfg.enabled) try setBaseConfig(self.state, ""); + if (!cfg.enabled) { + try setBaseConfig(self.state, ""); + } else if (cfg.path) |path| { + defer gpa.free(path); + try setBaseConfig(self.state, path); + } loadRuntimeDir(self.state) catch |err| if (err == error.LuaRuntime) { std.log.warn("{s}", .{try self.state.toString(-1)}); }; @@ -169,10 +153,6 @@ pub fn init(self: *Lua, cfg: Config) !void { std.log.warn("{s}", .{try self.state.toString(-1)}); }; - if (cfg.path) |path| { - defer gpa.free(path); - try setBaseConfig(self.state, path); - } if (cfg.enabled) { loadConfigDir(self.state) catch |err| if (err == error.LuaRuntime) { std.log.warn("{s}", .{try self.state.toString(-1)}); diff --git a/src/main.zig b/src/main.zig index 97c8753..e5100d9 100644 --- a/src/main.zig +++ b/src/main.zig @@ -54,9 +54,15 @@ pub fn main() !void { var lua_config: Lua.Config = .{ .enabled = true, .path = null }; if (res.args.u != null and res.args.clean == 1) { std.debug.panic("You cannot set both -u and --clean", .{}); - } else if (res.args.u != null) { + } else if (res.args.u != null) blk: { // this is freed in lua/lua.zig - const path = try std.fs.cwd().realpathAlloc(gpa, res.args.u.?); + const path = std.fs.cwd().realpathAlloc(gpa, res.args.u.?) catch |err| switch (err) { + error.FileNotFound => { + std.log.err("Path {s} does not exist, and therefore won't be used for the configuration.", .{ res.args.u.? }); + break :blk; + }, + else => return err, + }; lua_config.path = path; } else if (res.args.clean == 1) { lua_config.enabled = false;