[PATCH nfm v3] Expand tilde to $HOME
Export this patch
---
v2 -> v3:
Display a message on bufPrint() error
Use a block to assign 'name'
src/nfm.zig | 23 ++++++++++++++++++++---
1 file changed, 20 insertions(+), 3 deletions(-)
diff --git a/src/nfm.zig b/src/nfm.zig
index b58a4a06f985..90508f4a1054 100644
--- a/src/nfm.zig
+++ b/src/nfm.zig
@@ -73,13 +73,30 @@ 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| {
+ const name = blk: {
+ if (dirname[0] == '~') {
+ const home = os.getenv("HOME") orelse {
+ self.mode.setMessage(.err, "$HOME is not set");
+ return;
+ };
+
+ var buf: [100]u8 = undefined;
+ // TODO: Maybe add an additional heap buffer that gets used when
+ // the stack buffer is not enough.
+ break :blk fmt.bufPrint(&buf, "{s}{s}", .{ home, dirname[1..] }) catch |err| {
+ self.mode.setMessage(.err, "Directory name is too long");
+ return err;
+ };
+ }
+ break :blk dirname;
+ };
+
+ self.cwd = self.dirmap.getDirAndSetCwd(name) catch |err| {
switch (err) {
error.AccessDenied => {
self.mode.setMessage(.err, "Access Denied");
@@ -98,7 +115,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
nfm/patches: FAILED in 1m1s
[Expand tilde to $HOME][0] v3 from [Hugo Machet][1]
[0]: https://lists.sr.ht/~leon_plickat/public-inbox/patches/30880
[1]: mailto:mail@hmachet.com
✓ #731988 SUCCESS nfm/patches/alpine.yml https://builds.sr.ht/~leon_plickat/job/731988
✗ #731989 FAILED nfm/patches/freebsd.yml https://builds.sr.ht/~leon_plickat/job/731989
Thanks!
Friendly greetings,
Leon Henrik Plickat