summaryrefslogtreecommitdiff
path: root/src/Tables.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-24 14:05:34 -0300
committerGabriel Schneider <[email protected]>2026-10-24 15:56:58 -0300
commit8f5871340b87a0d5399967ccc7eac0d12dc10821 (patch)
tree96257104c2384c2c8f08b69a958228f67daab6ea /src/Tables.zig
parentce6ba3d8a9e1f5c1e31892639049317f6a27b1bc (diff)
downloadpardes-8f5871340b87a0d5399967ccc7eac0d12dc10821.tar.gz
pardes-8f5871340b87a0d5399967ccc7eac0d12dc10821.zip
Tables 2/3: the look, a table's pipes drawn as rules, its delimiter row one rule with its colons kept, its header bold on the tag's ground (glyphs and colours on the cells there, nothing moved); the GUI draws light box-drawing rules edge to edge so they join across rows
Diffstat (limited to 'src/Tables.zig')
-rw-r--r--src/Tables.zig118
1 files changed, 116 insertions, 2 deletions
diff --git a/src/Tables.zig b/src/Tables.zig
index ca4a4b57..8038dd65 100644
--- a/src/Tables.zig
+++ b/src/Tables.zig
@@ -31,7 +31,10 @@ 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;
@@ -448,7 +451,7 @@ pub fn between(pads: []const Pad, from: usize, to: usize) i32 {
/// 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 = @import("modal.zig").graphemeStart(text, from_raw);
+ 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;
@@ -458,7 +461,7 @@ pub fn byteAt(text: []const u8, pads: []const Pad, from_raw: usize, col: usize)
k += 1;
if (col < used) return at;
}
- const end = @import("modal.zig").nextGrapheme(text, at);
+ const end = modal.nextGrapheme(text, at);
used += File.graphemeDisplayWidth(text[at..end]);
if (col < used) return at;
at = end;
@@ -466,6 +469,101 @@ pub fn byteAt(text: []const u8, pads: []const Pad, from_raw: usize, col: usize)
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, 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;
@@ -578,6 +676,22 @@ test "tables: a markdown table is drawn aligned, its bytes in order, the caret a
}
}
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;