diff --git a/src/app.zig b/src/app.zig index ae01b69..e779a71 100644 --- a/src/app.zig +++ b/src/app.zig @@ -135,7 +135,7 @@ pub fn run(self: *App) !void { } if ((key.codepoint == 'r' and key.mods.ctrl)) { - if (config.parse(self.alloc)) { + if (config.parse(self.alloc, self)) { try self.notification.writeInfo(.ConfigReloaded); } else |err| switch (err) { error.SyntaxError => { @@ -145,7 +145,7 @@ pub fn run(self: *App) !void { try self.notification.writeErr(.InvalidKeybind); }, error.DuplicateKeybind => { - try self.notification.writeErr(.DuplicateKeybinds); + // Error logged in function }, else => { try self.notification.writeErr(.ConfigUnknownError); diff --git a/src/config.zig b/src/config.zig index 7ada7bb..03a93a7 100644 --- a/src/config.zig +++ b/src/config.zig @@ -4,6 +4,7 @@ const environment = @import("./environment.zig"); const vaxis = @import("vaxis"); const FileLogger = @import("file_logger.zig"); const Notification = @import("./notification.zig"); +const App = @import("./app.zig"); const CONFIG_NAME = "config.json"; const TRASH_DIR_NAME = "trash"; @@ -42,7 +43,7 @@ const Config = struct { return try parent.openDir(TRASH_DIR_NAME, .{ .iterate = true }); } - pub fn parse(self: *Config, alloc: std.mem.Allocator) !void { + pub fn parse(self: *Config, alloc: std.mem.Allocator, app: *App) !void { var dir = lbl: { if (try environment.getXdgConfigHomeDir()) |home_dir| { defer { @@ -122,13 +123,17 @@ const Config = struct { const res = try key_map.getOrPut(codepoint); if (res.found_existing) { + var keybind_str: [1024]u8 = undefined; + const keybind_str_bytes = try std.unicode.utf8Encode(codepoint, &keybind_str); + const message = try std.fmt.allocPrint( alloc, - "'{s}' and '{s}' have the same keybind: '{d}'", - .{ res.value_ptr.*, field.name, codepoint }, + "'{s}' and '{s}' have the same keybind: '{s}'", + .{ res.value_ptr.*, field.name, keybind_str[0..keybind_str_bytes] }, ); defer alloc.free(message); + try app.notification.write(message, .err); file_logger.write(message, .err) catch {}; return error.DuplicateKeybind; diff --git a/src/main.zig b/src/main.zig index 2dd64d8..0a96454 100644 --- a/src/main.zig +++ b/src/main.zig @@ -27,7 +27,7 @@ pub fn main() !void { var app = try App.init(alloc); defer app.deinit(); - config.parse(alloc) catch |err| switch (err) { + config.parse(alloc, &app) catch |err| switch (err) { error.SyntaxError => { try app.notification.writeErr(.ConfigSyntaxError); }, @@ -35,7 +35,7 @@ pub fn main() !void { try app.notification.writeErr(.InvalidKeybind); }, error.DuplicateKeybind => { - try app.notification.writeErr(.DuplicateKeybinds); + // Error logged in function }, else => { try app.notification.writeErr(.ConfigUnknownError); diff --git a/src/notification.zig b/src/notification.zig index 07a21a7..2c70f7a 100644 --- a/src/notification.zig +++ b/src/notification.zig @@ -28,7 +28,6 @@ const Error = enum { ConfigUnknownError, ConfigPathNotFound, CannotDeleteTrashDir, - DuplicateKeybinds, InvalidKeybind, NotADir, }; @@ -80,7 +79,6 @@ pub fn writeErr(self: *Self, err: Error) !void { .ConfigUnknownError => self.write("Could not read config due to an unknown error.", .err), .ConfigPathNotFound => self.write("Could not read config due to unset env variables. Please set either $HOME or $XDG_CONFIG_HOME.", .err), .CannotDeleteTrashDir => self.write("Cannot delete trash directory.", .err), - .DuplicateKeybinds => self.write("Config has keybinds with the same key. This can lead to undefined behaviour. Check log file for more information.", .err), .InvalidKeybind => self.write("Config has keybind(s) with invalid key(s).", .err), }; }