refactor: remove obsolete json config file writing support
json config files can still be read, but no longer written.
This commit is contained in:
parent
32f4bcec3a
commit
2609ce4fbc
2 changed files with 8 additions and 26 deletions
28
src/main.zig
28
src/main.zig
|
|
@ -498,9 +498,8 @@ var config_mutex: std.Thread.Mutex = .{};
|
||||||
pub fn exists_config(T: type) bool {
|
pub fn exists_config(T: type) bool {
|
||||||
config_mutex.lock();
|
config_mutex.lock();
|
||||||
defer config_mutex.unlock();
|
defer config_mutex.unlock();
|
||||||
const json_file_name = get_app_config_file_name(application_name, @typeName(T)) catch return false;
|
const 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(file_name, .{ .mode = .read_only }) catch return false;
|
||||||
var file = std.fs.openFileAbsolute(text_file_name, .{ .mode = .read_only }) catch return false;
|
|
||||||
defer file.close();
|
defer file.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
@ -520,19 +519,15 @@ pub fn read_config(T: type, allocator: std.mem.Allocator) struct { T, [][]const
|
||||||
config_mutex.lock();
|
config_mutex.lock();
|
||||||
defer config_mutex.unlock();
|
defer config_mutex.unlock();
|
||||||
var bufs: [][]const u8 = &[_][]const u8{};
|
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 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];
|
|
||||||
var conf: T = get_default(T);
|
var conf: T = get_default(T);
|
||||||
if (!read_config_file(T, allocator, &conf, &bufs, text_file_name)) {
|
_ = read_config_file(T, allocator, &conf, &bufs, file_name);
|
||||||
_ = read_config_file(T, allocator, &conf, &bufs, json_file_name);
|
|
||||||
}
|
|
||||||
read_nested_include_files(T, allocator, &conf, &bufs);
|
read_nested_include_files(T, allocator, &conf, &bufs);
|
||||||
return .{ conf, bufs };
|
return .{ conf, bufs };
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns true if the file was found
|
// 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 {
|
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: {
|
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 (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;
|
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);
|
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 {
|
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;
|
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;
|
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 {
|
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 {
|
fn get_app_config_dir_file_name(appname: []const u8, comptime config_file_name: []const u8) ConfigDirError![]const u8 {
|
||||||
|
|
|
||||||
|
|
@ -349,7 +349,7 @@ fn open_style_config(self: *Self, Style: type) command.Result {
|
||||||
tui.reset_drag_context();
|
tui.reset_drag_context();
|
||||||
try self.create_editor();
|
try self.create_editor();
|
||||||
try command.executeName("open_scratch_buffer", command.fmt(.{
|
try command.executeName("open_scratch_buffer", command.fmt(.{
|
||||||
file_name[0 .. file_name.len - ".json".len],
|
file_name,
|
||||||
conf.written(),
|
conf.written(),
|
||||||
"conf",
|
"conf",
|
||||||
}));
|
}));
|
||||||
|
|
@ -640,13 +640,13 @@ const cmds = struct {
|
||||||
|
|
||||||
pub fn open_config(_: *Self, _: Ctx) Result {
|
pub fn open_config(_: *Self, _: Ctx) Result {
|
||||||
const file_name = try root.get_config_file_name(@import("config"));
|
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 const open_config_meta: Meta = .{ .description = "Edit configuration" };
|
||||||
|
|
||||||
pub fn open_gui_config(_: *Self, _: Ctx) Result {
|
pub fn open_gui_config(_: *Self, _: Ctx) Result {
|
||||||
const file_name = try root.get_config_file_name(@import("gui_config"));
|
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" };
|
pub const open_gui_config_meta: Meta = .{ .description = "Edit gui configuration" };
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue