diff --git a/src/cli/list_keybinds.zig b/src/cli/list_keybinds.zig index 2788de83d..625b6a8ec 100644 --- a/src/cli/list_keybinds.zig +++ b/src/cli/list_keybinds.zig @@ -63,15 +63,7 @@ fn iterConfig(stdout: anytype, iter: anytype) !u8 { while (iter.next()) |next| { const keys = next.key_ptr.*; const value = next.value_ptr.*; - try stdout.print("{s}", .{@tagName(value)}); - switch (value) { - .goto_tab => |val| try stdout.print(" {d}:", .{val}), - .jump_to_prompt => |val| try stdout.print(" {d}:", .{val}), - .increase_font_size, .decrease_font_size => |val| try stdout.print(" {d}:", .{val}), - .goto_split => |val| try stdout.print(" {s}:", .{@tagName(val)}), - .inspector => |val| try stdout.print(" {s}:", .{@tagName(val)}), - inline else => try stdout.print(":", .{}), - } + try stdout.print("{}", .{value}); switch (keys.key) { .one, .two, .three, .four, .five, .six, .seven, .eight, .nine => try stdout.print(" {d} +", .{(@intFromEnum(keys.key) - start) + 1}), diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 9198e2f53..d0af763a9 100644 --- a/src/input/Binding.zig +++ b/src/input/Binding.zig @@ -303,6 +303,41 @@ pub const Action = union(enum) { return Error.InvalidAction; } + /// Implements the formatter for the fmt package. This encodes the + /// action back into the format used by parse. + pub fn format( + self: Action, + comptime layout: []const u8, + opts: std.fmt.FormatOptions, + writer: anytype, + ) !void { + _ = layout; + _ = opts; + + switch (self) { + inline else => |value| { + const Value = @TypeOf(value); + const value_info = @typeInfo(Value); + + // All actions start with the tag. + try writer.print("{s}", .{@tagName(self)}); + + // Write the value depending on the type + switch (Value) { + void => {}, + []const u8 => try writer.print(":{s}", .{value}), + else => switch (value_info) { + .Enum => try writer.print(":{s}", .{@tagName(value)}), + .Float => try writer.print(":{d}", .{value}), + .Int => try writer.print(":{d}", .{value}), + .Struct => try writer.print("{} (not configurable)", .{value}), + else => @compileError("unhandled type: " ++ @typeName(Value)), + }, + } + }, + } + } + /// Returns a hash code that can be used to uniquely identify this /// action. pub fn hash(self: Action) u64 {