Repository navigation
Expand file tree
/
Copy pathfile_browser.zig
More file actions
174 lines (148 loc) · 6.05 KB
/
Copy pathfile_browser.zig
File metadata and controls
174 lines (148 loc) · 6.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//! ZigZag File Browser Example
//! Demonstrates the FilePicker component for file system navigation.
const std = @import("std");
const zz = @import("zigzag");
const Model = struct {
file_picker: zz.components.FilePicker,
preview: std.array_list.Managed(u8),
error_message: []const u8,
pub const Msg = union(enum) {
key: zz.KeyEvent,
};
pub fn init(self: *Model, ctx: *zz.Context) !zz.Cmd(Msg) {
self.file_picker = zz.components.FilePicker.init(ctx.persistent_allocator);
self.file_picker.height = ctx.height -| 10;
self.file_picker.setHomePath(ctx.home_dir);
// Start at home directory
self.file_picker.navigateHome(ctx.io) catch {
try self.file_picker.navigate(ctx.io, "/");
};
self.preview = std.array_list.Managed(u8).init(ctx.persistent_allocator);
self.error_message = "";
return .none;
}
pub fn update(self: *Model, msg: Msg, ctx: *zz.Context) zz.Cmd(Msg) {
switch (msg) {
.key => |k| {
switch (k.key) {
.char => |c| switch (c) {
'q' => return .quit,
else => {
const selected = self.file_picker.handleKey(ctx.io, k) catch false;
if (selected) {
self.loadPreview(ctx.io);
}
},
},
.escape => return .quit,
else => {
const selected = self.file_picker.handleKey(ctx.io, k) catch false;
if (selected) {
self.loadPreview(ctx.io);
}
},
}
// Update height based on context
self.file_picker.height = ctx.height -| 10;
},
}
return .none;
}
fn loadPreview(self: *Model, io: std.Io) void {
if (self.file_picker.getSelected()) |path| {
// Try to read file preview
const file = std.Io.Dir.openFileAbsolute(io, path, .{}) catch {
self.error_message = "Cannot open file";
self.preview.clearRetainingCapacity();
return;
};
defer file.close(io);
// Read first 500 bytes
var buf: [500]u8 = undefined;
const bytes_read = file.readPositionalAll(io, &buf, 0) catch {
self.error_message = "Cannot read file";
self.preview.clearRetainingCapacity();
return;
};
self.preview.clearRetainingCapacity();
self.preview.appendSlice(buf[0..bytes_read]) catch {
self.error_message = "Out of memory";
self.preview.clearRetainingCapacity();
return;
};
self.error_message = "";
}
}
pub fn view(self: *const Model, ctx: *const zz.Context) ![]const u8 {
// Title
var title_style = zz.Style{};
title_style = title_style.bold(true);
title_style = title_style.fg(zz.Color.cyan);
title_style = title_style.inline_style(true);
const title = try title_style.render(ctx.allocator, "File Browser");
// File picker
const picker_view = try self.file_picker.view(ctx.allocator);
// Preview section
var preview_section: []const u8 = "";
if (self.file_picker.getSelected()) |path| {
var path_style = zz.Style{};
path_style = path_style.fg(zz.Color.green);
path_style = path_style.inline_style(true);
const path_display = try path_style.render(ctx.allocator, path);
var preview_style = zz.Style{};
preview_style = preview_style.borderAll(zz.Border.normal);
preview_style = preview_style.borderForeground(zz.Color.gray(12));
const preview_content = if (self.preview.items.len > 0)
self.preview.items
else if (self.error_message.len > 0)
self.error_message
else
"(empty file)";
const preview_box = try preview_style.render(ctx.allocator, preview_content);
preview_section = try std.fmt.allocPrint(
ctx.allocator,
"\nSelected: {s}\n\n{s}",
.{ path_display, preview_box },
);
}
// Help
var help_style = zz.Style{};
help_style = help_style.fg(zz.Color.gray(12));
help_style = help_style.inline_style(true);
const help = try help_style.render(
ctx.allocator,
"Up/Down: Navigate Enter: Open/Select Backspace: Parent h: Toggle hidden ~: Home q: Quit",
);
// Get max width for centering title and help
const picker_width = zz.measure.maxLineWidth(picker_view);
const title_width = zz.measure.width(title);
const help_width = zz.measure.width(help);
const max_width = @max(picker_width, @max(title_width, help_width));
// Center title and help
const centered_title = try zz.place.place(ctx.allocator, max_width, 1, .center, .top, title);
const centered_help = try zz.place.place(ctx.allocator, max_width, 1, .center, .top, help);
const content = try std.fmt.allocPrint(
ctx.allocator,
"{s}\n\n{s}{s}\n\n{s}",
.{ centered_title, picker_view, preview_section, centered_help },
);
// Center horizontally but keep at top vertically (file browser needs vertical space)
return zz.place.place(
ctx.allocator,
ctx.width,
ctx.height,
.center,
.top,
content,
);
}
pub fn deinit(self: *Model) void {
self.file_picker.deinit();
self.preview.deinit();
}
};
pub fn main(init: std.process.Init) !void {
var program = zz.Program(Model).init(init.gpa, init.io, init.environ_map);
defer program.deinit();
try program.run();
}