cli/list-keybindings: stylistic changes

This commit is contained in:
Mitchell Hashimoto
2023-11-03 17:51:34 -07:00
parent debeba99db
commit e73d3db497

View File

@ -6,57 +6,40 @@ const Allocator = std.mem.Allocator;
const Config = @import("../config/Config.zig"); const Config = @import("../config/Config.zig");
pub const Options = struct { pub const Options = struct {
_arena: ?Arena = null, /// If true, print out the default keybinds instead of the ones
/// configured in the config file.
default: bool = false, default: bool = false,
pub fn deinit(self: *Options) void { pub fn deinit(self: Options) void {
if (self._arena) |arena| arena.deinit(); _ = self;
self.* = undefined;
} }
}; };
/// The "list-keybinds" command is used to list all the available keybinds for Ghostty. /// 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. /// When executed without any arguments this will list the current keybinds
/// 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 /// 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 /// The "--default" argument will print out all the default keybinds
/// configured for Ghostty
pub fn run(alloc: Allocator) !u8 { pub fn run(alloc: Allocator) !u8 {
var opts: Options = .{}; var opts: Options = .{};
defer opts.deinit(); defer opts.deinit();
var iter = try std.process.argsWithAllocator(alloc); {
defer iter.deinit(); var iter = try std.process.argsWithAllocator(alloc);
try args.parse(Options, alloc, &opts, &iter); defer iter.deinit();
try args.parse(Options, alloc, &opts, &iter);
if (opts.default) {
return try listDefaultKeybinds(alloc);
} }
return try listKeybinds(alloc); var config = if (opts.default) try Config.default(alloc) else try Config.load(alloc);
} defer config.deinit();
fn listKeybinds(alloc: Allocator) !u8 {
var loaded_config = try Config.load(alloc);
defer loaded_config.deinit();
const stdout = std.io.getStdOut().writer(); const stdout = std.io.getStdOut().writer();
var iter = loaded_config.keybind.set.bindings.iterator(); var iter = config.keybind.set.bindings.iterator();
return try iterConfig(&stdout, &iter);
}
fn listDefaultKeybinds(alloc: Allocator) !u8 {
var default = try Config.default(alloc);
defer default.deinit();
const stdout = std.io.getStdOut().writer();
var iter = default.keybind.set.bindings.iterator();
return try iterConfig(&stdout, &iter);
}
fn iterConfig(stdout: anytype, iter: anytype) !u8 {
while (iter.next()) |next| { while (iter.next()) |next| {
const keys = next.key_ptr.*; const keys = next.key_ptr.*;
const value = next.value_ptr.*; const value = next.value_ptr.*;