diff --git a/src/cli/list_keybinds.zig b/src/cli/list_keybinds.zig index 1f5f4aa1f..56d6bb89a 100644 --- a/src/cli/list_keybinds.zig +++ b/src/cli/list_keybinds.zig @@ -7,15 +7,19 @@ const configpkg = @import("../config.zig"); const Config = configpkg.Config; pub const Options = struct { - /// If true, print out the default keybinds instead of the ones - /// configured in the config file. + /// If `true`, print out the default keybinds instead of the ones configured + /// in the config file. default: bool = false, + /// If `true`, print out documenation about the action associated with the + /// keybinds. + docs: bool = false, + pub fn deinit(self: Options) void { _ = self; } - /// Enables "-h" and "--help" to work. + /// Enables `-h` and `--help` to work. pub fn help(self: Options) !void { _ = self; return Action.help_error; @@ -46,7 +50,7 @@ pub fn run(alloc: Allocator) !u8 { defer config.deinit(); const stdout = std.io.getStdOut().writer(); - try config.keybind.formatEntry(configpkg.entryFormatter("keybind", stdout)); + try config.keybind.formatEntryDocs(opts.docs, configpkg.entryFormatter("keybind", stdout)); return 0; } diff --git a/src/config/Config.zig b/src/config/Config.zig index 7e3634819..46b6f13b0 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -29,6 +29,7 @@ const Key = @import("key.zig").Key; const KeyValue = @import("key.zig").Value; const ErrorList = @import("ErrorList.zig"); const MetricModifier = fontpkg.face.Metrics.Modifier; +const help_strings = @import("help_strings"); const log = std.log.scoped(.config); @@ -2774,8 +2775,8 @@ pub const Keybinds = struct { return true; } - /// Used by Formatter - pub fn formatEntry(self: Keybinds, formatter: anytype) !void { + /// Like formatEntry but has an option to include docs. + pub fn formatEntryDocs(self: Keybinds, formatter: anytype, docs: bool) !void { if (self.set.bindings.size == 0) { try formatter.formatEntry(void, {}); return; @@ -2786,6 +2787,24 @@ pub const Keybinds = struct { while (iter.next()) |next| { const k = next.key_ptr.*; const v = next.value_ptr.*; + if (docs) { + try formatter.writer.writeAll("\n"); + const name = @tagName(v); + inline for (@typeInfo(help_strings.KeybindAction).Struct.decls) |decl| { + if (std.mem.eql(u8, decl.name, name)) { + const help = @field(help_strings.KeybindAction, decl.name); + try formatter.writer.writeAll("# " ++ decl.name ++ "\n"); + var lines = std.mem.splitScalar(u8, help, '\n'); + while (lines.next()) |line| { + try formatter.writer.writeAll("# "); + try formatter.writer.writeAll(line); + try formatter.writer.writeAll("\n"); + } + break; + } + } + } + try formatter.formatEntry( []const u8, std.fmt.bufPrint( @@ -2797,6 +2816,11 @@ pub const Keybinds = struct { } } + /// Used by Formatter + pub fn formatEntry(self: Keybinds, formatter: anytype) !void { + try self.formatEntryDocs(false, formatter); + } + test "parseCLI" { const testing = std.testing; var arena = ArenaAllocator.init(testing.allocator);