fix: memory leak; add: keybindings J and K for file navigation

This commit is contained in:
2026-01-08 23:20:33 +01:00
parent 8d98d3226a
commit 3e9fc4ce9b
2 changed files with 21 additions and 1 deletions

View File

@@ -92,7 +92,17 @@ pub fn Tree(App: type) type {
this.scrollback = 0; this.scrollback = 0;
this.idx = 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) { .mouse => |mouse| if (this.len > 0) switch (mouse.button) {
.left => if (mouse.y + this.scrollback < this.len) { .left => if (mouse.y + this.scrollback < this.len) {
this.idx = mouse.y + this.scrollback; this.idx = mouse.y + this.scrollback;

View File

@@ -46,13 +46,23 @@ pub fn main() !void {
var element_root: tui_diff.elements.Root(App) = .init(allocator); var element_root: tui_diff.elements.Root(App) = .init(allocator);
var element_tree: tui_diff.elements.Tree(App) = .init(&app.queue); 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 iter = app.model.changes.iterator();
var entry = iter.next(); var entry = iter.next();
while (entry != null) : (entry = iter.next()) if (entry) |e| { while (entry != null) : (entry = iter.next()) if (entry) |e| {
var changes = try allocator.alloc(tui_diff.elements.Change(App), e.value_ptr.items.len); var changes = try allocator.alloc(tui_diff.elements.Change(App), e.value_ptr.items.len);
for (0.., e.value_ptr.items) |i, diff_index| for (0.., e.value_ptr.items) |i, diff_index|
changes[i] = .init(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 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); var root = try tui_diff.Container(App, allocator, &element_root, &element_tree);