diff options
| author | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-10-30 17:35:06 -0300 |
| commit | 6d8da8721141284cf33bb901168b26135893eb86 (patch) | |
| tree | 791d63401d4e85fbca1d597a8ce0326e70efd545 /src/Tables.zig | |
| parent | efd43a3a2cf03bb741366331158b368abf9ac583 (diff) | |
| parent | bf6810a8f9d6cc82050e7d9c0cfb06358301b574 (diff) | |
| download | pardes-6d8da8721141284cf33bb901168b26135893eb86.tar.gz pardes-6d8da8721141284cf33bb901168b26135893eb86.zip | |
Merge latest upstream into the macOS branch for Zig 0.17
Retain the macOS branch history and local Acme selection color while adopting upstream's current core and platform changes. Pin Zig and ZLS to 0.17.0, and fetch the existing zstbi compatibility commit through upstream after its original fork was removed.
Diffstat (limited to 'src/Tables.zig')
| -rw-r--r-- | src/Tables.zig | 1171 |
1 files changed, 1171 insertions, 0 deletions
diff --git a/src/Tables.zig b/src/Tables.zig new file mode 100644 index 00000000..2e65a716 --- /dev/null +++ b/src/Tables.zig @@ -0,0 +1,1171 @@ +//! A markdown file's pipe tables (GFM), laid out in their own rows. Every +//! byte of a table stays in its row as the text it is: B1, B2, B3 and the +//! chords work on it as on any text, and nothing is drawn over it. What +//! changes is the width some bytes take. A cell narrower than its column +//! is given the cells it lacks as virtual width, a *pad*, before one of its +//! bytes (the `|` that ends it, or for a right-aligned column the first +//! byte after the `|` that starts it), so every row's `|` of a column falls +//! on the same screen column whatever the source spacing. A pad is to the +//! byte it stands before what a tab's extra cells are to the tab: drawn as +//! blanks, every one of them maps back to that byte (a click there puts +//! the caret before it), and the caret before that byte is drawn after the +//! pad, against its glyph. +//! +//! The pads are the only mapping. File.zig's pane-aware column functions +//! (`displayOffset`, `displayEndOffset`, `byteAtRowDisplay`) and the row +//! walks that draw a body (`fillBody`, `recolorSyntax`) add them, so the +//! caret, every selection, a click and a Look map between a cell's x and +//! its byte as they map a tab's. The look itself (`paint`: the `|` as rules, +//! the delimiter row as one, the header bold) restyles cells that are +//! there; it moves nothing. +//! +//! Which lines are tables is found by lines, as tree-sitter-markdown's +//! scanner finds a `pipe_table` (its PIPE_TABLE_START: a header row's cells +//! counted by its pipes, a delimiter row of `:?-+:?` cells, the same count), +//! once per revision of the text, over the whole text: the highlighter +//! parses only the lines around the view, which start mid-table as soon as +//! a long one is scrolled, and fenced code is known only from the top. The +//! grammar is the oracle in a test ("tables are the pipe_tables tree-sitter +//! finds"). +const std = @import("std"); +const panes = @import("panes.zig"); +const pardes = @import("pardes.zig"); +const colors = @import("colors.zig"); +const modal = @import("modal.zig"); +const Surface = @import("surface.zig").Surface; +const Cell = @import("surface.zig").Cell; +const Color = @import("surface.zig").Color; +const Pardes = pardes.Pardes; +const Pane = panes.Pane; +const File = panes.File; + +const Tables = @This(); + +pub const Align = enum(u2) { left, right, center }; + +pub const Table = struct { + /// The header row's line; the delimiter row is the next. + first: u32, + /// One past the last row's line. + end: u32, + /// Where its columns' widths and alignments start in `widths`/`aligns`. + col0: u32, + /// Columns: the most cells any row ends with a `|`. + cols: u16, + /// The widest indentation before a leading `|`, in cells. + lead: u16, + /// The widest row laid out, in cells: a table wider than a wrapping + /// pane is shown as its source (`File.rowPads`). + width: u32, +}; + +/// `cells` of virtual width before the byte at `at` of its line. +pub const Pad = struct { at: u32, cells: u16 }; + +/// Columns past this many are not aligned (their pipes take no pad). +pub const max_cols = 64; +/// A centred column pads both sides of a cell, and a leading `|` its indent. +pub const max_pads = 2 * max_cols + 1; + +/// The text these tables are of: its revision, where it is, how long, and +/// how many lines. +revision: ?u32 = null, +content: usize = 0, +len: usize = 0, +lines: usize = 0, +list: std.ArrayList(Table) = .empty, +widths: std.ArrayList(u16) = .empty, +aligns: std.ArrayList(Align) = .empty, +/// The fenced code blocks, opener and closer lines (an unclosed one closes +/// past the end): no table is in one, and an edit to one of these lines +/// changes what every line after it is, so it finds them all again. +fences: std.ArrayList([2]u32) = .empty, +/// Columns in `widths` that no table points at any more (an edit's tables +/// are added at the end); past a bound, everything is found again. +garbage: usize = 0, +/// How many times the whole text was read (for the numbers and the tests). +scans: u32 = 0, +/// Where a reading of the whole text goes on from, until it reaches the +/// end: it is read a frame's budget at a time from the top (`frame_ns`), as +/// Places finds, so opening a big file never holds a frame; rows past it +/// are shown as their source until it gets there. +pending: ?Resume = null, + +/// A scan's place: the next line's byte and number, the fence it is in, +/// and whether the line before ended a block. +const Resume = struct { at: usize, n: u32, fence: ?Fence, prev_blank: bool }; + +/// What one frame spends reading a big text from the top. +pub const frame_ns: u64 = 4 * std.time.ns_per_ms; + +pub fn deinit(t: *Tables, gpa: std.mem.Allocator) void { + t.list.deinit(gpa); + t.widths.deinit(gpa); + t.aligns.deinit(gpa); + t.fences.deinit(gpa); + t.* = .{}; +} + +/// Whether these tables are of `f`'s text as it is now. +pub fn current(t: *const Tables, f: *const File.State) bool { + return t.revision == f.revision and t.content == @intFromPtr(f.content.ptr) and t.len == f.content.len; +} + +/// The table `line` is a row of, if any. +pub fn find(t: *const Tables, line: usize) ?*const Table { + const items = t.list.items; + var lo: usize = 0; + var hi: usize = items.len; + while (lo < hi) { + const mid = (lo + hi) / 2; + if (items[mid].end <= line) lo = mid + 1 else hi = mid; + } + if (lo < items.len and items[lo].first <= line) return &items[lo]; + return null; +} + +/// Find the tables of `f`'s text again unless they are of it already. Only +/// a markdown file has any; on no memory there are none (the text is shown +/// as it is). After one edit (File.State.splice) the tables it did not +/// touch shift and only the block around it is read again (`reflow`): a +/// keystroke costs its table, not the file. +pub fn ensure(t: *Tables, gpa: std.mem.Allocator, f: *File.State) void { + if (t.current(f)) { + if (t.pending != null) t.go(gpa, f.content, frame_ns); + return; + } + defer { + t.revision = f.revision; + t.content = @intFromPtr(f.content.ptr); + t.len = f.content.len; + } + if (f.output != null or f.mini != null or !isMarkdown(f.path)) { + t.list.clearRetainingCapacity(); + t.pending = null; + return; + } + const starts = File.lineIndex(gpa, f) catch { + t.list.clearRetainingCapacity(); + t.pending = null; + return; + }; + const one_edit = t.revision != null and t.revision.? +% 1 == f.revision and + f.splice.revision == f.revision and f.splice.old_len == t.len; + if (one_edit and t.garbage <= 4 * t.widths.items.len + 1024) { + if (t.pending) |r| { + // An edit past where the reading has got to changes nothing it + // read: it goes on. + if (lineHolding(starts, f.splice.head) >= r.n) { + t.lines = starts.len; + t.go(gpa, f.content, frame_ns); + return; + } + } else if (t.reflow(gpa, f)) |_| return else |_| {} + } + t.begin(starts.len); + t.go(gpa, f.content, frame_ns); +} + +/// Whether a reading of the text is still under way (Pardes.nextWake). +pub fn busy(t: *const Tables) bool { + return t.pending != null; +} + +/// Start reading the whole text again, from the top. +fn begin(t: *Tables, lines: usize) void { + t.list.clearRetainingCapacity(); + t.widths.clearRetainingCapacity(); + t.aligns.clearRetainingCapacity(); + t.fences.clearRetainingCapacity(); + t.garbage = 0; + t.scans +%= 1; + t.lines = lines; + t.pending = .{ .at = 0, .n = 0, .fence = null, .prev_blank = true }; +} + +/// Read on for about `budget_ns` (0: to the end). On no memory there are +/// no tables (the text is shown as it is). +fn go(t: *Tables, gpa: std.mem.Allocator, content: []const u8, budget_ns: u64) void { + var at = t.pending orelse return; + const done = t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, budget_ns) catch { + t.list.clearRetainingCapacity(); + t.pending = null; + return; + }; + t.pending = if (done) null else at; +} + +/// Line `n` of a text whose line index is `starts`. +fn lineOf(content: []const u8, starts: []const usize, n: usize) Line { + const start = starts[n]; + const end = if (n + 1 < starts.len) starts[n + 1] - 1 else content.len; + const text = content[start..end]; + return .{ .text = if (text.len > 0 and text[text.len - 1] == '\r') text[0 .. text.len - 1] else text, .start = start, .n = @intCast(n) }; +} + +/// The line byte `at` is on. +fn lineHolding(starts: []const usize, at: usize) usize { + var lo: usize = 0; + var hi: usize = starts.len; + while (lo < hi) { + const mid = (lo + hi) / 2; + if (starts[mid] <= at) lo = mid + 1 else hi = mid; + } + return lo -| 1; +} + +/// The tables after one edit: those it did not touch kept (shifted by the +/// lines it added), the block around it read again. error.Whole when the +/// edit could change what a line far from it is (a fence line in it), or +/// the block is unbounded. +fn reflow(t: *Tables, gpa: std.mem.Allocator, f: *File.State) !void { + const content = f.content; + const starts = try File.lineIndex(gpa, f); + const sp = f.splice; + const delta: i64 = @as(i64, @intCast(starts.len)) - @as(i64, @intCast(t.lines)); + // The edited lines: [a, b] now, [a, b_old] before it. + const a = lineHolding(starts, sp.head); + const b = lineHolding(starts, content.len - sp.tail); + const b_old_i = @as(i64, @intCast(b)) - delta; + if (b_old_i < @as(i64, @intCast(a))) return error.Whole; + const b_old: usize = @intCast(b_old_i); + for (a..b + 1) |n| if (fenceOf(lineOf(content, starts, n).text) != null) return error.Whole; + var inside = false; + for (t.fences.items) |fence| { + if ((fence[0] >= a and fence[0] <= b_old) or (fence[1] >= a and fence[1] <= b_old)) return error.Whole; + if (fence[0] < a and fence[1] > b_old) inside = true; + } + const shift = struct { + fn by(n: u32, d: i64) u32 { + return @intCast(@as(i64, n) + d); + } + }.by; + for (t.fences.items) |*fence| { + if (fence[0] > b_old) fence[0] = shift(fence[0], delta); + if (fence[1] > b_old and fence[1] != std.math.maxInt(u32)) fence[1] = shift(fence[1], delta); + } + // The old tables the edit touched or is next to: [lo_i, hi_i). + const items = t.list.items; + var lo_i: usize = 0; + while (lo_i < items.len and items[lo_i].end < a) lo_i += 1; + var hi_i = lo_i; + while (hi_i < items.len and items[hi_i].first <= b_old + 1) hi_i += 1; + for (items[hi_i..]) |*table| { + table.first = shift(table.first, delta); + table.end = shift(table.end, delta); + } + // Inside fenced code, no table changes. + if (inside) { + t.lines = starts.len; + return; + } + // The block: from the blank line before it to the one after, the old + // tables it touched taken in, never into a neighbouring table. + var r0 = a; + var r1 = b; + if (lo_i < hi_i) { + // Old lines: those past the edit moved by what it added. + const head: usize = items[lo_i].first; + const last: usize = items[hi_i - 1].end - 1; + r0 = @min(r0, if (head > b_old) @as(usize, shift(@intCast(head), delta)) else head); + r1 = @max(r1, if (last > b_old) @as(usize, shift(@intCast(last), delta)) else last); + } + const floor: usize = if (lo_i > 0) items[lo_i - 1].end else 0; + const ceiling: usize = if (hi_i < items.len) items[hi_i].first else starts.len; + var steps: usize = 0; + while (r0 > floor and !blank(lineOf(content, starts, r0 - 1).text)) : (steps += 1) { + if (steps > 512 or fenceOf(lineOf(content, starts, r0 - 1).text) != null) break; + r0 -= 1; + } + steps = 0; + while (r1 + 1 < ceiling and !blank(lineOf(content, starts, r1 + 1).text)) : (steps += 1) { + if (steps > 512) return error.Whole; + if (fenceOf(lineOf(content, starts, r1 + 1).text) != null) break; + r1 += 1; + } + for (items[lo_i..hi_i]) |table| t.garbage += table.cols; + // Its tables, found as the whole text's are, then put in the old ones' place. + const before = t.list.items.len; + const prev = if (r0 == 0) "" else lineOf(content, starts, r0 - 1).text; + const prev_blank = r0 == 0 or blank(prev) or closesAny(prev) or std.mem.startsWith(u8, prev[@min(indentOf(prev), prev.len)..], "#"); + const head = lineOf(content, starts, r0); + var at: Resume = .{ .at = head.start, .n = head.n, .fence = null, .prev_blank = prev_blank }; + _ = try t.scanFrom(gpa, content, &at, @intCast(r1), false, 0); + const fresh = try gpa.dupe(Table, t.list.items[before..]); + defer gpa.free(fresh); + t.list.shrinkRetainingCapacity(before); + try t.list.replaceRange(gpa, lo_i, hi_i - lo_i, fresh); + t.lines = starts.len; +} + +/// Whether a line closes some fence (any fence line does, to the block +/// after it: a header may follow it). +fn closesAny(text: []const u8) bool { + return fenceOf(text) != null; +} + +pub fn isMarkdown(path: []const u8) bool { + return std.ascii.endsWithIgnoreCase(path, ".md") or std.ascii.endsWithIgnoreCase(path, ".markdown"); +} + +/// One line of the text: its bytes and its number. +const Line = struct { text: []const u8, start: usize, n: u32 }; + +fn lineAt(content: []const u8, start: usize, n: u32) ?Line { + if (start > content.len) return null; + const end = std.mem.indexOfScalarPos(u8, content, start, '\n') orelse content.len; + const text = content[start..end]; + return .{ .text = if (text.len > 0 and text[text.len - 1] == '\r') text[0 .. text.len - 1] else text, .start = start, .n = n }; +} + +fn nextLine(content: []const u8, l: Line) ?Line { + const nl = std.mem.indexOfScalarPos(u8, content, l.start, '\n') orelse return null; + return lineAt(content, nl + 1, l.n + 1); +} + +fn indentOf(text: []const u8) usize { + var i: usize = 0; + while (i < text.len and (text[i] == ' ' or text[i] == '\t')) i += 1; + return i; +} + +fn blank(text: []const u8) bool { + return indentOf(text) == text.len; +} + +/// A fence's opener (```` ``` ```` or `~~~`, three or more, up to three +/// spaces in): its character and length. +const Fence = struct { ch: u8, len: usize }; + +fn fenceOf(text: []const u8) ?Fence { + const in = indentOf(text); + if (in > 3 or in >= text.len) return null; + const ch = text[in]; + if (ch != '`' and ch != '~') return null; + var n: usize = 0; + while (in + n < text.len and text[in + n] == ch) n += 1; + if (n < 3) return null; + // A backtick fence's info string has no backtick (CommonMark 4.5). + if (ch == '`' and std.mem.indexOfScalar(u8, text[in + n ..], '`') != null) return null; + return .{ .ch = ch, .len = n }; +} + +fn closes(text: []const u8, open: Fence) bool { + const f = fenceOf(text) orelse return false; + return f.ch == open.ch and f.len >= open.len and blank(text[indentOf(text) + f.len ..]); +} + +/// A line that ends a table's rows by starting a block of its own (what +/// the grammar's paragraph_interrupt_symbols are): a heading, a fence, a +/// quote, a list item, a thematic break, HTML. +fn interrupts(text: []const u8) bool { + const in = indentOf(text); + if (in > 3 or in >= text.len) return false; + const rest = text[in..]; + if (fenceOf(text) != null) return true; + switch (rest[0]) { + '#', '>', '<' => return true, + '-', '*', '+' => { + if (rest.len == 1 or rest[1] == ' ' or rest[1] == '\t') return true; + // A thematic break: three or more of one of -*_ and spaces. + var n: usize = 0; + for (rest) |b| { + if (b == rest[0]) n += 1 else if (b != ' ' and b != '\t') return false; + } + return n >= 3; + }, + '0'...'9' => { + var i: usize = 0; + while (i < rest.len and i < 9 and std.ascii.isDigit(rest[i])) i += 1; + return i < rest.len and (rest[i] == '.' or rest[i] == ')') and (i + 1 == rest.len or rest[i + 1] == ' '); + }, + else => return false, + } +} + +/// A row's unescaped pipes, in order, and whether the row starts with one +/// (after its indentation) and ends with one (before trailing blanks). +const Pipes = struct { + at: [max_cols + 2]u32 = undefined, + n: usize = 0, + /// Pipes past `at`'s room: counted, not kept. + more: usize = 0, + leading: bool = false, + trailing: bool = false, + + fn of(text: []const u8) Pipes { + var p: Pipes = .{}; + const in = indentOf(text); + p.leading = in < text.len and text[in] == '|'; + var last_ink: usize = text.len; + var i: usize = 0; + while (i < text.len) : (i += 1) { + const b = text[i]; + if (b == '\\' and i + 1 < text.len and std.ascii.isPrint(text[i + 1]) and !std.ascii.isAlphanumeric(text[i + 1]) and text[i + 1] != ' ') { + last_ink = i + 1; + i += 1; + continue; + } + if (b == '|') { + if (p.n < p.at.len) { + p.at[p.n] = @intCast(i); + p.n += 1; + } else p.more += 1; + } + if (b != ' ' and b != '\t') last_ink = i; + } + p.trailing = p.n > 0 and last_ink == p.at[p.n - 1] and p.more == 0; + return p; + } + + /// The cells the grammar counts: the pipes after a leading one, plus + /// the last cell when no pipe ends the row. + fn cells(p: *const Pipes) usize { + const pipes = p.n + p.more - @intFromBool(p.leading); + return pipes + @intFromBool(!p.trailing); + } + + /// The pipe that ends cell `k` (the pipes after a leading one). + fn end(p: *const Pipes, k: usize) ?usize { + const i = k + @intFromBool(p.leading); + return if (i < p.n) p.at[i] else null; + } + + /// Where cell `k` starts: after the pipe before it, or after the + /// indentation when it is the first and no pipe leads. + fn start(p: *const Pipes, k: usize) usize { + if (k == 0 and !p.leading) return 0; + return p.at[k - @intFromBool(!p.leading)] + 1; + } +}; + +/// A delimiter row's cells (`:?-+:?` between pipes), or null if `text` is +/// none: as the scanner reads one after a header. +fn delimiterCells(text: []const u8, out: *[max_cols]Align) ?usize { + var i = indentOf(text); + if (i > 3) return null; + var cells: usize = 0; + var pipes: usize = 0; + if (i < text.len and text[i] == '|') { + i += 1; + pipes += 1; + } + while (true) { + while (i < text.len and (text[i] == ' ' or text[i] == '\t')) i += 1; + if (i >= text.len) break; + if (text[i] == '|') { + // An empty cell (`||`): the scanner counts it, aligned as none. + if (cells < max_cols) out[cells] = .left; + cells += 1; + pipes += 1; + i += 1; + continue; + } + var left = false; + if (text[i] == ':') { + left = true; + i += 1; + if (i >= text.len or text[i] != '-') return null; + } + var dashes: usize = 0; + while (i < text.len and text[i] == '-') : (i += 1) dashes += 1; + if (dashes == 0) return null; + var right = false; + if (i < text.len and text[i] == ':') { + right = true; + i += 1; + } + if (cells < max_cols) out[cells] = if (left and right) .center else if (right) .right else .left; + cells += 1; + while (i < text.len and (text[i] == ' ' or text[i] == '\t')) i += 1; + if (i >= text.len) break; + if (text[i] != '|') return null; + pipes += 1; + i += 1; + } + if (pipes == 0) return null; + return cells; +} + +/// The display width of a cell's bytes; ASCII is a cell a byte. +fn widthOf(text: []const u8) usize { + for (text) |b| if (b < 0x20 or b >= 0x7f) return File.displayWidth(text); + return text.len; +} + +/// Find the tables of `content`, in place of the ones there were. +pub fn scan(t: *Tables, gpa: std.mem.Allocator, content: []const u8) !void { + t.list.clearRetainingCapacity(); + t.widths.clearRetainingCapacity(); + t.aligns.clearRetainingCapacity(); + t.begin(std.mem.count(u8, content, "\n") + 1); + var at = t.pending.?; + _ = try t.scanFrom(gpa, content, &at, std.math.maxInt(u32), true, 0); + t.pending = null; +} + +/// Find the tables whose header is on a line from `at` to `stop`, +/// appending them; with `fences`, keep the fences. With a budget, stop +/// when it is spent, `at` where to go on from. Whether it reached the end +/// (or `stop`). +fn scanFrom(t: *Tables, gpa: std.mem.Allocator, content: []const u8, at: *Resume, stop: u32, fences: bool, budget_ns: u64) !bool { + var fence = at.fence; + var prev_blank = at.prev_blank; + var line = lineAt(content, at.at, at.n); + const t0 = if (budget_ns > 0) panes.Places.nowNs() else 0; + var lines: usize = 0; + while (line) |l| : (lines += 1) { + if (l.n > stop) break; + if (budget_ns > 0 and lines % 256 == 255 and panes.Places.nowNs() -| t0 > budget_ns) { + at.* = .{ .at = l.start, .n = l.n, .fence = fence, .prev_blank = prev_blank }; + return false; + } + const following = nextLine(content, l); + if (fence) |open| { + if (closes(l.text, open)) { + fence = null; + if (fences) t.fences.items[t.fences.items.len - 1][1] = l.n; + } + prev_blank = fence == null; + line = following; + continue; + } + if (fenceOf(l.text)) |open| { + fence = open; + if (fences) try t.fences.append(gpa, .{ l.n, std.math.maxInt(u32) }); + prev_blank = false; + line = following; + continue; + } + const is_blank = blank(l.text); + var aligns: [max_cols]Align = undefined; + // A header starts a block: after a blank line, a heading or a fence + // (the grammar tries PIPE_TABLE_START where a paragraph could start). + if (!is_blank and prev_blank and indentOf(l.text) <= 3 and std.mem.indexOfScalar(u8, l.text, '|') != null) { + if (following) |delimiter| if (delimiterCells(delimiter.text, &aligns)) |count| { + const header = Pipes.of(l.text); + // A header's one pipe may not be its leading one alone, + // unless one ends it too (`| a |`, not `| a`). + const lone = header.n + header.more == @intFromBool(header.leading) and !(header.leading and header.trailing); + if (!lone and header.cells() == count) { + const last = try t.add(gpa, content, l, delimiter, aligns[0..@min(count, max_cols)]); + line = nextLine(content, last); + prev_blank = false; + continue; + } + }; + } + // A heading line ends a block too: a header may follow it. + prev_blank = is_blank or std.mem.startsWith(u8, l.text[@min(indentOf(l.text), l.text.len)..], "#"); + line = following; + } + return true; +} + +/// Take the table whose header is `header`: its rows, its columns' widths +/// and its width. Returns its last row's line. +fn add(t: *Tables, gpa: std.mem.Allocator, content: []const u8, header: Line, delimiter: Line, aligns: []const Align) !Line { + var last = delimiter; + while (nextLine(content, last)) |row| { + if (blank(row.text) or interrupts(row.text)) break; + last = row; + } + const col0: u32 = @intCast(t.widths.items.len); + var table: Table = .{ .first = header.n, .end = last.n + 1, .col0 = col0, .cols = 0, .lead = 0, .width = 0 }; + // Widths: each column's widest cell, the cell's bytes up to the pipe + // that ends it; a row's last cell with no pipe after it aligns nothing. + var row: ?Line = header; + while (row) |r| : (row = if (r.n == last.n) null else nextLine(content, r)) { + const pipes = Pipes.of(r.text); + if (pipes.leading) table.lead = @max(table.lead, @as(u16, @intCast(@min(widthOf(r.text[0..pipes.at[0]]), std.math.maxInt(u16))))); + var k: usize = 0; + while (k < max_cols) : (k += 1) { + const stop = pipes.end(k) orelse break; + const w: u16 = @intCast(@min(widthOf(r.text[pipes.start(k)..stop]), std.math.maxInt(u16))); + if (k >= table.cols) { + try t.widths.append(gpa, w); + try t.aligns.append(gpa, if (k < aligns.len) aligns[k] else .left); + table.cols += 1; + } else t.widths.items[col0 + k] = @max(t.widths.items[col0 + k], w); + } + } + // The width: each row laid out, its source width and its pads. + row = header; + var buf: [max_pads]Pad = undefined; + while (row) |r| : (row = if (r.n == last.n) null else nextLine(content, r)) { + var width = widthOf(r.text); + for (t.padsOf(&table, r.n, r.text, &buf)) |pad| width += pad.cells; + table.width = @max(table.width, @as(u32, @intCast(@min(width, std.math.maxInt(u32))))); + } + try t.list.append(gpa, table); + return last; +} + +/// The pads of one of `table`'s rows, by byte, in order. No pad is ever at +/// byte 0: a row is drawn from its first byte, so there is no cell before +/// it to give. +pub fn padsOf(t: *const Tables, table: *const Table, line: usize, text: []const u8, out: *[max_pads]Pad) []Pad { + const pipes = Pipes.of(text); + var n: usize = 0; + if (pipes.leading and pipes.at[0] > 0) { + const w = widthOf(text[0..pipes.at[0]]); + if (w < table.lead) { + out[n] = .{ .at = pipes.at[0], .cells = @intCast(table.lead - w) }; + n += 1; + } + } + var k: usize = 0; + while (k < table.cols) : (k += 1) { + const stop = pipes.end(k) orelse break; + const from = pipes.start(k); + const w = widthOf(text[from..stop]); + const want = t.widths.items[table.col0 + k]; + if (w >= want) continue; + const missing: u16 = @intCast(want - w); + // The first cell with no leading pipe starts at byte 0: it pads at + // its end, whatever its alignment. + const how: Align = if (from == 0) .left else t.aligns.items[table.col0 + k]; + // The delimiter row's cells keep their colons at their edges: the + // rule is drawn long between them (a pad before a closing `:`). + if (line == table.first + 1) { + var last = stop; + while (last > from and (text[last - 1] == ' ' or text[last - 1] == '\t')) last -= 1; + const at = if (last > from and text[last - 1] == ':') last - 1 else stop; + if (at == 0) continue; + out[n] = .{ .at = @intCast(at), .cells = missing }; + n += 1; + continue; + } + const before: u16 = switch (how) { + .left => 0, + .right => missing, + .center => missing / 2, + }; + // An empty cell's start is its pipe: one pad there. + if (before > 0 and from < stop) { + out[n] = .{ .at = @intCast(from), .cells = before }; + n += 1; + } + const after = if (from < stop) missing - before else missing; + if (after > 0) { + out[n] = .{ .at = @intCast(stop), .cells = after }; + n += 1; + } + } + return out[0..n]; +} + +/// The cells of virtual width between byte `from` (excluded: its pad is +/// before the row's first shown byte) and byte `to` (included: its pad is +/// before its glyph), negative when `to` is before `from`. +pub fn between(pads: []const Pad, from: usize, to: usize) i32 { + const lo = @min(from, to); + const hi = @max(from, to); + var sum: i32 = 0; + for (pads) |pad| { + if (pad.at > hi) break; + if (pad.at > lo) sum += pad.cells; + } + return if (to >= from) sum else -sum; +} + +/// The byte at display column `col` of a row shown from byte `from`, its +/// pads counted: a pad's cells are its byte's, as a tab's are the tab's. +/// Past the end, a virtual column a cell (File.rawAtDisplay). +pub fn byteAt(text: []const u8, pads: []const Pad, from_raw: usize, col: usize) usize { + if (from_raw >= text.len) return from_raw +| col; + var at = modal.graphemeStart(text, from_raw); + var k: usize = 0; + while (k < pads.len and pads[k].at <= at) k += 1; + var used: usize = 0; + while (at < text.len) { + if (k < pads.len and pads[k].at == at) { + used += pads[k].cells; + k += 1; + if (col < used) return at; + } + const end = modal.nextGrapheme(text, at); + used += File.graphemeDisplayWidth(text[at..end]); + if (col < used) return at; + at = end; + } + return text.len + (col - used); +} + +/// What a table's `|` and its delimiter row are drawn as: light box-drawing +/// rules, each one cell, as the glyph of the byte that is there. +const rule_v = "\u{2502}"; +const rule_h = "\u{2500}"; +const rule_cross = "\u{253c}"; +const rule_left = "\u{251c}"; +const rule_right = "\u{2524}"; + +fn glyph(cell: *Cell, g: []const u8) void { + cell.default = false; + @memcpy(cell.text[0..g.len], g); + cell.len = @intCast(g.len); +} + +/// A pane's table rows drawn as tables: every unescaped `|` a rule, the +/// delimiter row one rule across (its dashes, its blanks and its pads, its +/// `:` kept in the rule's colour, since they say the alignment), the header +/// row bold on the tag's ground, as compact context rows are. Only glyphs +/// and colours change, on the cells the row has: where each byte is and how +/// wide is fillBody's, so nothing is drawn over anything, B1 on a rule is +/// B1 on its `|`, and a selection's tint lies over the rules as over text. +pub fn paint(p: *Pardes, s: *Surface, pane: *Pane, r: pardes.Rect, tx: u16, tw: u16, body_h: u16) void { + const f = if (pane.file) |*file| file else return; + if (f.tables.list.items.len == 0) return; + const th = p.theme(); + const tint = p.settings.colors; + const rule_fg: Color = .{ .rgb = th.border orelse th.lineno }; + const head_bg: Color = .{ .rgb = th.tag_bg }; + const prefix: usize = File.gutterWidth(pane); + const body_y = p.bodyTop(pane, r); + var vr: u16 = 0; + while (vr < body_h) : (vr += 1) { + const w = pane.wrapAt(vr); + const table = File.rowTable(pane, w.line) orelse continue; + const text = File.sourceLine(pane, w.line); + var buf: [max_pads]Pad = undefined; + const pads = f.tables.padsOf(table, @intCast(w.line), text, &buf); + const pipes = Pipes.of(text); + const delimiter = w.line == table.first + 1; + const header = w.line == table.first; + const y = body_y + vr; + // The row's walk, as fillBody lays it out from its first shown byte. + var at = modal.graphemeStart(text, @min(@as(usize, @intCast(@max(0, w.at))), text.len)); + var col: usize = prefix + @as(usize, @intCast(w.pad)); + var k: usize = 0; + while (k < pads.len and pads[k].at <= at) k += 1; + var q: usize = 0; + while (q < pipes.n and pipes.at[q] < at) q += 1; + while (at < text.len and col < tw) { + if (k < pads.len and pads[k].at == at) { + for (0..pads[k].cells) |i| { + if (col + i >= tw) break; + const cell = s.at(tx + @as(u16, @intCast(col + i)), y); + if (delimiter) { + glyph(cell, rule_h); + if (tint) cell.style.fg = rule_fg; + } + if (header and tint) cell.style.bg = head_bg; + } + col += pads[k].cells; + k += 1; + if (col >= tw) break; + } + const end = modal.nextGrapheme(text, at); + const cells = File.graphemeDisplayWidth(text[at..end]); + const cell = s.at(tx + @as(u16, @intCast(col)), y); + const pipe = q < pipes.n and pipes.at[q] == at; + if (pipe) { + q += 1; + glyph(cell, if (!delimiter) + rule_v + else if (q == 1 and pipes.leading) + rule_left + else if (q == pipes.n and pipes.trailing) + rule_right + else + rule_cross); + if (tint) cell.style.fg = rule_fg; + cell.style.bold = false; + } else if (delimiter) { + if (text[at] == '-' or text[at] == ' ') glyph(cell, rule_h); + if (tint) cell.style.fg = rule_fg; + } else if (header) { + cell.default = false; + cell.style.bold = true; + } + if (header and tint) for (0..cells) |i| { + if (col + i < tw) s.at(tx + @as(u16, @intCast(col + i)), y).style.bg = head_bg; + }; + col += cells; + at = end; + } + } +} + +// ---- tests ---- + +const testing = std.testing; +const config = @import("config.zig"); +const Key = pardes.Key; + +const sample = + "# Fruit\n" ++ + "\n" ++ + "| name | qty | note |\n" ++ + "|:-----|----:|:----:|\n" ++ + "| apple | 3 | red |\n" ++ + "| kiwi | 12 | small and fuzzy |\n" ++ + "| fig || |\n" ++ + "\n" ++ + "after\n"; + +/// A pane on `content` as a markdown file, drawn once. +fn testPane(p: *Pardes, content: []const u8, frame: std.mem.Allocator) !*Pane { + const pane = try p.setTestFile(content); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, "/notes.md"); + _ = try p.render(frame); + return pane; +} + +test "tables: a pipe table's columns, alignments and pads, by line" { + var t: Tables = .{}; + defer t.deinit(testing.allocator); + try t.scan(testing.allocator, sample); + try testing.expectEqual(@as(usize, 1), t.list.items.len); + const table = t.list.items[0]; + try testing.expectEqual(@as(u32, 2), table.first); + try testing.expectEqual(@as(u32, 7), table.end); + try testing.expectEqualSlices(u16, &.{ 7, 5, 17 }, t.widths.items); + try testing.expectEqualSlices(Align, &.{ .left, .right, .center }, t.aligns.items); + // `| fig || |`: the first cell short by 2 (left: before its `|`), the + // empty second by 5 (one pad at its pipe), the third by 16 (centred). + var buf: [max_pads]Pad = undefined; + const fig = "| fig || |"; + try testing.expectEqualSlices(Pad, &.{ + .{ .at = 6, .cells = 2 }, .{ .at = 7, .cells = 5 }, .{ .at = 8, .cells = 8 }, .{ .at = 9, .cells = 8 }, + }, t.padsOf(&table, 6, fig, &buf)); + // `| apple | 3 | red |`: right-aligned 3 pads before its first byte. + try testing.expectEqualSlices(Pad, &.{ + .{ .at = 9, .cells = 2 }, .{ .at = 13, .cells = 6 }, .{ .at = 18, .cells = 6 }, + }, t.padsOf(&table, 4, "| apple | 3 | red |", &buf)); + // Every row laid out is one width: the table's. + try testing.expectEqual(@as(u32, 1 + 7 + 1 + 5 + 1 + 17 + 1), table.width); + // Not tables: a fence's rows, counts that differ, a delimiter alone. + for ([_][]const u8{ + "```\n| a | b |\n|---|---|\n```\n", + "| a | b |\n|---|\n", + "|---|---|\n", + "| a\n|---\n", + }) |text| { + try t.scan(testing.allocator, text); + try testing.expectEqual(@as(usize, 0), t.list.items.len); + } +} + +test "tables: every byte's cell maps back to it, and every pad cell to the byte after it" { + var t: Tables = .{}; + defer t.deinit(testing.allocator); + try t.scan(testing.allocator, sample); + const table = &t.list.items[0]; + var lines = std.mem.splitScalar(u8, sample, '\n'); + var n: usize = 0; + while (lines.next()) |text| : (n += 1) { + if (n < table.first or n >= table.end) continue; + var buf: [max_pads]Pad = undefined; + const pads = t.padsOf(table, n, text, &buf); + var col: usize = 0; + for (text, 0..) |_, b| { + // The pad before b: each of its cells is b's. + const start = File.displayWidth(text[0..b]) + @as(usize, @intCast(between(pads, 0, b))); + while (col < start) : (col += 1) try testing.expectEqual(b, byteAt(text, pads, 0, col)); + try testing.expectEqual(b, byteAt(text, pads, 0, col)); + col += 1; + } + // Past the end, a virtual column a cell. + try testing.expectEqual(text.len + 2, byteAt(text, pads, 0, col + 2)); + // From a scrolled start, the pads before it are not counted. + try testing.expectEqual(@as(usize, 2), byteAt(text, pads, 2, 0)); + } +} + +test "tables: a markdown table is drawn aligned, its bytes in order, the caret and a click on each byte's cell" { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try testPane(p, sample, frame.allocator()); + const r = p.rects[0]; + const text_x = r.x + config.GUTTER + File.gutterWidth(pane); + const body_y = p.bodyTop(pane, r); + // The k-th pipe of every row on one screen column. + var pipe_x: [4]?u16 = @splat(null); + for (2..7) |line| { + const text = File.sourceLine(pane, @intCast(line)); + var k: usize = 0; + for (text, 0..) |b, i| { + const x = text_x + @as(u16, @intCast(File.displayOffset(pane, @intCast(line), 0, @intCast(i)))); + const cell = p.surface.at(x, body_y + @as(u16, @intCast(line))); + if (b == '|') { + if (pipe_x[k]) |want| try testing.expectEqual(want, x) else pipe_x[k] = x; + try testing.expect(!std.mem.eql(u8, cell.grapheme(), " ")); + k += 1; + } else if (line != 3) try testing.expectEqualStrings(text[i .. i + 1], cell.grapheme()); + } + } + try testing.expectEqual(@as(u16, 1 + 7 + 1 + 5 + 1 + 17), pipe_x[3].? - text_x); + // The look: each `|` a rule; the delimiter row one rule across, its + // `:` kept; the header bold. The bytes under them are the source's. + const rule_y = body_y + 3; + var x = text_x; + while (x <= pipe_x[3].?) : (x += 1) { + const g = p.surface.at(x, rule_y).grapheme(); + try testing.expect(std.mem.eql(u8, g, rule_h) or std.mem.eql(u8, g, rule_cross) or + std.mem.eql(u8, g, rule_left) or std.mem.eql(u8, g, rule_right) or std.mem.eql(u8, g, ":")); + } + try testing.expectEqualStrings(rule_left, p.surface.at(pipe_x[0].?, rule_y).grapheme()); + try testing.expectEqualStrings(rule_cross, p.surface.at(pipe_x[1].?, rule_y).grapheme()); + try testing.expectEqualStrings(rule_right, p.surface.at(pipe_x[3].?, rule_y).grapheme()); + try testing.expectEqualStrings(rule_v, p.surface.at(pipe_x[2].?, body_y + 4).grapheme()); + try testing.expect(p.surface.at(text_x + 2, body_y + 2).style.bold); + try testing.expect(!p.surface.at(text_x + 2, body_y + 4).style.bold); + try testing.expectEqualStrings("| name | qty | note |", File.sourceLine(pane, 2)); + // B1 on any cell of `| fig || |`: the caret on the byte the cell is + // (a pad's cells are the byte after them), drawn where that byte is. + const fig: i32 = 6; + const text = File.sourceLine(pane, fig); + const y = body_y + 6; + var col: u16 = 0; + while (col < pipe_x[3].? - text_x + 1) : (col += 1) { + p.update(.{ .mouse = .{ .button = config.select_button, .kind = .press, .col = text_x + col, .row = y } }); + p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = text_x + col, .row = y } }); + const want = File.byteAtRowDisplay(pane, fig, 0, col); + try testing.expectEqual(fig, pane.body.cur_row); + try testing.expectEqual(want, pane.body.cur_col); + try testing.expect(want < text.len); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + const caret = p.surface.cursor orelse return error.NoCaret; + try testing.expectEqual(text_x + @as(u16, @intCast(File.displayOffset(pane, fig, 0, want))), caret.x); + // The caret is on the cell clicked, or on the glyph after its pad. + try testing.expect(caret.x >= text_x + col); + } + // A pad cell: the empty second cell's five, all the `|` after them. + const empty_x = pipe_x[1].? - text_x + 1; + for (0..5) |i| try testing.expectEqual(@as(i32, 7), File.byteAtRowDisplay(pane, fig, 0, @intCast(empty_x + i))); +} + +test "tables: typing in a cell re-aligns the table live, and j/k keep the column shown" { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try testPane(p, sample, frame.allocator()); + // On `3` in the apple row: insert before it. + pane.body.cur_row = 4; + pane.body.cur_col = 10; + p.update(.{ .key = .{ .cp = 'i', .text = "i" } }); + for ("123456789") |c| p.update(.{ .key = .{ .cp = c, .text = &.{c} } }); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try testing.expectEqualStrings("| apple | 1234567893 | red |", File.sourceLine(pane, 4)); + // The qty column is now 12 wide in every row. + for (2..7) |line| { + const text = File.sourceLine(pane, @intCast(line)); + var k: usize = 0; + for (text, 0..) |b, i| { + if (b != '|') continue; + k += 1; + if (k == 3) try testing.expectEqual(@as(i32, 1 + 7 + 1 + 12), File.displayOffset(pane, @intCast(line), 0, @intCast(i))); + } + } + // k from the kiwi row's `and` keeps its screen column: the apple + // row's centred `red` under it. + p.update(.{ .key = .{ .cp = Key.escape } }); + pane.body.cur_row = 5; + pane.body.cur_col = @intCast(std.mem.indexOf(u8, File.sourceLine(pane, 5), "and").?); + pane.body.vsel.active = false; + pane.body.sticky_col = -1; + const shown = File.displayOffset(pane, 5, 0, pane.body.cur_col); + p.update(.{ .key = .{ .cp = 'k', .text = "k" } }); + try testing.expectEqual(@as(i32, 4), pane.body.cur_row); + try testing.expectEqual(shown, File.displayOffset(pane, 4, 0, pane.body.cur_col)); +} + +test "tables: an edit's tables, shifted and found again around it, are the whole text's" { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try testPane(p, sample ++ "\n```\n| a | b |\n|---|---|\n```\n\n" ++ sample ++ "para\n| x | y |\n|---|--|\n| 1 | 2 |\n", frame.allocator()); + const f = &pane.file.?; + var whole: Tables = .{}; + defer whole.deinit(testing.allocator); + var prng: std.Random.DefaultPrng = .init(11); + const random = prng.random(); + const alphabet = "|||--: \n\nab`"; + var reflowed: usize = 0; + for (0..1500) |round| { + const old = f.content; + const at = random.uintAtMost(usize, old.len); + const cut = if (random.boolean()) @min(old.len - at, random.uintAtMost(usize, 3)) else 0; + const grow = random.uintAtMost(usize, 2); + const new = try p.gpa.alloc(u8, old.len - cut + grow); + @memcpy(new[0..at], old[0..at]); + for (new[at..][0..grow]) |*c| c.* = alphabet[random.uintLessThan(usize, alphabet.len)]; + @memcpy(new[at + grow ..], old[at + cut ..]); + const scans = f.tables.scans; + File.setContent(p, f, new); + f.tables.ensure(p.gpa, f); + if (f.tables.scans == scans) reflowed += 1; + try whole.scan(testing.allocator, f.content); + testing.expectEqual(whole.list.items.len, f.tables.list.items.len) catch |e| { + std.debug.print("round {d}\n{s}\n", .{ round, f.content }); + return e; + }; + for (whole.list.items, f.tables.list.items) |w, mine| { + try testing.expectEqual(w.first, mine.first); + try testing.expectEqual(w.end, mine.end); + try testing.expectEqual(w.cols, mine.cols); + try testing.expectEqual(w.lead, mine.lead); + try testing.expectEqual(w.width, mine.width); + try testing.expectEqualSlices(u16, whole.widths.items[w.col0..][0..w.cols], f.tables.widths.items[mine.col0..][0..mine.cols]); + try testing.expectEqualSlices(Align, whole.aligns.items[w.col0..][0..w.cols], f.tables.aligns.items[mine.col0..][0..mine.cols]); + } + try testing.expectEqual(whole.lines, f.tables.lines); + } + // Most edits took the short way. + try testing.expect(reflowed > 1000); +} + +test "tables: a big text's tables are read a frame's budget at a time from the top, edits on the way kept" { + var big: std.ArrayList(u8) = .empty; + defer big.deinit(testing.allocator); + for (0..1500) |i| try big.print(testing.allocator, "## {d}\n\n| a | b {d} |\n|---|--:|\n| one | two |\n| three | {d} |\n\n", .{ i, i, i * 7 }); + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 80, .rows = 24 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try testPane(p, big.items, frame.allocator()); + const f = &pane.file.?; + // One frame did not read it all, and asks for the next. + try testing.expect(f.tables.busy()); + try testing.expect(p.nextWake() != null); + // The top's tables are found already: the view is right at once. + try testing.expect(File.rowTable(pane, 2) != null); + const reached = f.tables.pending.?.n; + // An edit past where it got to: it goes on; one before it: from the top. + var edits: usize = 0; + var frames: usize = 0; + while (f.tables.busy()) : (frames += 1) { + if (edits < 2) { + const at = if (edits == 0) f.content.len - 3 else 10; + const new = try std.mem.concat(p.gpa, u8, &.{ f.content[0..at], "x", f.content[at..] }); + File.setContent(p, f, new); + edits += 1; + } + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try testing.expect(frames < 10_000); + } + try testing.expect(reached < File.nlines(p.gpa, f)); + var whole: Tables = .{}; + defer whole.deinit(testing.allocator); + try whole.scan(testing.allocator, f.content); + try testing.expectEqual(whole.list.items.len, f.tables.list.items.len); + for (whole.list.items, f.tables.list.items) |w, mine| { + try testing.expectEqual(w.first, mine.first); + try testing.expectEqual(w.end, mine.end); + try testing.expectEqual(w.width, mine.width); + } +} + +test "tables: a table wider than a wrapping pane is shown as its source, laid out again with Wrap off" { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 34, .rows = 24 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try testPane(p, sample, frame.allocator()); + try testing.expect(pane.body_wrapped); + try testing.expect(File.wrapWidth(pane, true) < 34); + try testing.expect(File.rowTable(pane, 2) == null); + // Source: a pipe is a pipe, at its source column. + try testing.expectEqual(@as(i32, 7), File.displayOffset(pane, 5, 0, 7)); + try testing.expect(p.executeBuiltinLine(0, "Wrap off")); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + try testing.expect(!pane.body_wrapped); + try testing.expect(File.rowTable(pane, 2) != null); +} + +test "zz tables timings on a big markdown file (TABLES_TIMES=<path>)" { + const path_z = std.c.getenv("TABLES_TIMES") orelse return error.SkipZigTest; + const path = std.mem.span(path_z); + const io = testing.io; + const bytes = try std.Io.Dir.cwd().readFileAlloc(io, path, testing.allocator, .limited(16 << 20)); + defer testing.allocator.free(bytes); + const now = panes.Places.nowNs; + var report: std.ArrayList(u8) = .empty; + defer report.deinit(testing.allocator); + { + var t: Tables = .{}; + defer t.deinit(testing.allocator); + var worst: u64 = 0; + var sum: u64 = 0; + for (0..5) |_| { + const t0 = now(); + try t.scan(testing.allocator, bytes); + const d = now() - t0; + sum += d; + worst = @max(worst, d); + } + try report.print(testing.allocator, "scan of {d} bytes: {d} tables, mean {d} us, worst {d} us\n", .{ bytes.len, t.list.items.len, sum / 5 / 1000, worst / 1000 }); + } + // The same text as markdown and as plain text: what the tables cost a + // frame and an edit. + for ([_][]const u8{ "/stress.md", "/stress.txt" }) |name| { + const p = try Pardes.init(testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + var frame = std.heap.ArenaAllocator.init(testing.allocator); + defer frame.deinit(); + const pane = try p.setTestFile(bytes); + p.gpa.free(pane.file.?.path); + pane.file.?.path = try p.gpa.dupe(u8, name); + { + var t0 = now(); + _ = try p.render(frame.allocator()); + const first = now() - t0; + var frames: usize = 1; + var worst: u64 = first; + t0 = now(); + while (pane.file.?.tables.busy()) : (frames += 1) { + const t1 = now(); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + worst = @max(worst, now() - t1); + } + try report.print(testing.allocator, "{s}: the first frame {d} us; the tables read in {d} frames, the worst {d} us\n", .{ name, first / 1000, frames, worst / 1000 }); + } + // Into the middle of a table half way down. + var line: usize = File.nlines(p.gpa, &pane.file.?) / 2; + while (!std.mem.startsWith(u8, File.sourceLine(pane, @intCast(line)), "| ") or + std.mem.indexOf(u8, File.sourceLine(pane, @intCast(line)), "col") != null) line += 1; + pane.body.cur_row = @intCast(line + 2); + pane.body.cur_col = 2; + pane.ensureCursorVisible(); + _ = frame.reset(.retain_capacity); + _ = try p.render(frame.allocator()); + const Clock = struct { + fn step(pp: *Pardes, fr: *std.heap.ArenaAllocator, ev: pardes.Event) !u64 { + const t0 = panes.Places.nowNs(); + pp.update(ev); + _ = fr.reset(.retain_capacity); + _ = try pp.render(fr.allocator()); + return panes.Places.nowNs() - t0; + } + }; + var sum: u64 = 0; + var worst: u64 = 0; + for (0..10) |_| { + const d = try Clock.step(p, &frame, .{ .key = .{ .cp = 'j', .text = "j" } }); + sum += d; + worst = @max(worst, d); + } + try report.print(testing.allocator, "{s}: j and its frame: mean {d} us, worst {d} us\n", .{ name, sum / 10 / 1000, worst / 1000 }); + _ = try Clock.step(p, &frame, .{ .key = .{ .cp = 'i', .text = "i" } }); + sum = 0; + worst = 0; + for (0..10) |_| { + const d = try Clock.step(p, &frame, .{ .key = .{ .cp = 'x', .text = "x" } }); + sum += d; + worst = @max(worst, d); + } + try report.print(testing.allocator, "{s}: a letter typed in a cell and its frame: mean {d} us, worst {d} us\n", .{ name, sum / 10 / 1000, worst / 1000 }); + } + std.debug.print("\n{s}", .{report.items}); +} |
