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 20 exe.linkSystemLibrary("lua5.4");
17 21 exe.linkSystemLibrary("X11");
18 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 {
106 - const home = std.posix.getenv("HOME") orelse {
107 - std.debug.print("error: HOME environment variable not set\n", .{});
108 - return;
109 - };
105 +fn get_config_path(allocator: std.mem.Allocator) ![]u8 {
106 + const home = std.posix.getenv("HOME") orelse return error.CouldNotGetHomeDir;
107 + const config_path = try std.fs.path.join(allocator, &.{ home, ".config", "oxwm", "config.lua" });
110 108
111 - var path_buf: [512]u8 = undefined;
112 - const config_dir = std.fmt.bufPrint(&path_buf, "{s}/.config/oxwm", .{home}) catch {
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 - };
109 + return config_path;
110 +}
121 111
122 - var config_path_buf: [512]u8 = undefined;
123 - const config_path = std.fmt.bufPrint(&config_path_buf, "{s}/config.lua", .{config_dir}) catch {
124 - std.debug.print("error: path too long\n", .{});
125 - return;
126 - };
112 +fn init_config(allocator: std.mem.Allocator) void {
113 + const config_path = get_config_path(allocator) catch return;
114 + defer allocator.free(config_path);
127 115
128 - const template = std.fs.cwd().readFileAlloc(allocator, "templates/config.lua", 64 * 1024) catch |err| {
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);
116 + const template = @embedFile("templates/config.lua");
134 117
135 118 const file = std.fs.createFileAbsolute(config_path, .{}) catch |err| {
136 119 std.debug.print("error: could not create config file: {}\n", .{err});
@@ -152,12 +135,13 @@ pub fn main() !void {
152 135 const allocator = gpa.allocator();
153 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 140 var args = std.process.args();
157 141 _ = args.skip();
158 142 while (args.next()) |arg| {
159 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 145 } else if (std.mem.eql(u8, arg, "-h") or std.mem.eql(u8, arg, "--help")) {
162 146 print_help();
163 147 return;
@@ -177,18 +161,16 @@ pub fn main() !void {
177 161 config_mod.set_config(&config);
178 162
179 163 if (lua.init(&config)) {
180 - const loaded = if (config_path) |path|
181 - lua.load_file(path)
182 - else
183 - lua.load_config();
164 + const loaded = if (std.fs.cwd().statFile(config_path)) |_|
165 + lua.load_file(config_path)
166 + else |_| blk: {
167 + init_config(allocator);
168 + break :blk lua.load_config();
169 + };
184 170
185 171 if (loaded) {
186 172 config_path_global = config_path;
187 - if (config_path) |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 - }
173 + std.debug.print("loaded config from {s}\n", .{config_path});
192 174 apply_config_values();
193 175 } else {
194 176 std.debug.print("no config found, using defaults\n", .{});