fix: memory leak; add: keybindings J and K for file navigation
This commit is contained in:
@@ -92,7 +92,17 @@ pub fn Tree(App: type) type {
|
||||
this.scrollback = 0;
|
||||
this.idx = 0;
|
||||
},
|
||||
// TODO also support key inputs to change the current file?
|
||||
.key => |key| {
|
||||
if (key.eql(.{ .cp = 'J' }) and this.idx < this.len - 1) {
|
||||
this.idx += 1;
|
||||
this.queue.push(.{ .file = this.idx });
|
||||
}
|
||||
|
||||
if (key.eql(.{ .cp = 'K' }) and this.idx > 0) {
|
||||
this.idx -= 1;
|
||||
this.queue.push(.{ .file = this.idx });
|
||||
}
|
||||
},
|
||||
.mouse => |mouse| if (this.len > 0) switch (mouse.button) {
|
||||
.left => if (mouse.y + this.scrollback < this.len) {
|
||||
this.idx = mouse.y + this.scrollback;
|
||||
|
||||
10
src/main.zig
10
src/main.zig
@@ -46,13 +46,23 @@ pub fn main() !void {
|
||||
var element_root: tui_diff.elements.Root(App) = .init(allocator);
|
||||
var element_tree: tui_diff.elements.Tree(App) = .init(&app.queue);
|
||||
|
||||
// NOTE hold change set as entries of the changes for each file of in the `App.Model.changes`
|
||||
var change_set: std.ArrayList([]tui_diff.elements.Change(App)) = .empty;
|
||||
defer {
|
||||
for (change_set.items) |change| allocator.free(change);
|
||||
change_set.deinit(allocator);
|
||||
}
|
||||
|
||||
var iter = app.model.changes.iterator();
|
||||
var entry = iter.next();
|
||||
while (entry != null) : (entry = iter.next()) if (entry) |e| {
|
||||
var changes = try allocator.alloc(tui_diff.elements.Change(App), e.value_ptr.items.len);
|
||||
for (0.., e.value_ptr.items) |i, diff_index|
|
||||
changes[i] = .init(diff_index);
|
||||
// `tui_diff.elements.Diff` does not own the changes array because it only passes the
|
||||
// corresponding `Element` instances for the `Container` tree generation
|
||||
try element_root.diffs.append(allocator, try tui_diff.elements.Diff(App, allocator, changes));
|
||||
try change_set.append(allocator, changes);
|
||||
};
|
||||
|
||||
var root = try tui_diff.Container(App, allocator, &element_root, &element_tree);
|
||||
|
||||
Reference in New Issue
Block a user