Skip to content

Commit

Permalink
move tilde expansion functionality to os/homedir
Browse files Browse the repository at this point in the history
  • Loading branch information
z-jxy committed Dec 29, 2024
1 parent 31b32bc commit 965542b
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
35 changes: 13 additions & 22 deletions src/config/Config.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4257,33 +4257,24 @@ pub const RepeatablePath = struct {

// Check if the path starts with a tilde and expand it to the home directory on linux/mac
if (std.mem.startsWith(u8, path, "~/")) {
const home_var = try internal_os.home(&buf); // cache this?
if (home_var) |home_dir| {
const rest = path[1..]; // Skip the ~
const expanded_len = home_dir.len + rest.len;
if (expanded_len > buf.len) {
try diags.append(alloc, .{
.message = try std.fmt.allocPrintZ(
alloc,
"error resolving file path {s}: path too long after expanding home directory",
.{path},
),
});
self.value.items[i] = .{ .required = "" };
continue;
}

@memcpy(buf[home_dir.len..expanded_len], rest);

if (try internal_os.expandHome(path, &buf)) |expanded_path| {
log.debug(
"expanding file path from home directory: path={s}",
.{buf[0..expanded_len]},
.{expanded_path},
);

switch (self.value.items[i]) {
.optional, .required => |*p| p.* = try alloc.dupeZ(u8, buf[0..expanded_len]),
.optional, .required => |*p| p.* = try alloc.dupeZ(u8, expanded_path),
}

continue;
} else {
try diags.append(alloc, .{
.message = try std.fmt.allocPrintZ(
alloc,
"error expanding home path {s}",
.{path},
),
});
self.value.items[i] = .{ .required = "" };
continue;
}
}
Expand Down
16 changes: 16 additions & 0 deletions src/os/homedir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ fn trimSpace(input: []const u8) []const u8 {
return std.mem.trim(u8, input, " \n\t");
}

/// Expands a path that starts with a tilde (~) to the home directory of the user.
///
/// Errors if `home` fails or if the size of the expanded path is larger than `buf.len`.
///
/// Returns null if the value returned from `home` is null, otherwise returns a slice to the expanded path.
pub fn expandHome(path: []const u8, buf: []u8) !?[]u8 {
const home_dir = try home(buf) orelse return null;
const rest = path[1..]; // Skip the ~
const expanded_len = home_dir.len + rest.len;

if (expanded_len > buf.len) return Error.BufferTooSmall;
@memcpy(buf[home_dir.len..expanded_len], rest);

return buf[0..expanded_len];
}

test {
const testing = std.testing;

Expand Down
1 change: 1 addition & 0 deletions src/os/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub const freeTmpDir = file.freeTmpDir;
pub const isFlatpak = flatpak.isFlatpak;
pub const FlatpakHostCommand = flatpak.FlatpakHostCommand;
pub const home = homedir.home;
pub const expandHome = homedir.expandHome;
pub const ensureLocale = locale.ensureLocale;
pub const clickInterval = mouse.clickInterval;
pub const open = openpkg.open;
Expand Down

0 comments on commit 965542b

Please sign in to comment.