diff --git a/src/cli/action.zig b/src/cli/action.zig index 4151d7aa2..38cc1fbd7 100644 --- a/src/cli/action.zig +++ b/src/cli/action.zig @@ -3,6 +3,7 @@ const Allocator = std.mem.Allocator; const list_fonts = @import("list_fonts.zig"); const version = @import("version.zig"); +const list_keybinds = @import("list_keybinds.zig"); /// Special commands that can be invoked via CLI flags. These are all /// invoked by using `+` as a CLI flag. The only exception is @@ -14,6 +15,9 @@ pub const Action = enum { /// List available fonts @"list-fonts", + /// List available keybinds + @"list-keybinds", + pub const Error = error{ /// Multiple actions were detected. You can specify at most one /// action on the CLI otherwise the behavior desired is ambiguous. @@ -52,6 +56,7 @@ pub const Action = enum { return switch (self) { .version => try version.run(), .@"list-fonts" => try list_fonts.run(alloc), + .@"list-keybinds" => try list_keybinds.run(alloc), }; } }; diff --git a/src/cli/list_keybinds.zig b/src/cli/list_keybinds.zig new file mode 100644 index 000000000..8d9585be9 --- /dev/null +++ b/src/cli/list_keybinds.zig @@ -0,0 +1,50 @@ +const std = @import("std"); +const inputpkg = @import("../input.zig"); +const args = @import("args.zig"); +const Arena = std.heap.ArenaAllocator; +const Allocator = std.mem.Allocator; +const Config = @import("../config/Config.zig"); + +pub const Options = struct { + /// If true, print out the default keybinds instead of the ones + /// configured in the config file. + default: bool = false, + + pub fn deinit(self: Options) void { + _ = self; + } +}; + +/// The "list-keybinds" command is used to list all the available keybinds +/// for Ghostty. +/// +/// When executed without any arguments this will list the current keybinds +/// loaded by the config file. If no config file is found or there aren't any +/// changes to the keybinds it will print out the default ones configured for +/// Ghostty +/// +/// The "--default" argument will print out all the default keybinds +/// configured for Ghostty +pub fn run(alloc: Allocator) !u8 { + var opts: Options = .{}; + defer opts.deinit(); + + { + var iter = try std.process.argsWithAllocator(alloc); + defer iter.deinit(); + try args.parse(Options, alloc, &opts, &iter); + } + + var config = if (opts.default) try Config.default(alloc) else try Config.load(alloc); + defer config.deinit(); + + const stdout = std.io.getStdOut().writer(); + var iter = config.keybind.set.bindings.iterator(); + while (iter.next()) |next| { + const keys = next.key_ptr.*; + const value = next.value_ptr.*; + try stdout.print("{}={}\n", .{ keys, value }); + } + + return 0; +} diff --git a/src/input/Binding.zig b/src/input/Binding.zig index 9198e2f53..0531d6b12 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 { @@ -381,6 +416,27 @@ pub const Trigger = extern struct { std.hash.autoHash(&hasher, self.physical); return hasher.final(); } + + /// Format implementation for fmt package. + pub fn format( + self: Trigger, + comptime layout: []const u8, + opts: std.fmt.FormatOptions, + writer: anytype, + ) !void { + _ = layout; + _ = opts; + + // Modifiers first + if (self.mods.super) try writer.writeAll("super+"); + if (self.mods.ctrl) try writer.writeAll("ctrl+"); + if (self.mods.alt) try writer.writeAll("alt+"); + if (self.mods.shift) try writer.writeAll("shift+"); + + // Key + if (self.physical) try writer.writeAll("physical:"); + try writer.print("{s}", .{@tagName(self.key)}); + } }; /// A structure that contains a set of bindings and focuses on fast lookup.