From 3ca0fa951817b6fa230e3c37ebd4102e5c29b081 Mon Sep 17 00:00:00 2001 From: Kitty-Cricket Piapiac Date: Wed, 15 Nov 2023 00:56:42 +0000 Subject: allow using text entry to goto dir --- pick.zig | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/pick.zig b/pick.zig index f11adb3..3b17fbd 100644 --- a/pick.zig +++ b/pick.zig @@ -50,8 +50,9 @@ pub const Ctx = struct { .path = try gpa.alloc(u8, std.fs.MAX_PATH_BYTES), .filename = try gpa.alloc(u8, std.fs.MAX_PATH_BYTES), }; + @memset(ret.path, 0); const path = ret.path[0..std.fs.MAX_PATH_BYTES]; - ret.path = try std.os.realpath(".", path); + _ = try std.os.realpath(".", path); ret.filename.len = 0; try ret.list_files(); return ret; @@ -90,7 +91,10 @@ pub const Ctx = struct { for (ctx.entries.items) |entry| gpa.free(entry.name); ctx.entries.clearRetainingCapacity(); - var dir = try std.fs.openIterableDirAbsolute(ctx.path, .{}); + const len = std.mem.indexOfScalar(u8, ctx.path, 0) orelse ctx.path.len; + const path = ctx.path[0..len]; + + var dir = try std.fs.openIterableDirAbsolute(path, .{}); defer dir.close(); var iterator = dir.iterate(); @@ -124,23 +128,18 @@ pub const Ctx = struct { const button_pressed = try dvui.button(@src(), "go", opts.override(.{ .gravity_x = 1 })); if (button_pressed) { - try ctx.goto(ctx.path); + try ctx.list_files(); } const text = try dvui.textEntry(@src(), .{ .text = ctx.path }, bg); defer text.deinit(); } - fn goto(ctx: *Ctx, dir: []const u8) !void { - var tmp_buf = std.mem.zeroes([std.fs.MAX_PATH_BYTES]u8); - var tmp = std.heap.FixedBufferAllocator.init(&tmp_buf); - - const path = try std.fs.path.resolve(tmp.allocator(), &.{ ctx.path, dir }); - ctx.path.len = path.len; - - const buf = ctx.path.ptr[0..std.fs.MAX_PATH_BYTES]; - @memcpy(buf, &tmp_buf); + fn goto(ctx: *Ctx, dirname: []const u8) !void { + var dir = try std.fs.openDirAbsoluteZ(@ptrCast(ctx.path), .{}); + defer dir.close(); + _ = try dir.realpath(dirname, ctx.path); try ctx.list_files(); } -- cgit v1.2.3