diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-10 14:48:26 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-10 15:14:31 -0300 |
| commit | 7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb (patch) | |
| tree | a6add92cd4dcbbf4c31cd1ce989d99b190258353 /anim/src/source.zig | |
| parent | ed46c51fe127377eb1fde8dee83da984e0f61860 (diff) | |
| download | computeranim-7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb.tar.gz computeranim-7e9bf9e6e85c6c6e7a400d4fa0b43b77133ed3cb.zip | |
canim: animations of recordings, as a debugger's whole context with a lens beside it
A package of its own (anim/), on sketchbook's anim module and pardes's
syntax. A scene opens a recording, takes a cursor into it, and declares
blocks that read the moment the cursor is at: the state column (registers
each followed as a pointer and inked by where it points, the stack
telescope, the backtrace), the program column (the whole source, the
disassembly in address order, and the scene's lens: a node-link list over
a hexdump of its heap, array cells, a call tree, a syscall crossing), and
ties between them. Motion is a step told as a sentence (the line fires,
its effects cascade a level at a time, the mark moves on last), timed by
what the step contains; walk, hurry and sprint pace a loop. Five scenes
from real recordings of fib, a linked list, a sort and syscalls.
First drafts, by the agent's own handover: the frame of the design's
second revision, the hexdump lens and the ties; no disassembly
annotations, no gallery, scenes not yet reviewed against the mocks.
Diffstat (limited to 'anim/src/source.zig')
| -rw-r--r-- | anim/src/source.zig | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/anim/src/source.zig b/anim/src/source.zig new file mode 100644 index 0000000..6c10b50 --- /dev/null +++ b/anim/src/source.zig @@ -0,0 +1,193 @@ +//! The source panel: the program's text as pardes colours it, line numbers, +//! the current mark (gold: a band along the line and a plaque over its +//! number), and the three lines before it (the trail, gold cooling). The +//! mark fires on the line about to run, and moves on last, when what the +//! line did has been seen: a slide to a line near by, a jump (out, then +//! in) into a call or out of one. +//! Reads `Moment.line` (through the recording's index of it). +const std = @import("std"); +const sk = @import("sketch"); +const anim = @import("anim"); +const pardes = @import("pardes-syntax"); +const ui = @import("ui.zig"); +const cursors = @import("cursor.zig"); +const Cursor = cursors.Cursor; +const theme = ui.theme; +const Rect = ui.Rect; + +const gpa = std.heap.smp_allocator; + +pub const Source = struct { + cursor: Cursor, + box: Rect, + focus: bool = false, + + pub fn draw(p: *const Source, f: *sk.Frame, at: anim.Placed) void { + const u: ui.Ui = .{ .f = f, .at = at }; + const rec = p.cursor.rec; + const now = p.cursor.now(at); + const size = theme.size.mono.context; + const adv = ui.advance(size); + const text = prepared(rec.source, rec.file) orelse return; + const fit: u32 = @intFromFloat(@floor((p.box.h - m.tag - 2 * m.pad) / m.row)); + // The mark: leaving the line of one moment for that of the next. + const was = rec.own[now.marks[0]]; + const here = rec.own[now.marks[1]]; + const on_line = rec.own[now.marked()]; + // The window: where the line it is on has it, and (as the mark + // moves on) gliding to where the next one does. It never cuts. + const going = now.advancing(); + const windows = [2]f32{ @floatFromInt(rec.viewFirst(fit, was)), @floatFromInt(rec.viewFirst(fit, here)) }; + const first = std.math.lerp(windows[0], windows[1], cursors.glide(going)); + const from: u32 = @intFromFloat(@floor(first)); + const last: u32 = @min(from + fit, rec.line_count); + const body = u.panel(p.box.w, p.box.h, rec.file, .{ .focus = p.focus, .note = if (rec.line_count > fit) u.print("lines {d} to {d} of {d}", .{ from, @min(from + fit - 1, rec.line_count), rec.line_count }) else u.print("{d} lines", .{rec.line_count}) }); + const top = body.y + m.pad; + const gutter = 3 * adv + 22; + const code_x = gutter + 12; + const max_chars: usize = @intFromFloat(@floor((p.box.w - code_x - m.side) / adv)); + + // Where the mark is, and how much of it. + const current = theme.mark(.current); + const travel = now.plan.travel; + const moved = going * travel; + const scrolls = windows[0] != windows[1]; + var mark_line = here; + var mark_at: f32 = @floatFromInt(here); + var shows: f32 = 1; + if (going < 1) { + if (!cursors.jumps(rec, now.marks[0], now.marks[1])) { + // A line near by: it slides, off promptly and settling on + // the next. The step's follow-through. + mark_at = std.math.lerp(@as(f32, @floatFromInt(was)), mark_at, cursors.standard(going)); + if (@abs(mark_at - @as(f32, @floatFromInt(was))) < 0.5) mark_line = was; + } else { + // A call, a return, a line far off: a jump. Out there, then + // in here; it does not cross the lines between. + const half = @min(theme.time.current_jump, travel / 2); + if (moved < travel - half) { + mark_line = was; + mark_at = @floatFromInt(was); + shows = 1 - std.math.clamp(moved / half, 0, 1); + } else shows = std.math.clamp((moved - (travel - half)) / half, 0, 1); + } + } + const mark_y = top + (mark_at - first) * m.row; + u.clip(.{ .x = theme.stroke.rule, .y = body.y + 2, .w = p.box.w - 2 * theme.stroke.rule, .h = body.h - 4 }); + const marked = u.faded(shows); + if (shows > 0) marked.fill(.{ .x = theme.stroke.rule, .y = mark_y, .w = p.box.w - 2 * theme.stroke.rule, .h = m.row }, current.ember); + + // The trail: the last three other lines it was on. It shifts without animation. + var trail: [3]u32 = @splat(0); + var found: usize = 0; + var back = now.marked(); + while (back > 0 and found < trail.len) { + back -= 1; + const l = rec.own[back]; + if (l == 0 or l == on_line or std.mem.indexOfScalar(u32, trail[0..found], l) != null) continue; + trail[found] = l; + found += 1; + } + for (trail[0..found], 0..) |l, k| u.fill(.{ .x = theme.stroke.rule, .y = top + (@as(f32, @floatFromInt(l)) - first) * m.row + 2, .w = 4, .h = m.row - 4 }, theme.color.trail[k]); + + // The plaque over the number. Fire: on the line about to run its + // edges go out and come back, once, and nothing else moves. + const out = now.fires(.source); + if (shows > 0) marked.fill(.{ .x = theme.stroke.rule - out, .y = mark_y - out, .w = gutter - 2 + 2 * out, .h = m.row + 2 * out }, current.solid); + if (shows >= 1 and mark_y >= body.y and mark_y + m.row <= body.y + body.h) @import("ties.zig").anchor(u, .source, .{ .x = 0, .y = mark_y, .w = gutter, .h = m.row }); + + var line = @max(from, 1); + while (line <= last) : (line += 1) { + const y = top + (@as(f32, @floatFromInt(line)) - first) * m.row; + const row: Rect = .{ .x = 0, .y = y, .w = gutter - 10, .h = m.row }; + // The number under the plaque is the one in ink. + const on = shows >= 0.5 and line == mark_line and !(scrolls and going > 0 and going < 1 and line != here); + u.label(if (on) .mono_bold else .mono, size, row, .right, u.print("{d}", .{line}), if (on) current.on_solid else theme.color.text.faint); + const span = text.lines[line - 1]; + const bytes = text.bytes[span[0]..span[1]]; + const styles = text.styles[span[0]..span[1]]; + // Runs of one class, each set where its column is. + var i: usize = 0; + const end = @min(bytes.len, max_chars); + while (i < end) { + var j = i + 1; + while (j < end and styles[j] == styles[i]) j += 1; + const class: pardes.Syn = @enumFromInt(styles[i]); + const x = code_x + @as(f32, @floatFromInt(i)) * adv; + if (std.mem.indexOfNone(u8, bytes[i..j], " ") != null) u.text(face(class), size, x, theme.baseline(y, m.row, size), bytes[i..j], ink(class)); + i = j; + } + // A line longer than the panel is cut, and says so. + if (bytes.len > max_chars) u.text(.mono, size, code_x + @as(f32, @floatFromInt(max_chars)) * adv, theme.baseline(y, m.row, size), "\u{2026}", theme.color.text.faint); + } + u.unclip(); + } +}; + +const m = theme.metric.context; + +fn ink(class: pardes.Syn) u24 { + const c = theme.color.code; + return switch (class) { + .none => c.plain, + .keyword => c.keyword, + .string, .number => c.literal, + .comment => c.comment, + }; +} + +fn face(class: pardes.Syn) ui.Face { + return switch (class) { + .keyword => .mono_bold, + .comment => .mono_italic, + else => .mono, + }; +} + +/// A source ready to draw: a class for each byte, and where each line is. +const Text = struct { + bytes: []const u8, + styles: []const u8, + lines: []const [2]usize, +}; + +/// Made once for each source and kept: a scene draws the same few. +var texts: std.ArrayList(Text) = .empty; + +fn prepared(source: []const u8, file: []const u8) ?*const Text { + for (texts.items) |*t| if (t.bytes.ptr == source.ptr) return t; + texts.append(gpa, prepare(source, file) catch return null) catch return null; + return &texts.items[texts.items.len - 1]; +} + +fn prepare(source: []const u8, file: []const u8) !Text { + var analysis = try pardes.analyzeSource(gpa, file, source, false, true); + var lines: std.ArrayList([2]usize) = .empty; + var start: usize = 0; + while (start < source.len) { + const end = std.mem.indexOfScalarPos(u8, source, start, '\n') orelse source.len; + try lines.append(gpa, .{ start, end }); + start = end + 1; + } + // A language pardes has no grammar for is drawn plain. + if (analysis.colors.len != source.len) { + analysis.deinit(gpa); + const plain = try gpa.alloc(u8, source.len); + @memset(plain, 0); + analysis.colors = plain; + } + return .{ .bytes = source, .styles = analysis.colors, .lines = try lines.toOwnedSlice(gpa) }; +} + +test "a source is cut into lines and classed byte by byte" { + const source = "int main(void) {\n return 0; // done\n}\n"; + const text = try prepare(source, "main.c"); + defer gpa.free(text.lines); + defer gpa.free(text.styles); + try std.testing.expectEqual(@as(usize, 3), text.lines.len); + try std.testing.expectEqualStrings(" return 0; // done", source[text.lines[1][0]..text.lines[1][1]]); + const at = std.mem.indexOf(u8, source, "return").?; + try std.testing.expectEqual(pardes.Syn.keyword, @as(pardes.Syn, @enumFromInt(text.styles[at]))); + try std.testing.expectEqual(pardes.Syn.number, @as(pardes.Syn, @enumFromInt(text.styles[at + 7]))); + try std.testing.expectEqual(pardes.Syn.comment, @as(pardes.Syn, @enumFromInt(text.styles[at + 10]))); +} |
