From 2609ce4fbcfcc9656c6f780b91fb9d035e07cd15 Mon Sep 17 00:00:00 2001 From: CJ van den Berg Date: Tue, 27 Jan 2026 17:39:24 +0100 Subject: [PATCH] refactor: remove obsolete json config file writing support json config files can still be read, but no longer written. --- src/main.zig | 28 +++++----------------------- src/tui/mainview.zig | 6 +++--- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/src/main.zig b/src/main.zig index b864adf..b7813cc 100644 --- a/src/main.zig +++ b/src/main.zig @@ -498,9 +498,8 @@ var config_mutex: std.Thread.Mutex = .{}; pub fn exists_config(T: type) bool { config_mutex.lock(); defer config_mutex.unlock(); - const json_file_name = get_app_config_file_name(application_name, @typeName(T)) catch return false; - const text_file_name = json_file_name[0 .. json_file_name.len - ".json".len]; - var file = std.fs.openFileAbsolute(text_file_name, .{ .mode = .read_only }) catch return false; + const file_name = get_app_config_file_name(application_name, @typeName(T)) catch return false; + var file = std.fs.openFileAbsolute(file_name, .{ .mode = .read_only }) catch return false; defer file.close(); return true; } @@ -520,19 +519,15 @@ pub fn read_config(T: type, allocator: std.mem.Allocator) struct { T, [][]const config_mutex.lock(); defer config_mutex.unlock(); var bufs: [][]const u8 = &[_][]const u8{}; - const json_file_name = get_app_config_file_name(application_name, @typeName(T)) catch return .{ get_default(T), bufs }; - const text_file_name = json_file_name[0 .. json_file_name.len - ".json".len]; + const file_name = get_app_config_file_name(application_name, @typeName(T)) catch return .{ get_default(T), bufs }; var conf: T = get_default(T); - if (!read_config_file(T, allocator, &conf, &bufs, text_file_name)) { - _ = read_config_file(T, allocator, &conf, &bufs, json_file_name); - } + _ = read_config_file(T, allocator, &conf, &bufs, file_name); read_nested_include_files(T, allocator, &conf, &bufs); return .{ conf, bufs }; } // returns true if the file was found fn read_config_file(T: type, allocator: std.mem.Allocator, conf: *T, bufs: *[][]const u8, file_name: []const u8) bool { - // std.log.info("loading {s}", .{file_name}); const err: anyerror = blk: { if (std.mem.endsWith(u8, file_name, ".json")) if (read_json_config_file(T, allocator, conf, bufs, file_name)) return true else |e| break :blk e; if (read_text_config_file(T, allocator, conf, bufs, file_name)) return true else |e| break :blk e; @@ -817,19 +812,6 @@ fn config_eql(config_type: type, T: type, a: T, b: T) bool { unsupported_error(config_type, T); } -fn write_json_file(comptime T: type, data: T, allocator: std.mem.Allocator, file_name: []const u8) !void { - var file = try std.fs.createFileAbsolute(file_name, .{ .truncate = true }); - defer file.close(); - - var cb = std.ArrayList(u8).init(allocator); - defer cb.deinit(); - try cbor.writeValue(cb.writer(), data); - - var s = std.json.writeStream(file.writer(), .{ .whitespace = .indent_4 }); - var iter: []const u8 = cb.items; - 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; @@ -1044,7 +1026,7 @@ fn get_app_state_dir(appname: []const u8) ![]const u8 { } fn get_app_config_file_name(appname: []const u8, comptime base_name: []const u8) ConfigDirError![]const u8 { - return get_app_config_dir_file_name(appname, base_name ++ ".json"); + return get_app_config_dir_file_name(appname, base_name); } fn get_app_config_dir_file_name(appname: []const u8, comptime config_file_name: []const u8) ConfigDirError![]const u8 { diff --git a/src/tui/mainview.zig b/src/tui/mainview.zig index 754d243..e237a9e 100644 --- a/src/tui/mainview.zig +++ b/src/tui/mainview.zig @@ -349,7 +349,7 @@ fn open_style_config(self: *Self, Style: type) command.Result { tui.reset_drag_context(); try self.create_editor(); try command.executeName("open_scratch_buffer", command.fmt(.{ - file_name[0 .. file_name.len - ".json".len], + file_name, conf.written(), "conf", })); @@ -640,13 +640,13 @@ const cmds = struct { pub fn open_config(_: *Self, _: Ctx) Result { const file_name = try root.get_config_file_name(@import("config")); - try tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_name[0 .. file_name.len - 5] } }); + try tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_name } }); } pub const open_config_meta: Meta = .{ .description = "Edit configuration" }; pub fn open_gui_config(_: *Self, _: Ctx) Result { const file_name = try root.get_config_file_name(@import("gui_config")); - try tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_name[0 .. file_name.len - ".json".len] } }); + try tp.self_pid().send(.{ "cmd", "navigate", .{ .file = file_name } }); } pub const open_gui_config_meta: Meta = .{ .description = "Edit gui configuration" };