diff options
Diffstat (limited to 'anim/src/aligned.zig')
| -rw-r--r-- | anim/src/aligned.zig | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/anim/src/aligned.zig b/anim/src/aligned.zig index ce2f5a0..24601c2 100644 --- a/anim/src/aligned.zig +++ b/anim/src/aligned.zig @@ -37,6 +37,57 @@ const theme = ui.theme; const Rect = ui.Rect; const t = theme.color.text; +pub const LiveRow = struct { + instruction: ?usize, + source: ?@import("ztrace").store.Line = null, + again: bool = false, +}; + +/// The same column alignment for a bounded live listing. Source-only gaps have no address. +pub fn livePlan(lines: []const ?@import("ztrace").store.Line, out: []LiveRow) []LiveRow { + var n: usize = 0; + var previous: ?@import("ztrace").store.Line = null; + for (lines, 0..) |line, i| { + const head = if (line) |l| previous == null or previous.?.file != l.file or previous.?.line != l.line else false; + if (head and previous != null) { + const before = previous.?; + const after = line.?; + if (before.file == after.file and after.line > before.line and after.line - before.line <= 8) { + var number = before.line + 1; + while (number < after.line and n < out.len) : (number += 1) { + out[n] = .{ .instruction = null, .source = .{ .file = after.file, .line = number } }; + n += 1; + } + } + } + if (n == out.len) break; + var again = false; + if (head) for (lines[0..i]) |old| { + if (old) |l| if (l.file == line.?.file and l.line == line.?.line) { + again = true; + break; + }; + }; + out[n] = .{ .instruction = i, .source = if (head) line else null, .again = again }; + n += 1; + previous = line; + } + return out[0..n]; +} + +test "live columns retain repeated runs and source-only gaps" { + const Line = @import("ztrace").store.Line; + const a: Line = .{ .file = 1, .line = 4 }; + const b: Line = .{ .file = 1, .line = 7 }; + var out: [16]LiveRow = undefined; + const got = livePlan(&.{ a, a, b, a, null }, &out); + try std.testing.expectEqual(@as(usize, 7), got.len); + try std.testing.expect(got[1].source == null); + try std.testing.expect(got[2].instruction == null and got[2].source.?.line == 5); + try std.testing.expect(got[5].again); + try std.testing.expect(got[6].source == null); +} + const size = theme.size.mono.context; const adv = ui.advance(size); const pitch = theme.metric.context.row; |
