feat: load and edit key bindings in config directory
This commit is contained in:
parent
3af2b09891
commit
dc914ba562
5 changed files with 61 additions and 1 deletions
27
src/main.zig
27
src/main.zig
|
@ -438,6 +438,20 @@ fn write_json_file(comptime T: type, data: T, allocator: std.mem.Allocator, file
|
|||
try cbor.JsonStream(std.fs.File).jsonWriteValue(&s, &iter);
|
||||
}
|
||||
|
||||
pub fn read_keybind_namespace(allocator: std.mem.Allocator, namespace_name: []const u8) ?[]const u8 {
|
||||
const file_name = get_keybind_namespace_file_name(namespace_name) catch return null;
|
||||
var file = std.fs.openFileAbsolute(file_name, .{ .mode = .read_only }) catch return null;
|
||||
defer file.close();
|
||||
return file.readToEndAlloc(allocator, 64 * 1024) catch null;
|
||||
}
|
||||
|
||||
pub fn write_keybind_namespace(namespace_name: []const u8, content: []const u8) !void {
|
||||
const file_name = try get_keybind_namespace_file_name(namespace_name);
|
||||
var file = try std.fs.createFileAbsolute(file_name, .{ .truncate = true });
|
||||
defer file.close();
|
||||
return file.writeAll(content);
|
||||
}
|
||||
|
||||
pub fn get_config_dir() ![]const u8 {
|
||||
return get_app_config_dir(application_name);
|
||||
}
|
||||
|
@ -478,6 +492,10 @@ fn get_app_config_dir(appname: []const u8) ![]const u8 {
|
|||
error.PathAlreadyExists => {},
|
||||
else => return e,
|
||||
};
|
||||
|
||||
var keybind_dir_buffer: [std.posix.PATH_MAX]u8 = undefined;
|
||||
std.fs.makeDirAbsolute(try std.fmt.bufPrint(&keybind_dir_buffer, "{s}/{s}", .{ config_dir, keybind_dir })) catch {};
|
||||
|
||||
return config_dir;
|
||||
}
|
||||
|
||||
|
@ -604,6 +622,15 @@ pub fn get_restore_file_name() ![]const u8 {
|
|||
return restore_file;
|
||||
}
|
||||
|
||||
const keybind_dir = "keys";
|
||||
|
||||
pub fn get_keybind_namespace_file_name(namespace_name: []const u8) ![]const u8 {
|
||||
const local = struct {
|
||||
var file_buffer: [std.posix.PATH_MAX]u8 = undefined;
|
||||
};
|
||||
return try std.fmt.bufPrint(&local.file_buffer, "{s}/{s}/{s}.json", .{ try get_app_config_dir(application_name), keybind_dir, namespace_name });
|
||||
}
|
||||
|
||||
fn restart() noreturn {
|
||||
const argv = [_]?[*:0]const u8{
|
||||
std.os.argv[0],
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue