tony / oxwm

generate default config if config file is not found/set Browse files
fixes: #147, #148
emzywastaken committed 2026-02-16 parent 98f70b0 commit 3f07d04a27890d438f32acfb19cc9759e9e9cab1
Showing 2 changed files with 23 additions and 37 deletions. UnifiedSplit
+4 −0 build.zig
@@ -13,6 +13,10 @@ pub fn build(b: *std.Build) void {
13 }), 13 }),
14 }); 14 });
15 15
16 + exe.root_module.addAnonymousImport("templates/config.lua", .{
17 + .root_source_file = b.path("templates/config.lua"),
18 + });
19 +
16 exe.linkSystemLibrary("lua5.4"); 20 exe.linkSystemLibrary("lua5.4");
17 exe.linkSystemLibrary("X11"); 21 exe.linkSystemLibrary("X11");
18 exe.linkSystemLibrary("Xinerama"); 22 exe.linkSystemLibrary("Xinerama");
+19 −37 src/main.zig
@@ -102,35 +102,18 @@ fn print_help() void {
102 , .{}); 102 , .{});
103 } 103 }
104 104
105 -fn init_config(allocator: std.mem.Allocator) void { 105 +fn get_config_path(allocator: std.mem.Allocator) ![]u8 {
106 - const home = std.posix.getenv("HOME") orelse { 106 + const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
107 - std.debug.print("error: HOME environment variable not set\n", .{}); 107 + const config_path = try std.fs.path.join(allocator, &.{ home, ".config", "oxwm", "config.lua" });
108 - return;
109 - };
110 108
111 - var path_buf: [512]u8 = undefined; 109 + return config_path;
112 - const config_dir = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm", .{home}) catch { 110 +}
113 - std.debug.print("error: path too long\n", .{});
114 - return;
115 - };
116 -
117 - std.fs.cwd().makePath(config_dir) catch |err| {
118 - std.debug.print("error: could not create config directory: {}\n", .{err});
119 - return;
120 - };
121 111
122 - var config_path_buf: [512]u8 = undefined; 112 +fn init_config(allocator: std.mem.Allocator) void {
123 - const config_path = std.fmt.bufPrint(&config_path_buf, "{s}/config.lua", .{config_dir}) catch { 113 + const config_path = get_config_path(allocator) catch return;
124 - std.debug.print("error: path too long\n", .{}); 114 + defer allocator.free(config_path);
125 - return;
126 - };
127 115
128 - const template = std.fs.cwd().readFileAlloc(allocator, "templates/config.lua", 64 * 1024) catch |err| { 116 + const template = @embedFile("templates/config.lua");
129 - std.debug.print("error: could not read template (templates/config.lua): {}\n", .{err});
130 - std.debug.print("hint: run from the oxwm source directory, or copy templates/config.lua manually\n", .{});
131 - return;
132 - };
133 - defer allocator.free(template);
134 117
135 const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| { 118 const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
136 std.debug.print("error: could not create config file: {}\n", .{err}); 119 std.debug.print("error: could not create config file: {}\n", .{err});
@@ -152,12 +135,13 @@ pub fn main() !void {
152 const allocator = gpa.allocator(); 135 const allocator = gpa.allocator();
153 defer _ = gpa.deinit(); 136 defer _ = gpa.deinit();
154 137
155 - var config_path: ?[]const u8 = null; 138 + var config_path: []const u8 = try get_config_path(allocator);
139 + defer allocator.free(config_path);
156 var args = std.process.args(); 140 var args = std.process.args();
157 _ = args.skip(); 141 _ = args.skip();
158 while (args.next()) |arg| { 142 while (args.next()) |arg| {
159 if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) { 143 if (std.mem.eql(u8, arg, "-c") or std.mem.eql(u8, arg, "--config")) {
160 - config_path = args.next(); 144 + if (args.next()) |path| config_path = path;
161 } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) { 145 } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
162 print_help(); 146 print_help();
163 return; 147 return;
@@ -177,18 +161,16 @@ pub fn main() !void {
177 config_mod.set_config(&config); 161 config_mod.set_config(&config);
178 162
179 if (lua.init(&config)) { 163 if (lua.init(&config)) {
180 - const loaded = if (config_path) |path| 164 + const loaded = if (std.fs.cwd().statFile(config_path)) |_|
181 - lua.load_file(path) 165 + lua.load_file(config_path)
182 - else 166 + else |_| blk: {
183 - lua.load_config(); 167 + init_config(allocator);
168 + break :blk lua.load_config();
169 + };
184 170
185 if (loaded) { 171 if (loaded) {
186 config_path_global = config_path; 172 config_path_global = config_path;
187 - if (config_path) |path| { 173 + std.debug.print("loaded config from {s}\n", .{config_path});
188 - std.debug.print("loaded config from {s}\n", .{path});
189 - } else {
190 - std.debug.print("loaded config from ~/.config/oxwm/config.lua\n", .{});
191 - }
192 apply_config_values(); 174 apply_config_values();
193 } else { 175 } else {
194 std.debug.print("no config found, using defaults\n", .{}); 176 std.debug.print("no config found, using defaults\n", .{});