move shell expand of theme from theme.zig to config.zig

This commit is contained in:
phl
2026-08-15 20:04:12 +02:00
parent f748b17e27
commit c121734295
2 changed files with 52 additions and 32 deletions

View File

@@ -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;

View File

@@ -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(