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