╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1700 style.* = .{}; 1701 return; 1702 } 1703 var i: usize = 0; 1704 while (i < n) : (i += 1) { 1705 switch (nums[i]) { 1706 0 => style.* = .{}, 1707 7 => style.inverted = true, 1708 27 => style.inverted = false, 1709 39 => style.fg = null, 1710 49 => style.bg = null, 1711 38, 48 => { 1712 // truecolor form: 38;2;r;g;b — anything else is ignored 1713 if (i + 4 < n and nums[i + 1] == 2) { 1714 const c = Grid.Color{ 1715 .r = @truncate(nums[i + 2]), 1716 .g = @truncate(nums[i + 3]), 1717 .b = @truncate(nums[i + 4]), 1718 }; 1719 if (nums[i] == 38) style.fg = c else style.bg = c; 1720 i += 4; 1721 } 1722 }, 1723 else => {}, 1724 } 1725 } 1726 } 1727 }; 1728 1729 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1730 pub const AnsiBackground = struct { 1731 grid: ?Grid, 1732 child: *Widget, 1733 art: AnsiArt, 1734 session: *Session, 1735 1736 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1737 var cw = child_widget; 1738 const child = allocator.create(Widget) catch |e| { 1739 cw.deinit(allocator); 1740 return e; 1741 }; 1742 child.* = cw; 1743 errdefer { 1744 child.deinit(allocator); 1745 allocator.destroy(child); 1746 } 1747 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1748 } 1749 1750 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1751 if (self.grid) |*grid| grid.deinit(); 1752 self.art.deinit(allocator); 1753 self.child.deinit(allocator); 1754 allocator.destroy(self.child); 1755 } 1756 1757 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1758 self.clearGrid(); 1759 1760 // make the wrapped page fill the whole available area 1761 try self.child.build(allocator, .{ 1762 .min_size = .{ 1763 .width = constraint.max_size.width orelse constraint.min_size.width, 1764 .height = constraint.max_size.height orelse constraint.min_size.height, 1765 }, 1766 .max_size = constraint.max_size, 1767 }, root_focus); 1768 1769 if (self.session.data.enable_ansi) { 1770 if (self.child.getGrid()) |fg| { 1771 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1772 } 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