feat: load and edit key bindings in config directory

This commit is contained in:
CJ van den Berg 2024-11-19 18:11:22 +01:00
parent 3af2b09891
commit dc914ba562
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9
5 changed files with 61 additions and 1 deletions

View file

@ -7,6 +7,7 @@ const tp = @import("thespian");
const cbor = @import("cbor");
const builtin = @import("builtin");
const log = @import("log");
const root = @import("root");
const input = @import("input");
const command = @import("command");
@ -235,7 +236,12 @@ const BindingSet = struct {
.command = self.allocator.dupe(u8, "toggle_input_mode") catch @panic("failed to add toggle_input_mode fallback"),
.args = "",
}) catch {};
const json_string: []const u8 = builtin_keybinds.get(namespace_name) orelse return error.NotFound;
var free_json_string = true;
const json_string = root.read_keybind_namespace(self.allocator, namespace_name) orelse blk: {
free_json_string = false;
break :blk builtin_keybinds.get(namespace_name) orelse return error.NotFound;
};
defer if (free_json_string) self.allocator.free(json_string);
const parsed = try std.json.parseFromSlice(std.json.Value, self.allocator, json_string, .{});
defer parsed.deinit();
if (parsed.value != .object) return error.NotAnObject;
@ -504,6 +510,18 @@ pub const CursorShape = enum {
beam,
};
pub fn get_or_create_namespace_config_file(allocator: std.mem.Allocator, namespace_name: []const u8) ![]const u8 {
if (root.read_keybind_namespace(allocator, namespace_name)) |content| {
allocator.free(content);
} else {
try root.write_keybind_namespace(
namespace_name,
builtin_keybinds.get(namespace_name) orelse builtin_keybinds.get("flow").?,
);
}
return try root.get_keybind_namespace_file_name(namespace_name);
}
const expectEqual = std.testing.expectEqual;
const parse_test_cases = .{