diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-13 01:24:16 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-13 01:34:46 -0300 |
| commit | 5a40dca5adb6e9e650f55f3ec6f254a13bf50493 (patch) | |
| tree | 5fdffebd75ed3fbac1eae5dec0a3f2d491450342 /gui/src/Navigation.zig | |
| parent | 4b4113a0217ee27c78e7efddb66ff5a455d49083 (diff) | |
| download | computeranim-5a40dca5adb6e9e650f55f3ec6f254a13bf50493.tar.gz computeranim-5a40dca5adb6e9e650f55f3ec6f254a13bf50493.zip | |
debugger: align source and add rich Vim-style navigation
Diffstat (limited to 'gui/src/Navigation.zig')
| -rw-r--r-- | gui/src/Navigation.zig | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/gui/src/Navigation.zig b/gui/src/Navigation.zig new file mode 100644 index 0000000..f9ae34a --- /dev/null +++ b/gui/src/Navigation.zig @@ -0,0 +1,56 @@ +//! Bounded per-pane jump history. Scrolling changes the current location, not the target. +const std = @import("std"); +const Navigation = @This(); +pub const Location = struct { address: u64 = 0, row: f32 = 0, follow: bool = true }; +place: Location = .{}, +history: [32]Location = @splat(.{}), +len: u8 = 0, +cursor: u8 = 0, + +pub fn visit(n: *Navigation, address: u64) void { + if (n.place.address == address and !n.place.follow) return; + if (n.len == 0) { + n.history[0] = n.place; + n.len = 1; + } + n.history[n.cursor] = n.place; + n.len = n.cursor + 1; + if (n.len == n.history.len) { + std.mem.copyForwards(Location, n.history[0..31], n.history[1..]); + n.len -= 1; + } + n.cursor = n.len; + n.place = .{ .address = address, .follow = false }; + n.history[n.len] = n.place; + n.len += 1; +} + +pub fn back(n: *Navigation) void { + if (n.len == 0 or n.cursor == 0) return; + n.history[n.cursor] = n.place; + n.cursor -= 1; + n.place = n.history[n.cursor]; +} + +pub fn forward(n: *Navigation) void { + if (n.cursor + 1 >= n.len) return; + n.history[n.cursor] = n.place; + n.cursor += 1; + n.place = n.history[n.cursor]; +} + +test "pane jumps retain scroll and discard the abandoned forward branch" { + var n: Navigation = .{}; + n.visit(0x1000); + n.place.row = 3; + n.visit(0x2000); + n.back(); + try std.testing.expectEqual(@as(f32, 3), n.place.row); + n.visit(0x3000); + n.forward(); + try std.testing.expectEqual(@as(u64, 0x3000), n.place.address); + for (0..40) |i| n.visit(0x4000 + i); + try std.testing.expectEqual(@as(u8, 32), n.len); + for (0..40) |_| n.back(); + try std.testing.expectEqual(@as(u64, 0x4008), n.place.address); +} |
