~leon_plickat/public-inbox

nfm: Expand tilde to $HOME v2 SUPERSEDED

Hugo Machet: 1
 Expand tilde to $HOME

 1 files changed, 13 insertions(+), 3 deletions(-)
Export patchset (mbox)
How do I use this?

Copy & paste the following snippet into your terminal to import this patchset into git:

curl -s https://lists.sr.ht/~leon_plickat/public-inbox/patches/30795/mbox | git am -3
Learn more about email & git

[PATCH nfm v2] Expand tilde to $HOME Export this patch

---
v1 -> v2:
Use $HOME instead of /home/$USER

 src/nfm.zig | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/nfm.zig b/src/nfm.zig
index b58a4a06f985..e4adefbf519e 100644
--- a/src/nfm.zig
+++ b/src/nfm.zig
@@ -73,13 +73,23 @@ pub const Context = struct {

    gpa: mem.Allocator = heap.page_allocator,

    // TODO expand '~'
    pub fn setCwd(self: *Context, dirname: []const u8) !void {
        if (dirname.len == 1 and dirname[0] == '.') return;

        const prev_basename = fs.path.basename(self.cwd.name);

        self.cwd = self.dirmap.getDirAndSetCwd(dirname) catch |err| {
        var name = dirname;
        if (dirname[0] == '~') {
            const home = os.getenv("HOME") orelse {
                context.mode.setMessage(.err, "$HOME is not set");
                return;
            };

            var buf: [100]u8 = undefined;
            name = try fmt.bufPrint(&buf, "{s}{s}", .{ home, dirname[1..] });
        }

        self.cwd = self.dirmap.getDirAndSetCwd(name) catch |err| {
            switch (err) {
                error.AccessDenied => {
                    self.mode.setMessage(.err, "Access Denied");
@@ -98,7 +108,7 @@ pub const Context = struct {
        };

        // If we go up one directory, ensure that the cursor is on the previous cwd.
        if (dirname[0] == '.' and dirname[1] == '.') {
        if (name[0] == '.' and name[1] == '.') {
            var i: usize = 0;
            for (self.cwd.files.items) |*dir| {
                if (mem.eql(u8, prev_basename, dir.name)) {
-- 
2.35.1