summaryrefslogtreecommitdiff
path: root/pick.zig
diff options
context:
space:
mode:
Diffstat (limited to 'pick.zig')
-rw-r--r--pick.zig59
1 files changed, 33 insertions, 26 deletions
diff --git a/pick.zig b/pick.zig
index 80e35e8..f11adb3 100644
--- a/pick.zig
+++ b/pick.zig
@@ -50,21 +50,18 @@ pub const Ctx = struct {
.path = try gpa.alloc(u8, std.fs.MAX_PATH_BYTES),
.filename = try gpa.alloc(u8, std.fs.MAX_PATH_BYTES),
};
- ret.path.len = 0;
+ const path = ret.path[0..std.fs.MAX_PATH_BYTES];
+ ret.path = try std.os.realpath(".", path);
ret.filename.len = 0;
try ret.list_files();
return ret;
}
pub fn deinit(ctx: *Ctx) void {
- for (ctx.entries.items) |entry| {
- gpa.free(entry.name);
- }
+ for (ctx.entries.items) |entry| gpa.free(entry.name);
ctx.entries.deinit(gpa);
-
- @setRuntimeSafety(false);
- gpa.free(ctx.filename[0..std.fs.MAX_PATH_BYTES]);
- gpa.free(ctx.path[0..std.fs.MAX_PATH_BYTES]);
+ gpa.free(ctx.filename.ptr[0..std.fs.MAX_PATH_BYTES]);
+ gpa.free(ctx.path.ptr[0..std.fs.MAX_PATH_BYTES]);
}
const bg = dvui.Options{
@@ -79,7 +76,7 @@ pub const Ctx = struct {
pub const ExitStatus = enum { ok, exit };
pub fn frame(ctx: *Ctx) !ExitStatus {
- var box = try dvui.box(@src(), .vertical, bg.override(.{ .expand = .both }));
+ const box = try dvui.box(@src(), .vertical, bg.override(.{ .expand = .both }));
defer box.deinit();
try ctx.draw_top_path();
@@ -90,19 +87,13 @@ pub const Ctx = struct {
}
pub fn list_files(ctx: *Ctx) !void {
- const path = @as([*]u8, @ptrCast(ctx.path))[0..std.fs.MAX_PATH_BYTES];
- ctx.path = try std.os.realpath(".", path);
-
- const cwd = std.fs.cwd();
- var iterable = try cwd.openIterableDir(".", .{});
- defer iterable.close();
- var iterator = iterable.iterate();
-
- for (ctx.entries.items) |entry| {
- gpa.free(entry.name);
- }
+ for (ctx.entries.items) |entry| gpa.free(entry.name);
ctx.entries.clearRetainingCapacity();
+ var dir = try std.fs.openIterableDirAbsolute(ctx.path, .{});
+ defer dir.close();
+ var iterator = dir.iterate();
+
while (try iterator.next()) |iter_entry| {
const entry = try ctx.entries.addOne(gpa);
entry.name = try gpa.dupe(u8, iter_entry.name);
@@ -128,12 +119,29 @@ pub const Ctx = struct {
defer box.deinit();
if (try dvui.button(@src(), "..", opts)) {
- try std.os.chdir("..");
- try ctx.list_files();
+ try ctx.goto("..");
}
- const te = try dvui.textEntry(@src(), .{ .text = ctx.path }, bg);
- defer te.deinit();
+ const button_pressed = try dvui.button(@src(), "go", opts.override(.{ .gravity_x = 1 }));
+ if (button_pressed) {
+ try ctx.goto(ctx.path);
+ }
+
+ 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);
+
+ try ctx.list_files();
}
fn draw_bottom_filename(ctx: *Ctx) !?ExitStatus {
@@ -211,8 +219,7 @@ pub const Ctx = struct {
if (clicked) switch (entry.kind) {
.directory => {
- try std.os.chdir(entry.name);
- try ctx.list_files();
+ try ctx.goto(entry.name);
break :files;
},
else => {