╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1761 } 1762 1763 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1764 try self.child.input(allocator, key, root_focus); 1765 } 1766 1767 pub fn clearGrid(self: *AnsiBackground) void { 1768 if (self.grid) |*grid| { 1769 grid.deinit(); 1770 self.grid = null; 1771 } 1772 self.child.clearGrid(); 1773 self.art.clearGrid(); 1774 } 1775 1776 pub fn getGrid(self: AnsiBackground) ?Grid { 1777 return self.grid orelse self.child.getGrid(); 1778 } 1779 1780 pub fn getFocus(self: *AnsiBackground) *Focus { 1781 return self.child.getFocus(); 1782 } 1783 1784 // which top corner the art hugs. 1785 const ArtAnchor = enum { top_left, top_right }; 1786 1787 // a foreground cell counts as blank if it's empty or a bare space without styling 1788 fn cellIsBlank(cell: Grid.Cell) bool { 1789 if (cell.rune) |rune| { 1790 return std.mem.eql(u8, rune, " ") and 1791 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1792 } 1793 return true; 1794 } 1795 1796 // the art is dimmed to this fraction of its brightness so foreground text 1797 // stays legible over it 1798 const art_brightness = 45; // percent 1799 1800 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1801 const c = color orelse return null; 1802 return .{ 1803 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1804 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1805 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1806 }; 1807 } 1808 1809 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1810 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1811 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1812 // snap such channels clear of that range so the backdrop doesn't blink or 1813 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1814 // actually render truecolor. 1815 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1816 const c = color orelse return null; 1817 const snap = struct { 1818 fn f(v: u8) u8 { 1819 return if (v >= 1 and v <= 9) 10 else v; 1820 } 1821 }.f; 1822 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1823 } 1824 1825 // composites ANSI art behind a foreground grid 1826 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1827 art.clearGrid(); 1828 try art.build(allocator, .{ 1829 .min_size = .{ .width = null, .height = null }, 1830 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1831 }, root_focus); 1832 1833 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1834 errdefer out.deinit(); 1835 1836 if (art.getGrid()) |art_grid| { 1837 const anchor_x = switch (anchor) { 1838 .top_left => 0, 1839 .top_right => foreground.size.width -| art_grid.size.width, 1840 }; 1841 for (0..art_grid.size.height) |y| { 1842 for (0..art_grid.size.width) |x| { 1843 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1844 if (src.rune == null) continue; 1845 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1846 const dst = &out.cells.items[idx]; 1847 // composite the dimmed art behind every cell so it shows 1848 // through the whole UI: a blank cell becomes the art, while a 1849 // cell with a glyph keeps the glyph but takes the dimmed art as 1850 // its background (replacing whatever background it had, which 1851 // would otherwise obscure the art). 1852 if (cellIsBlank(dst.*)) { 1853 dst.* = src; 1854 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1855 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1856 } else { 1857 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1858 } 1859 } 1860 } 1861 } 1862 return out; 1863 } 1864 }; 1865