summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/File.zig16
-rw-r--r--src/Tables.zig118
-rw-r--r--src/body_layer.zig1
-rw-r--r--src/gui/gui.zig72
-rw-r--r--src/panes.zig1
5 files changed, 199 insertions, 9 deletions
diff --git a/src/File.zig b/src/File.zig
index ddf91af3..5db04443 100644
--- a/src/File.zig
+++ b/src/File.zig
@@ -587,12 +587,18 @@ test "indexed source rows match uncached scans across content changes" {
/// else none. Every mapping between a file row's bytes and its cells goes
/// through here, so the drawn row, the caret and a click cannot disagree.
pub fn rowPads(pane: *const Pane, row: i32, out: *[Tables.max_pads]Tables.Pad) []const Tables.Pad {
- const f = if (pane.file) |*file| file else return &.{};
- if (row < 0 or f.tables.list.items.len == 0 or !f.tables.current(f)) return &.{};
- const table = f.tables.find(@intCast(row)) orelse return &.{};
+ const table = rowTable(pane, row) orelse return &.{};
+ return pane.file.?.tables.padsOf(table, sourceLine(pane, row), out);
+}
+
+/// The table `row` is laid out in, in this pane (`rowPads`).
+pub fn rowTable(pane: *const Pane, row: i32) ?*const Tables.Table {
+ const f = if (pane.file) |*file| file else return null;
+ if (row < 0 or f.tables.list.items.len == 0 or !f.tables.current(f)) return null;
+ const table = f.tables.find(@intCast(row)) orelse return null;
const width = wrapWidth(pane, pane.body_wrapped);
- if (width > 0 and table.width > width) return &.{};
- return f.tables.padsOf(table, sourceLine(pane, row), out);
+ if (width > 0 and table.width > width) return null;
+ return table;
}
pub fn displayOffset(pane: *const Pane, row: i32, from_raw: i32, to_raw: i32) i32 {
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;
diff --git a/src/body_layer.zig b/src/body_layer.zig
index 90622e52..3684605f 100644
--- a/src/body_layer.zig
+++ b/src/body_layer.zig
@@ -282,6 +282,7 @@ pub fn renderBody(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane
panes.File.drawGutter(p, s, pane, r, tx, tw, body_h, active);
if (p.settings.colors and pane.colorAlgo() != .none)
panes.File.recolorSyntax(p, s, pane, f, r, tx, tw, body_h);
+ panes.Tables.paint(p, s, pane, r, tx, tw, body_h);
panes.File.drawWrapMarkers(p, s, pane, r, tx, tw, body_h, pane_bg);
pane.places.paint(p, s, pane, r, body_h);
panes.Query.paint(p, s, pane, r, body_h);
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 49a98f5b..d71f7047 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -105,9 +105,52 @@ const GlyphKey = packed struct(u64) {
centered: bool = false,
lead: u4 = 0,
span: u4 = 1,
- _: u2 = 0,
+ /// A light box-drawing rule (`ruleArms`), drawn edge to edge rather
+ /// than from the font; `glyph` is its codepoint.
+ rule: bool = false,
+ _: u1 = 0,
};
+/// The light box-drawing rules drawn by hand, as Ghostty and kitty draw
+/// them: a logical pixel thick through the cell's middle, out to its edges,
+/// so a markdown table's `|` (Tables.zig) joins the rows above and below it
+/// and its delimiter row is one line across, at any line height a font's
+/// own glyph would leave gaps in. The arms each one has: left, right, up, down.
+fn ruleArms(cp: u32) ?[4]bool {
+ return switch (cp) {
+ 0x2500 => .{ true, true, false, false }, // ─
+ 0x2502 => .{ false, false, true, true }, // │
+ 0x250c => .{ false, true, false, true }, // ┌
+ 0x2510 => .{ true, false, false, true }, // ┐
+ 0x2514 => .{ false, true, true, false }, // └
+ 0x2518 => .{ true, false, true, false }, // ┘
+ 0x251c => .{ false, true, true, true }, // ├
+ 0x2524 => .{ true, false, true, true }, // ┤
+ 0x252c => .{ true, true, false, true }, // ┬
+ 0x2534 => .{ true, true, true, false }, // ┴
+ 0x253c => .{ true, true, true, true }, // ┼
+ else => null,
+ };
+}
+
+fn drawRule(out: [*]u8, stride: usize, width: usize, height: usize, arms: [4]bool) void {
+ if (width == 0 or height == 0) return;
+ const t: usize = @min(@max(1, @as(usize, @intFromFloat(dp(1)))), @min(width, height));
+ const mx = (width - t) / 2;
+ const my = (height - t) / 2;
+ for (0..height) |y| @memset(out[y * stride ..][0..width], 0);
+ if (arms[0] or arms[1]) for (0..t) |k| {
+ const from: usize = if (arms[0]) 0 else mx;
+ const to: usize = if (arms[1]) width else mx + t;
+ @memset(out[(my + k) * stride + from ..][0 .. to - from], 255);
+ };
+ if (arms[2] or arms[3]) {
+ const from: usize = if (arms[2]) 0 else my;
+ const to: usize = if (arms[3]) height else my + t;
+ for (from..to) |y| @memset(out[y * stride + mx ..][0..t], 255);
+ }
+}
+
const GlyphDecoration = packed struct(u5) {
bold: bool = false,
underline: @FieldType(pardes.CellStyle, "ul") = .off,
@@ -7314,6 +7357,8 @@ fn wordCell(cell: *const pardes.Cell) WordCell {
/// cell tints its own slice.
fn wordBreak(a: WordCell, b: WordCell, i: usize, cursor: ?usize) bool {
if (a.cp == ' ' or b.cp == ' ') return true;
+ // A rule is drawn by hand (ruleArms), never a slice of a shaped word.
+ if (ruleArms(a.cp) != null or ruleArms(b.cp) != null) return true;
if (cursor) |at| if (at == i or at == i + 1) return true;
if (a.role != b.role or !std.meta.eql(a.decoration, b.decoration)) return true;
return (a.cp == 'f' and (b.cp == 'i' or b.cp == 'l')) or (a.cp == 's' and b.cp == 't');
@@ -7334,6 +7379,8 @@ fn cellSlot(g: *Gui, line: []const pardes.Cell, at: usize, cursor: ?usize, role:
var here = wordCell(&line[at]);
// An underlined or struck-out space is a glyph of its own: it draws its line.
if (here.cp == ' ' and !here.decoration.drawsBlank()) return g.space_slot;
+ if (ruleArms(here.cp) != null)
+ return cachedGlyph(g, .{ .face = 0, .glyph = here.cp, .role = role, .rule = true }) orelse g.space_slot;
const gpa = g.glyphs.allocator;
const cell_key: Words.CellKey = .{ .cp = @intCast(here.cp), .role = role, .decoration = here.decoration };
const known = words.cells.getOrPut(gpa, cell_key) catch return g.space_slot;
@@ -7589,7 +7636,9 @@ fn cachedGlyph(g: *Gui, key: GlyphKey) ?Slot {
const raster_w = if (key.centered) slot_w else cell_w * key.span;
const raster_h = if (key.role == .tagline) g.tagline_height else g.cell_h;
const bold = @intFromBool(key.decoration.bold);
- if (key.centered) {
+ if (key.rule) {
+ drawRule(out, atlas_w, raster_w, raster_h, ruleArms(key.glyph).?);
+ } else if (key.centered) {
_ = c.ui_font_raster_centered(face, px, key.glyph, bold, out, @intCast(atlas_w), @intCast(raster_w), @intCast(raster_h));
} else {
_ = c.ui_font_raster(face, px, key.glyph, bold, out, @intCast(atlas_w), @intCast(raster_w), @intCast(raster_h), @intCast(cell_w * key.lead), @intCast(cell_w), baseline);
@@ -7764,6 +7813,25 @@ fn decorateLine(out: [*]u8, stride: usize, width: usize, height: usize, baseline
}
}
+test "a box-drawing rule reaches its cell's edges, and only its arms do" {
+ const stride = 12;
+ const width = 9;
+ const height = 15;
+ var mask: [stride * height]u8 = @splat(0xa5);
+ drawRule(&mask, stride, width, height, ruleArms(0x253c).?);
+ // ┼: ink on all four edges, none in the corners, nothing past the slot.
+ try std.testing.expectEqual(@as(u8, 255), mask[0 * stride + width / 2]);
+ try std.testing.expectEqual(@as(u8, 255), mask[(height - 1) * stride + width / 2]);
+ try std.testing.expectEqual(@as(u8, 255), mask[(height / 2) * stride]);
+ try std.testing.expectEqual(@as(u8, 255), mask[(height / 2) * stride + width - 1]);
+ try std.testing.expectEqual(@as(u8, 0), mask[0]);
+ for (0..height) |y| for (mask[y * stride + width ..][0 .. stride - width]) |guard| try std.testing.expectEqual(@as(u8, 0xa5), guard);
+ drawRule(&mask, stride, width, height, ruleArms(0x2502).?);
+ try std.testing.expectEqual(@as(u8, 0), mask[(height / 2) * stride]);
+ try std.testing.expectEqual(@as(u8, 255), mask[0 * stride + width / 2]);
+ try std.testing.expect(ruleArms('|') == null);
+}
+
test "glyph decorations render spaces and stay inside their atlas slot" {
const stride = 9;
const width = 5;
diff --git a/src/panes.zig b/src/panes.zig
index 10e0d427..20618ccc 100644
--- a/src/panes.zig
+++ b/src/panes.zig
@@ -662,6 +662,7 @@ pub const File = @import("File.zig");
pub const Output = @import("Output.zig");
pub const Places = @import("Places.zig");
+pub const Tables = @import("Tables.zig");
pub const Query = @import("Query.zig");