//! Cell-based box-drawing borders, ported from uv `border.go`. A [`Border`] is //! a side for each edge and corner; [`draw`](Border.draw) stamps the matching //! side into every perimeter cell of an area on any [`screen.Screen`], leaving //! the interior untouched. The preset constructors mirror the uv `*Border()` //! functions, and [`withStyle`](Border.withStyle)/[`withLink`](Border.withLink) //! spread a pen or hyperlink across all eight sides. //! //! Named departure from upstream: a [`Side`] borrows its `content` slice (uv //! copies Go strings), so presets point at comptime literals. Each drawn cell //! is built fresh by `Cell.new` with the side's width/style/link, per uv //! `borderCell`. const std = @import("std"); const ir = @import("root.zig"); const scr = @import("screen.zig"); const Cell = ir.Cell; const Link = ir.Link; const Method = ir.width.Method; const Rectangle = ir.Rectangle; const Screen = scr.Screen; const Style = ir.Style; const t = std.testing; const Border = @This(); /// A single border side: its grapheme plus the pen and link stamped into the /// destination cells. const Side = struct { content: []const u8 = "", style: Style = .{}, link: Link = .{}, }; /// A complete border: one side per edge and corner. The zero value draws /// empty cells (uv `Border{}`). top: Side = .{}, bottom: Side = .{}, left: Side = .{}, right: Side = .{}, top_left: Side = .{}, top_right: Side = .{}, bottom_left: Side = .{}, bottom_right: Side = .{}, /// A copy of `self` with `style` applied to every side (uv `Border.Style`). pub fn withStyle(self: Border, style: Style) Border { var b = self; b.top.style = style; b.bottom.style = style; b.left.style = style; b.right.style = style; b.top_left.style = style; b.top_right.style = style; b.bottom_left.style = style; b.bottom_right.style = style; return b; } /// A copy of `self` with `link` applied to every side (uv `Border.Link`). pub fn withLink(self: Border, link: Link) Border { var b = self; b.top.link = link; b.bottom.link = link; b.left.link = link; b.right.link = link; b.top_left.link = link; b.top_right.link = link; b.bottom_left.link = link; b.bottom_right.link = link; return b; } /// Draws the border around `area` on `screen` (uv `Border.Draw`): every cell /// on the perimeter receives its side's cell; the interior is untouched. /// Corners win over edges, in top-left, top-right, bottom-left, /// bottom-right order (so degenerate areas still draw a corner). pub fn draw(self: *const Border, screen: *Screen, area: Rectangle) void { const m = screen.widthMethod(); var y = area.min_y; while (y < area.max_y) : (y += 1) { var x = area.min_x; while (x < area.max_x) : (x += 1) { var side: ?*const Side = null; if (y == area.min_y and x == area.min_x) { side = &self.top_left; } else if (y == area.min_y and x == area.max_x - 1) { side = &self.top_right; } else if (y == area.max_y - 1 and x == area.min_x) { side = &self.bottom_left; } else if (y == area.max_y - 1 and x == area.max_x - 1) { side = &self.bottom_right; } else if (y == area.min_y) { side = &self.top; } else if (y == area.max_y - 1) { side = &self.bottom; } else if (x == area.min_x) { side = &self.left; } else if (x == area.max_x - 1) { side = &self.right; } else { continue; } screen.setCell(x, y, borderCell(m, side.?)); } } } /// Builds the cell a side stamps: a fresh `Cell.new` plus the pen/link (uv /// `borderCell`). fn borderCell(method: Method, side: *const Side) Cell { var cell: Cell = .new(method, side.content); cell.style = side.style; cell.link = side.link; return cell; } pub const normal: Border = .{ .top = .{ .content = "─" }, .bottom = .{ .content = "─" }, .left = .{ .content = "│" }, .right = .{ .content = "│" }, .top_left = .{ .content = "┌" }, .top_right = .{ .content = "┐" }, .bottom_left = .{ .content = "└" }, .bottom_right = .{ .content = "┘" }, }; pub const rounded: Border = .{ .top = .{ .content = "─" }, .bottom = .{ .content = "─" }, .left = .{ .content = "│" }, .right = .{ .content = "│" }, .top_left = .{ .content = "╭" }, .top_right = .{ .content = "╮" }, .bottom_left = .{ .content = "╰" }, .bottom_right = .{ .content = "╯" }, }; pub const block: Border = .{ .top = .{ .content = "█" }, .bottom = .{ .content = "█" }, .left = .{ .content = "█" }, .right = .{ .content = "█" }, .top_left = .{ .content = "█" }, .top_right = .{ .content = "█" }, .bottom_left = .{ .content = "█" }, .bottom_right = .{ .content = "█" }, }; pub const outer_half_block: Border = .{ .top = .{ .content = "▀" }, .bottom = .{ .content = "▄" }, .left = .{ .content = "▌" }, .right = .{ .content = "▐" }, .top_left = .{ .content = "▛" }, .top_right = .{ .content = "▜" }, .bottom_left = .{ .content = "▙" }, .bottom_right = .{ .content = "▟" }, }; pub const inner_half_block: Border = .{ .top = .{ .content = "▄" }, .bottom = .{ .content = "▀" }, .left = .{ .content = "▐" }, .right = .{ .content = "▌" }, .top_left = .{ .content = "▗" }, .top_right = .{ .content = "▖" }, .bottom_left = .{ .content = "▝" }, .bottom_right = .{ .content = "▘" }, }; pub const thick: Border = .{ .top = .{ .content = "━" }, .bottom = .{ .content = "━" }, .left = .{ .content = "┃" }, .right = .{ .content = "┃" }, .top_left = .{ .content = "┏" }, .top_right = .{ .content = "┓" }, .bottom_left = .{ .content = "┗" }, .bottom_right = .{ .content = "┛" }, }; pub const double: Border = .{ .top = .{ .content = "═" }, .bottom = .{ .content = "═" }, .left = .{ .content = "║" }, .right = .{ .content = "║" }, .top_left = .{ .content = "╔" }, .top_right = .{ .content = "╗" }, .bottom_left = .{ .content = "╚" }, .bottom_right = .{ .content = "╝" }, }; pub const hidden: Border = .{ .top = .{ .content = " " }, .bottom = .{ .content = " " }, .left = .{ .content = " " }, .right = .{ .content = " " }, .top_left = .{ .content = " " }, .top_right = .{ .content = " " }, .bottom_left = .{ .content = " " }, .bottom_right = .{ .content = " " }, }; pub const markdown: Border = .{ .left = .{ .content = "|" }, .right = .{ .content = "|" }, .top_left = .{ .content = "|" }, .top_right = .{ .content = "|" }, .bottom_left = .{ .content = "|" }, .bottom_right = .{ .content = "|" }, }; pub const ascii: Border = .{ .top = .{ .content = "-" }, .bottom = .{ .content = "-" }, .left = .{ .content = "|" }, .right = .{ .content = "|" }, .top_left = .{ .content = "+" }, .top_right = .{ .content = "+" }, .bottom_left = .{ .content = "+" }, .bottom_right = .{ .content = "+" }, }; test "border presets" { const cases = [_]struct { b: Border, edges: [4][]const u8, corners: [4][]const u8 }{ .{ .b = .normal, .edges = .{ "─", "─", "│", "│" }, .corners = .{ "┌", "┐", "└", "┘" } }, .{ .b = .rounded, .edges = .{ "─", "─", "│", "│" }, .corners = .{ "╭", "╮", "╰", "╯" } }, .{ .b = .block, .edges = .{ "█", "█", "█", "█" }, .corners = .{ "█", "█", "█", "█" } }, .{ .b = .outer_half_block, .edges = .{ "▀", "▄", "▌", "▐" }, .corners = .{ "▛", "▜", "▙", "▟" } }, .{ .b = .inner_half_block, .edges = .{ "▄", "▀", "▐", "▌" }, .corners = .{ "▗", "▖", "▝", "▘" } }, .{ .b = .thick, .edges = .{ "━", "━", "┃", "┃" }, .corners = .{ "┏", "┓", "┗", "┛" } }, .{ .b = .double, .edges = .{ "═", "═", "║", "║" }, .corners = .{ "╔", "╗", "╚", "╝" } }, .{ .b = .hidden, .edges = .{ " ", " ", " ", " " }, .corners = .{ " ", " ", " ", " " } }, .{ .b = .markdown, .edges = .{ "", "", "|", "|" }, .corners = .{ "|", "|", "|", "|" } }, .{ .b = .ascii, .edges = .{ "-", "-", "|", "|" }, .corners = .{ "+", "+", "+", "+" } }, }; for (cases) |case| { try t.expectEqualStrings(case.edges[0], case.b.top.content); try t.expectEqualStrings(case.edges[1], case.b.bottom.content); try t.expectEqualStrings(case.edges[2], case.b.left.content); try t.expectEqualStrings(case.edges[3], case.b.right.content); try t.expectEqualStrings(case.corners[0], case.b.top_left.content); try t.expectEqualStrings(case.corners[1], case.b.top_right.content); try t.expectEqualStrings(case.corners[2], case.b.bottom_left.content); try t.expectEqualStrings(case.corners[3], case.b.bottom_right.content); } } test "border style and link spread without mutating the base" { const base: Border = .normal; const pen: Style = .{ .attrs = .{ .bold = true } }; const link: Link = .init("https://example.com", "id=1"); const border = base.withStyle(pen).withLink(link); inline for ([8]Side{ border.top, border.bottom, border.left, border.right, border.top_left, border.top_right, border.bottom_left, border.bottom_right, }) |s| { try t.expect(s.style.equal(pen)); try t.expectEqualStrings(link.url, s.link.url); try t.expectEqualStrings(link.params, s.link.params); } try t.expect(base.top.style.isZero()); try t.expectEqualStrings("", base.top.link.url); try t.expectEqualStrings("", base.top.link.params); } test "border draw normal" { var dst: ir.Screen = try .init(t.allocator, 20, 10); defer dst.deinit(); var border: Border = .normal; border.draw(&dst.screen, .new(1, 1, 6, 4)); try t.expectEqualStrings("┌", dst.cellAt(1, 1).?.content); try t.expectEqualStrings("┐", dst.cellAt(6, 1).?.content); try t.expectEqualStrings("└", dst.cellAt(1, 4).?.content); try t.expectEqualStrings("┘", dst.cellAt(6, 4).?.content); var x: usize = 2; while (x <= 5) : (x += 1) { try t.expectEqualStrings("─", dst.cellAt(@intCast(x), 1).?.content); try t.expectEqualStrings("─", dst.cellAt(@intCast(x), 4).?.content); } var y: usize = 2; while (y <= 3) : (y += 1) { try t.expectEqualStrings("│", dst.cellAt(1, @intCast(y)).?.content); try t.expectEqualStrings("│", dst.cellAt(6, @intCast(y)).?.content); } y = 2; while (y <= 3) : (y += 1) { x = 2; while (x <= 5) : (x += 1) { try t.expectEqualStrings(" ", dst.cellAt(@intCast(x), @intCast(y)).?.content); } } } test "border draw hidden with style and link" { var dst: ir.Screen = try .init(t.allocator, 10, 6); defer dst.deinit(); const pen: Style = .{ .attrs = .{ .bold = true } }; const link = Link.init("https://example.com", ""); var b = Border.hidden.withStyle(pen).withLink(link); b.draw(&dst.screen, .new(2, 2, 5, 3)); const perimeter: [12][2]usize = .{ .{ 2, 2 }, .{ 6, 2 }, .{ 2, 4 }, .{ 6, 4 }, .{ 3, 2 }, .{ 4, 2 }, .{ 5, 2 }, .{ 3, 4 }, .{ 4, 4 }, .{ 5, 4 }, .{ 2, 3 }, .{ 6, 3 }, }; for (perimeter) |p| { const c = dst.cellAt(@intCast(p[0]), @intCast(p[1])).?; try t.expectEqualStrings(" ", c.content); try t.expect(c.style.equal(pen)); try t.expectEqualStrings(link.url, c.link.url); } const c = dst.cellAt(4, 3).?; try t.expectEqualStrings(" ", c.content); try t.expect(c.style.isZero()); try t.expectEqualStrings("", c.link.url); } test "border draw small areas" { var dst: ir.Screen = try .init(t.allocator, 3, 3); defer dst.deinit(); var border: Border = .normal; border.draw(&dst.screen, .new(0, 0, 1, 1)); try t.expectEqualStrings("┌", dst.cellAt(0, 0).?.content); border.draw(&dst.screen, .new(0, 1, 1, 2)); try t.expectEqualStrings("┌", dst.cellAt(0, 1).?.content); try t.expectEqualStrings("└", dst.cellAt(0, 2).?.content); } test "fuzz border draw" { try t.fuzz({}, fuzzBorderDraw, .{ .corpus = &.{ "normal", "rounded", "hidden", "block", "世", "ab┌c", }, }); } fn fuzzBorderDraw(_: void, smith: *t.Smith) anyerror!void { var buf: ir.Screen = try .init(t.allocator, 24, 16); defer buf.deinit(); const presets = [_]Border{ .normal, .rounded, .block, .outer_half_block, .inner_half_block, .thick, .double, .hidden, .markdown, .ascii, }; var border = presets[smith.index(presets.len)]; if (smith.value(bool)) border = border.withStyle(.{ .attrs = .{ .bold = true } }); if (smith.value(bool)) border = border.withLink(.init("https://b.example/", "id=1")); // Randomize the side contents with a small vocabulary so drawing exercises // empty, narrow, and wide (combining / double-width) graphemes. const set = [_][]const u8{ "", "─", "│", "█", "a", "世", "┌" }; border.top.content = set[smith.index(set.len)]; border.bottom.content = set[smith.index(set.len)]; border.left.content = set[smith.index(set.len)]; border.right.content = set[smith.index(set.len)]; border.top_left.content = set[smith.index(set.len)]; border.top_right.content = set[smith.index(set.len)]; border.bottom_left.content = set[smith.index(set.len)]; border.bottom_right.content = set[smith.index(set.len)]; const x: ir.Size = @intCast(smith.index(20)); const y: ir.Size = @intCast(smith.index(12)); const w: ir.Size = @intCast(smith.index(16)); const h: ir.Size = @intCast(smith.index(10)); border.draw(&buf.screen, .new(x, y, w, h)); }