refactor(config): remove globals
Browse files
+5 −6 src/bar/bar.zig
| @@ -9,9 +9,8 @@ const ColorScheme = config_mod.ColorScheme; | |||
| 9 | const Monitor = monitor_mod.Monitor; | 9 | const Monitor = monitor_mod.Monitor; |
| 10 | const Block = blocks_mod.Block; | 10 | const Block = blocks_mod.Block; |
| 11 | 11 | ||
| 12 | -fn get_layout_symbol(layout_index: u32) []const u8 { | 12 | +fn get_layout_symbol(layout_index: u32, config: ?config_mod.Config) []const u8 { |
| 13 | - const cfg = config_mod.get_config(); | 13 | + if (config) |conf| { |
| 14 | - if (cfg) |conf| { | ||
| 15 | return switch (layout_index) { | 14 | return switch (layout_index) { |
| 16 | 0 => conf.layout_tile_symbol, | 15 | 0 => conf.layout_tile_symbol, |
| 17 | 1 => conf.layout_monocle_symbol, | 16 | 1 => conf.layout_monocle_symbol, |
| @@ -162,7 +161,7 @@ pub const Bar = struct { | |||
| 162 | self.needs_redraw = true; | 161 | self.needs_redraw = true; |
| 163 | } | 162 | } |
| 164 | 163 | ||
| 165 | - pub fn draw(self: *Bar, display: *xlib.Display, tags: []const []const u8) void { | 164 | + pub fn draw(self: *Bar, display: *xlib.Display, tags: []const []const u8, config: ?config_mod.Config) void { |
| 166 | if (!self.needs_redraw) return; | 165 | if (!self.needs_redraw) return; |
| 167 | 166 | ||
| 168 | self.fill_rect(display, 0, 0, self.width, self.height, self.scheme_normal.background); | 167 | self.fill_rect(display, 0, 0, self.width, self.height, self.scheme_normal.background); |
| @@ -196,7 +195,7 @@ pub const Bar = struct { | |||
| 196 | 195 | ||
| 197 | x_position += padding; | 196 | x_position += padding; |
| 198 | 197 | ||
| 199 | - const layout_symbol = get_layout_symbol(monitor.sel_lt); | 198 | + const layout_symbol = get_layout_symbol(monitor.sel_lt, config); |
| 200 | self.draw_text(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground); | 199 | self.draw_text(display, x_position, @divTrunc(self.height + self.font_height, 2) - 4, layout_symbol, self.scheme_normal.foreground); |
| 201 | x_position += self.text_width(display, layout_symbol) + padding; | 200 | x_position += self.text_width(display, layout_symbol) + padding; |
| 202 | 201 | ||
| @@ -326,7 +325,7 @@ pub fn draw_bars(display: *xlib.Display, tags: []const []const u8) void { | |||
| 326 | while (current_monitor) |monitor| { | 325 | while (current_monitor) |monitor| { |
| 327 | _ = monitor; | 326 | _ = monitor; |
| 328 | if (bars) |bar| { | 327 | if (bars) |bar| { |
| 329 | - bar.draw(display, tags); | 328 | + bar.draw(display, tags, null); |
| 330 | } | 329 | } |
| 331 | current_monitor = if (current_monitor) |m| m.next else null; | 330 | current_monitor = if (current_monitor) |m| m.next else null; |
| 332 | } | 331 | } |
+0 −10 src/config/config.zig
| @@ -184,13 +184,3 @@ pub const Config = struct { | |||
| 184 | try self.autostart.append(self.allocator, cmd); | 184 | try self.autostart.append(self.allocator, cmd); |
| 185 | } | 185 | } |
| 186 | }; | 186 | }; |
| 187 | - | ||
| 188 | -pub var global_config: ?*Config = null; | ||
| 189 | - | ||
| 190 | -pub fn get_config() ?*Config { | ||
| 191 | - return global_config; | ||
| 192 | -} | ||
| 193 | - | ||
| 194 | -pub fn set_config(cfg: *Config) void { | ||
| 195 | - global_config = cfg; | ||
| 196 | -} | ||
+3 −4 src/main.zig
| @@ -213,7 +213,6 @@ pub fn main() !void { | |||
| 213 | 213 | ||
| 214 | config = config_mod.Config.init(allocator); | 214 | config = config_mod.Config.init(allocator); |
| 215 | defer config.deinit(); | 215 | defer config.deinit(); |
| 216 | - config_mod.set_config(&config); | ||
| 217 | 216 | ||
| 218 | if (lua.init(&config)) { | 217 | if (lua.init(&config)) { |
| 219 | const loaded = if (std.fs.cwd().statFile(config_path)) |_| | 218 | const loaded = if (std.fs.cwd().statFile(config_path)) |_| |
| @@ -711,7 +710,7 @@ fn run_event_loop(display: *Display) void { | |||
| 711 | var current_bar = bar_mod.bars; | 710 | var current_bar = bar_mod.bars; |
| 712 | while (current_bar) |bar| { | 711 | while (current_bar) |bar| { |
| 713 | bar.update_blocks(); | 712 | bar.update_blocks(); |
| 714 | - bar.draw(display.handle, &tags); | 713 | + bar.draw(display.handle, &tags, config); |
| 715 | current_bar = bar.next; | 714 | current_bar = bar.next; |
| 716 | } | 715 | } |
| 717 | 716 | ||
| @@ -1011,7 +1010,7 @@ fn execute_action(display: *Display, action: config_mod.Action, int_arg: i32, st | |||
| 1011 | if (keybind_overlay) |overlay| { | 1010 | if (keybind_overlay) |overlay| { |
| 1012 | const mon = monitor_mod.selected_monitor orelse monitor_mod.monitors; | 1011 | const mon = monitor_mod.selected_monitor orelse monitor_mod.monitors; |
| 1013 | if (mon) |m| { | 1012 | if (mon) |m| { |
| 1014 | - overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h); | 1013 | + overlay.toggle(m.mon_x, m.mon_y, m.mon_w, m.mon_h, &config); |
| 1015 | } | 1014 | } |
| 1016 | } | 1015 | } |
| 1017 | }, | 1016 | }, |
| @@ -1860,7 +1859,7 @@ fn handle_expose(display: *Display, event: *xlib.XExposeEvent) void { | |||
| 1860 | 1859 | ||
| 1861 | if (bar_mod.window_to_bar(event.window)) |bar| { | 1860 | if (bar_mod.window_to_bar(event.window)) |bar| { |
| 1862 | bar.invalidate(); | 1861 | bar.invalidate(); |
| 1863 | - bar.draw(display.handle, &tags); | 1862 | + bar.draw(display.handle, &tags, config); |
| 1864 | } | 1863 | } |
| 1865 | } | 1864 | } |
| 1866 | 1865 | ||
+4 −5 src/overlay.zig
| @@ -89,19 +89,18 @@ pub const Keybind_Overlay = struct { | |||
| 89 | } | 89 | } |
| 90 | } | 90 | } |
| 91 | 91 | ||
| 92 | - pub fn toggle(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32) void { | 92 | + pub fn toggle(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32, config: *config_mod.Config) void { |
| 93 | if (self.visible) { | 93 | if (self.visible) { |
| 94 | self.hide(); | 94 | self.hide(); |
| 95 | } else { | 95 | } else { |
| 96 | - self.show(mon_x, mon_y, mon_w, mon_h); | 96 | + self.show(mon_x, mon_y, mon_w, mon_h, config); |
| 97 | } | 97 | } |
| 98 | } | 98 | } |
| 99 | 99 | ||
| 100 | - pub fn show(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32) void { | 100 | + pub fn show(self: *Keybind_Overlay, mon_x: i32, mon_y: i32, mon_w: i32, mon_h: i32, config: *config_mod.Config) void { |
| 101 | const display = self.display orelse return; | 101 | const display = self.display orelse return; |
| 102 | - const cfg = config_mod.get_config() orelse return; | ||
| 103 | 102 | ||
| 104 | - self.collect_keybinds(cfg); | 103 | + self.collect_keybinds(config); |
| 105 | if (self.line_count == 0) return; | 104 | if (self.line_count == 0) return; |
| 106 | 105 | ||
| 107 | var max_key_width: i32 = 0; | 106 | var max_key_width: i32 = 0; |