From c1217342958b90ed3a25413c1616dfd2dd8cd1bf Mon Sep 17 00:00:00 2001 From: phl Date: Sat, 15 Aug 2026 20:04:12 +0200 Subject: [PATCH] move shell expand of theme from theme.zig to config.zig --- src/config/Config.zig | 52 ++++++++++++++++++++++++++++++++++++++++++- src/config/theme.zig | 32 +------------------------- 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/src/config/Config.zig b/src/config/Config.zig index 814532b5d..bb4e7a38d 100644 --- a/src/config/Config.zig +++ b/src/config/Config.zig @@ -9973,9 +9973,32 @@ pub const Theme = struct { // Trim our value const trimmed = std.mem.trim(u8, input, cli.args.whitespace); + // Expand our value + var buf: [std.fs.max_path_bytes]u8 = undefined; + const expanded = expanded: { + if (!std.mem.startsWith(u8, trimmed, "~/")) + break :expanded trimmed; + + var environ_map = global.environMap() catch |err| { + log.warn("error getting environment map for theme path {s}: {}", .{ trimmed, err }); + break :expanded trimmed; + }; + defer environ_map.deinit(); + + break :expanded internal_os.expandHome( + global.io(), + &environ_map, + trimmed, + &buf, + ) catch |err| { + log.warn("error expanding home directory in theme path {s}: {}", .{ trimmed, err }); + break :expanded trimmed; + }; + }; + // Set the value to the specified value directly. self.* = .{ - .light = try alloc.dupeZ(u8, trimmed), + .light = try alloc.dupeZ(u8, expanded), .dark = self.light, }; } @@ -10028,6 +10051,33 @@ pub const Theme = struct { try testing.expectEqualStrings("foo", v.dark); } + // Expand home + { + var environ_map = try testing.environ.createMap(alloc); + defer environ_map.deinit(); + + var home_buf: [std.fs.max_path_bytes]u8 = undefined; + const home = try internal_os.expandHome( + testing.io, + &environ_map, + "~/theme", + &home_buf, + ); + + var v: Theme = undefined; + try v.parseCLI(alloc, "~/theme/foo"); + + var expected_buf: [std.fs.max_path_bytes]u8 = undefined; + const expected = try std.fmt.bufPrint( + &expected_buf, + "{s}/theme/foo", + .{home}, + ); + + try testing.expectEqualStrings(expected, v.light); + try testing.expectEqualStrings(expected, v.dark); + } + // Light/dark { var v: Theme = undefined; diff --git a/src/config/theme.zig b/src/config/theme.zig index 0a3a8ecee..a8c39cad9 100644 --- a/src/config/theme.zig +++ b/src/config/theme.zig @@ -5,8 +5,6 @@ const internal_os = @import("../os/main.zig"); const cli = @import("../cli.zig"); const global = @import("../global.zig"); -const log = std.log.scoped(.config); - /// Location of possible themes. The order of this enum matters because it /// defines the priority of theme search (from top to bottom). pub const Location = enum { @@ -115,40 +113,12 @@ pub const LocationIterator = struct { /// will be added to the list and null will be returned. pub fn open( arena_alloc: Allocator, - theme_: []const u8, + theme: []const u8, diags: *cli.DiagnosticList, ) error{ OutOfMemory, Unexpected }!?struct { path: []const u8, file: std.Io.File, } { - var buf: [std.fs.max_path_bytes]u8 = undefined; - const theme = expanded: { - if (!std.mem.startsWith(u8, theme_, "~/")) - break :expanded theme_; - - var environ_map = global.environMap() catch |err| { - log.warn( - "error getting environment map when expanding theme path={s}: {}", - .{ theme_, err }, - ); - break :expanded theme_; - }; - defer environ_map.deinit(); - - break :expanded internal_os.expandHome( - global.io(), - &environ_map, - theme_, - &buf, - ) catch |err| { - log.warn( - "error expanding home directory for theme path={s}: {}", - .{ theme_, err }, - ); - break :expanded theme_; - }; - }; - // Absolute themes are loaded a different path. if (std.fs.path.isAbsolute(theme)) { const file: std.Io.File = try openAbsolute(