refactor: simplify and improve management of config buffers

This commit is contained in:
CJ van den Berg 2025-07-16 19:40:11 +02:00
parent 489c4027cb
commit 46dfde7685
Signed by: neurocyte
GPG key ID: 8EB1E1BB660E3FB9

View file

@ -23,8 +23,9 @@ const Allocator = std.mem.Allocator;
allocator: Allocator, allocator: Allocator,
rdr_: renderer, rdr_: renderer,
config_: @import("config"), config_: @import("config"),
highlight_columns_: []u16, config_bufs: [][]const u8,
highlight_columns_configured: []u16, highlight_columns_: []const u16,
highlight_columns_configured: []const u16,
frame_time: usize, // in microseconds frame_time: usize, // in microseconds
frame_clock: tp.metronome, frame_clock: tp.metronome,
frame_clock_running: bool = false, frame_clock_running: bool = false,
@ -103,19 +104,12 @@ const InitError = error{
fn init(allocator: Allocator) InitError!*Self { fn init(allocator: Allocator) InitError!*Self {
var conf, const conf_bufs = root.read_config(@import("config"), allocator); var conf, const conf_bufs = root.read_config(@import("config"), allocator);
defer root.free_config(allocator, conf_bufs);
if (@hasDecl(renderer, "install_crash_handler") and conf.start_debugger_on_crash) if (@hasDecl(renderer, "install_crash_handler") and conf.start_debugger_on_crash)
renderer.jit_debugger_enabled = true; renderer.jit_debugger_enabled = true;
const theme_, const parsed_theme = get_theme_by_name(allocator, conf.theme) orelse get_theme_by_name(allocator, "dark_modern") orelse return error.UnknownTheme; const theme_, const parsed_theme = get_theme_by_name(allocator, conf.theme) orelse get_theme_by_name(allocator, "dark_modern") orelse return error.UnknownTheme;
conf.theme = theme_.name; conf.theme = theme_.name;
conf.whitespace_mode = try allocator.dupe(u8, conf.whitespace_mode);
conf.input_mode = try allocator.dupe(u8, conf.input_mode);
conf.top_bar = try allocator.dupe(u8, conf.top_bar);
conf.bottom_bar = try allocator.dupe(u8, conf.bottom_bar);
conf.include_files = try allocator.dupe(u8, conf.include_files);
conf.highlight_columns = try allocator.dupe(u8, conf.highlight_columns);
if (build_options.gui) conf.enable_terminal_cursor = false; if (build_options.gui) conf.enable_terminal_cursor = false;
const frame_rate: usize = @intCast(tp.env.get().num("frame-rate")); const frame_rate: usize = @intCast(tp.env.get().num("frame-rate"));
@ -138,8 +132,9 @@ fn init(allocator: Allocator) InitError!*Self {
self.* = .{ self.* = .{
.allocator = allocator, .allocator = allocator,
.config_ = conf, .config_ = conf,
.highlight_columns_ = if (conf.highlight_columns_enabled) highlight_columns__ else &.{}, .config_bufs = conf_bufs,
.highlight_columns_configured = highlight_columns__, .highlight_columns_ = if (conf.highlight_columns_enabled) conf.highlight_columns else &.{},
.highlight_columns_configured = conf.highlight_columns,
.rdr_ = try renderer.init(allocator, self, tp.env.get().is("no-alternate"), dispatch_initialized), .rdr_ = try renderer.init(allocator, self, tp.env.get().is("no-alternate"), dispatch_initialized),
.frame_time = frame_time, .frame_time = frame_time,
.frame_clock = frame_clock, .frame_clock = frame_clock,
@ -225,7 +220,6 @@ fn init_delayed(self: *Self) command.Result {
} }
fn deinit(self: *Self) void { fn deinit(self: *Self) void {
self.allocator.free(self.highlight_columns_configured);
if (self.mouse_idle_timer) |*t| { if (self.mouse_idle_timer) |*t| {
t.cancel() catch {}; t.cancel() catch {};
t.deinit(); t.deinit();
@ -256,6 +250,7 @@ fn deinit(self: *Self) void {
self.rdr_.deinit(); self.rdr_.deinit();
self.logger.deinit(); self.logger.deinit();
self.query_cache_.deinit(); self.query_cache_.deinit();
root.free_config(self.allocator, self.config_bufs);
self.allocator.destroy(self); self.allocator.destroy(self);
} }