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