╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1673 } 1674 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1675 } 1676 1677 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1678 if (self.grid) |*grid| grid.deinit(); 1679 self.art.deinit(allocator); 1680 self.child.deinit(allocator); 1681 allocator.destroy(self.child); 1682 } 1683 1684 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1685 self.clearGrid(); 1686 1687 // make the wrapped page fill the whole available area 1688 try self.child.build(allocator, .{ 1689 .min_size = .{ 1690 .width = constraint.max_size.width orelse constraint.min_size.width, 1691 .height = constraint.max_size.height orelse constraint.min_size.height, 1692 }, 1693 .max_size = constraint.max_size, 1694 }, root_focus); 1695 1696 if (self.session.data.enable_ansi) { 1697 if (self.child.getGrid()) |fg| { 1698 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1699 } 1700 } 1701 } 1702 1703 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1704 try self.child.input(allocator, key, root_focus); 1705 } 1706 1707 pub fn clearGrid(self: *AnsiBackground) void { 1708 if (self.grid) |*grid| { 1709 grid.deinit(); 1710 self.grid = null; 1711 } 1712 self.child.clearGrid(); 1713 self.art.clearGrid(); 1714 } 1715 1716 pub fn getGrid(self: AnsiBackground) ?Grid { 1717 return self.grid orelse self.child.getGrid(); 1718 } 1719 1720 pub fn getFocus(self: *AnsiBackground) *Focus { 1721 return self.child.getFocus(); 1722 } 1723 1724 // which top corner the art hugs. 1725 const ArtAnchor = enum { top_left, top_right }; 1726 1727 // a foreground cell counts as blank if it's empty or a bare space without styling 1728 fn cellIsBlank(cell: Grid.Cell) bool { 1729 if (cell.rune) |rune| { 1730 return std.mem.eql(u8, rune, " ") and 1731 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1732 } 1733 return true; 1734 } 1735 1736 // the art is dimmed to this fraction of its brightness so foreground text 1737 // stays legible over it 1738 const art_brightness = 45; // percent 1739 1740 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1741 const c = color orelse return null; 1742 return .{ 1743 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1744 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1745 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1746 }; 1747 } 1748 1749 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1750 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1751 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1752 // snap such channels clear of that range so the backdrop doesn't blink or 1753 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1754 // actually render truecolor. 1755 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1756 const c = color orelse return null; 1757 const snap = struct { 1758 fn f(v: u8) u8 { 1759 return if (v >= 1 and v <= 9) 10 else v; 1760 } 1761 }.f; 1762 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1763 } 1764 1765 // composites ANSI art behind a foreground grid 1766 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1767 art.clearGrid(); 1768 try art.build(allocator, .{ 1769 .min_size = .{ .width = null, .height = null }, 1770 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1771 }, root_focus); 1772 1773 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1774 errdefer out.deinit(); 1775 1776 if (art.getGrid()) |art_grid| { 1777 const anchor_x = switch (anchor) { 1778 .top_left => 0, 1779 .top_right => foreground.size.width -| art_grid.size.width, 1780 }; 1781 for (0..art_grid.size.height) |y| { 1782 for (0..art_grid.size.width) |x| { 1783 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1784 if (src.rune == null) continue; 1785 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1786 const dst = &out.cells.items[idx]; 1787 // composite the dimmed art behind every cell so it shows 1788 // through the whole UI: a blank cell becomes the art, while a 1789 // cell with a glyph keeps the glyph but takes the dimmed art as 1790 // its background (replacing whatever background it had, which 1791 // would otherwise obscure the art). 1792 if (cellIsBlank(dst.*)) { 1793 dst.* = src; 1794 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1795 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1796 } else { 1797 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1798 } 1799 } 1800 } 1801 } 1802 return out; 1803 } 1804 }; 1805