Add option to include docs when listing keybinds.

This commit is contained in:
Jeffrey C. Ollie
2024-01-23 00:26:38 -06:00
parent 63139ad7e4
commit d51b6d4799
2 changed files with 31 additions and 5 deletions

View File

@ -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;
}

View File

@ -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);
@ -2717,7 +2718,7 @@ pub const Keybinds = struct {
}
/// Used by Formatter
pub fn formatEntry(self: Keybinds, formatter: anytype) !void {
pub fn formatEntryDocs(self: Keybinds, docs: bool, formatter: anytype) !void {
if (self.set.bindings.size == 0) {
try formatter.formatEntry(void, {});
return;
@ -2728,6 +2729,23 @@ 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(
@ -2739,6 +2757,10 @@ pub const Keybinds = struct {
}
}
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);