summaryrefslogtreecommitdiff
path: root/pick.zig
diff options
context:
space:
mode:
Diffstat (limited to 'pick.zig')
-rw-r--r--pick.zig68
1 files changed, 48 insertions, 20 deletions
diff --git a/pick.zig b/pick.zig
index 76b1213..ed07266 100644
--- a/pick.zig
+++ b/pick.zig
@@ -21,6 +21,7 @@ pub fn main() !void {
win.theme = &dvui.Adwaita.light;
var ctx = try Ctx.init();
+ defer ctx.deinit();
loop: while (true) {
var nstime = win.beginWait(backend.hasEvent());
@@ -40,16 +41,32 @@ pub fn main() !void {
}
pub const Ctx = struct {
- path: []u8 = "",
- filename: []u8 = "",
+ path: []u8,
+ filename: []u8,
entries: std.ArrayListUnmanaged(std.fs.IterableDir.Entry) = .{},
pub fn init() !Ctx {
- var ret = Ctx{};
+ var ret = Ctx{
+ .path = try gpa.alloc(u8, std.fs.MAX_PATH_BYTES),
+ .filename = try gpa.alloc(u8, std.fs.MAX_PATH_BYTES),
+ };
+ ret.path.len = 0;
+ 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);
+ }
+ 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]);
+ }
+
pub const ExitStatus = enum { ok, exit };
pub fn frame(ctx: *Ctx) !ExitStatus {
const bg = dvui.Options{
@@ -105,13 +122,19 @@ pub const Ctx = struct {
defer tl.deinit();
}
{
+ var inner = try dvui.box(@src(), .vertical, .{
+ .expand = .both,
+ .margin = dvui.Rect.all(12),
+ });
+ defer inner.deinit();
+
var scroll = try dvui.scrollArea(@src(), .{}, .{ .expand = .both });
defer scroll.deinit();
- for (ctx.entries.items, 0..) |entry, i| {
+ entries: for (ctx.entries.items, 0..) |entry, i| {
const name, const icon, const color_style: dvui.Theme.ColorStyle = switch (entry.kind) {
- .directory => .{ "folder", entypo.folder, .window },
- .file => .{ "file", entypo.text_document, .control },
+ .directory => .{ "folder", entypo.folder, .control },
+ .file => .{ "file", entypo.text_document, .window },
else => .{ "other", entypo.help, .content },
};
@@ -140,10 +163,11 @@ pub const Ctx = struct {
.directory => {
try std.os.chdir(entry.name);
try ctx.list_files();
+ break :entries;
},
else => {
- gpa.free(ctx.filename);
- ctx.filename = try gpa.dupe(u8, entry.name);
+ ctx.filename.len = entry.name.len;
+ std.mem.copy(u8, ctx.filename, entry.name);
},
};
}
@@ -153,32 +177,36 @@ pub const Ctx = struct {
}
pub fn list_files(ctx: *Ctx) !void {
- var buf: [std.fs.MAX_PATH_BYTES]u8 = undefined;
- const path = try std.os.realpath(".", &buf);
- ctx.path = try gpa.dupe(u8, path);
+ 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();
- const iterable = try cwd.openIterableDir(".", .{});
+ var iterable = try cwd.openIterableDir(".", .{});
+ defer iterable.close();
var iterator = iterable.iterate();
+ for (ctx.entries.items) |entry| {
+ gpa.free(entry.name);
+ }
ctx.entries.clearRetainingCapacity();
- while (try iterator.next()) |entry| {
- const name = try gpa.dupe(u8, entry.name);
- try ctx.entries.append(gpa, .{ .name = name, .kind = entry.kind });
+ while (try iterator.next()) |iter_entry| {
+ const entry = try ctx.entries.addOne(gpa);
+ entry.name = try gpa.dupe(u8, iter_entry.name);
+ entry.kind = iter_entry.kind;
}
const Entry = std.fs.IterableDir.Entry;
const items = ctx.entries.items;
- const closures = struct {
- pub fn less_than_0(_: void, l: Entry, r: Entry) bool {
+ const sort = struct {
+ pub fn less_than_name(_: void, l: Entry, r: Entry) bool {
return std.mem.lessThan(u8, l.name, r.name);
}
- pub fn less_than_1(_: void, l: Entry, r: Entry) bool {
+ pub fn less_than_kind(_: void, l: Entry, r: Entry) bool {
return @intFromEnum(l.kind) < @intFromEnum(r.kind);
}
};
- std.mem.sort(Entry, items, {}, closures.less_than_0);
- std.mem.sort(Entry, items, {}, closures.less_than_1);
+ std.mem.sort(Entry, items, {}, sort.less_than_name);
+ std.mem.sort(Entry, items, {}, sort.less_than_kind);
}
};