╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1752 if (self.grid) |*grid| grid.deinit(); 1753 self.arena.deinit(); 1754 } 1755 1756 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1757 _ = root_focus; 1758 self.clearGrid(); 1759 const width = constraint.max_size.width orelse return; 1760 if (width == 0) return; 1761 1762 _ = self.arena.reset(.retain_capacity); 1763 const aa = self.arena.allocator(); 1764 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1765 const text = if (self.session.web_port) |port| 1766 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1767 else 1768 path; 1769 1770 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1771 errdefer grid.deinit(); 1772 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1773 var i: usize = 0; 1774 while (utf8.nextCodepointSlice()) |ch| { 1775 if (i == width) break; 1776 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1777 i += 1; 1778 } 1779 self.grid = grid; 1780 } 1781 1782 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1783 _ = self; 1784 _ = allocator; 1785 _ = key; 1786 _ = root_focus; 1787 } 1788 1789 pub fn clearGrid(self: *Footer) void { 1790 if (self.grid) |*grid| { 1791 grid.deinit(); 1792 self.grid = null; 1793 } 1794 } 1795 1796 pub fn getGrid(self: Footer) ?Grid { 1797 return self.grid; 1798 } 1799 1800 pub fn getFocus(self: *Footer) *Focus { 1801 return self.focus; 1802 } 1803 }; 1804 1805 // a single-child wrapper that builds the child at its natural size and 1806 // positions its grid in the middle of the area granted by the parent 1807 pub const Center = struct { 1808 focus: *Focus, 1809 grid: ?Grid, 1810 child: *Widget, 1811 1812 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1813 const child = try allocator.create(Widget); 1814 errdefer allocator.destroy(child); 1815 child.* = child_widget; 1816 return .{ 1817 .focus = try Focus.create(allocator, .container), 1818 .grid = null, 1819 .child = child, 1820 }; 1821 } 1822 1823 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1824 self.focus.destroy(allocator); 1825 if (self.grid) |*grid| { 1826 grid.deinit(); 1827 self.grid = null; 1828 } 1829 self.child.deinit(allocator); 1830 allocator.destroy(self.child); 1831 } 1832 1833 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1834 self.clearGrid(); 1835 self.getFocus().clear(); 1836 1837 // build the child without forcing it to fill min_size; it sizes 1838 // itself to its content within the available max. 1839 try self.child.build(allocator, .{ 1840 .min_size = .{ .width = null, .height = null }, 1841 .max_size = constraint.max_size, 1842 }, root_focus); 1843 1844 const child_grid = self.child.getGrid() orelse return; 1845 1846 // prefer max when given; otherwise grow to max(min, child) so that 1847 // when only a min is set (e.g. the wasm path passes viewport rows 1848 // as min_height with no max), we can still vertically center while 1849 // letting taller content extend past the min. 1850 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1851 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1852 if (width == 0 or height == 0) return; 1853 1854 const offset_x: usize = (width -| child_grid.size.width) / 2; 1855 const offset_y: usize = (height -| child_grid.size.height) / 2; 1856 1857 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1858 errdefer grid.deinit(); 1859 try grid.drawGrid(child_grid, offset_x, offset_y); 1860 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1861 self.getFocus().child_id = self.child.getFocus().id; 1862 1863 self.grid = grid; 1864 } 1865 1866 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1867 try self.child.input(allocator, key, root_focus); 1868 } 1869 1870 pub fn clearGrid(self: *Center) void { 1871 if (self.grid) |*grid| { 1872 grid.deinit(); 1873 self.grid = null; 1874 } 1875 self.child.clearGrid(); 1876 } 1877 1878 pub fn getGrid(self: Center) ?Grid { 1879 return self.grid; 1880 } 1881 1882 pub fn getFocus(self: *Center) *Focus { 1883 return self.focus; 1884 } 1885 }; 1886 1887 // renders truecolor ANSI art 1888 pub const AnsiArt = struct { 1889 focus: *Focus, 1890 grid: ?Grid, 1891 content: []const u8, 1892 1893 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1894 return .{ 1895 .focus = try Focus.create(allocator, .container), 1896 .grid = null, 1897 .content = content, 1898 }; 1899 } 1900 1901 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1902 self.focus.destroy(allocator); 1903 if (self.grid) |*grid| { 1904 grid.deinit(); 1905 self.grid = null; 1906 } 1907 } 1908 1909 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1910 _ = root_focus; 1911 self.clearGrid(); 1912 1913 // parse into rows of cells, then copy into a rectangular grid 1914 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1915 defer { 1916 for (rows.items) |*row| row.deinit(allocator); 1917 rows.deinit(allocator); 1918 } 1919 var row: std.ArrayList(Grid.Cell) = .empty; 1920 errdefer row.deinit(allocator); 1921 1922 var style: Grid.Style = .{}; 1923 var width: usize = 0; 1924 const content = self.content; 1925 var i: usize = 0; 1926 while (i < content.len) { 1927 const byte = content[i]; 1928 if (byte == '\n') { 1929 width = @max(width, row.items.len); 1930 try rows.append(allocator, row); 1931 row = .empty; 1932 i += 1; 1933 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1934 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1935 var j = i + 2; 1936 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1937 if (j >= content.len) { 1938 i = content.len; // malformed trailing escape; stop 1939 } else { 1940 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1941 i = j + 1; 1942 } 1943 } else { 1944 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1945 const end = @min(content.len, i + len); 1946 const rune = content[i..end]; 1947 const transparent = end - i == 1 and rune[0] == ' ' and 1948 style.fg == null and style.bg == null and !style.inverted; 1949 try row.append(allocator, .{ 1950 .rune = if (transparent) null else rune, 1951 .style = style, 1952 }); 1953 i = end; 1954 } 1955 } 1956 // a trailing row with content but no closing newline 1957 if (row.items.len > 0) { 1958 width = @max(width, row.items.len); 1959 try rows.append(allocator, row); 1960 } else { 1961 row.deinit(allocator); 1962 } 1963 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1964 1965 const height = rows.items.len; 1966 if (width == 0 or height == 0) return; 1967 1968 const clamped_w = @min(width, constraint.max_size.width orelse width); 1969 const clamped_h = @min(height, constraint.max_size.height orelse height); 1970 1971 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1972 errdefer grid.deinit(); 1973 for (rows.items[0..clamped_h], 0..) |r, y| { 1974 const n = @min(r.items.len, clamped_w); 1975 for (r.items[0..n], 0..) |cell, x| { 1976 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1977 } 1978 } 1979 self.grid = grid; 1980 } 1981 1982 pub fn clearGrid(self: *AnsiArt) void { 1983 if (self.grid) |*grid| { 1984 grid.deinit(); 1985 self.grid = null; 1986 } 1987 } 1988 1989 pub fn getGrid(self: AnsiArt) ?Grid { 1990 return self.grid; 1991 } 1992 1993 pub fn getFocus(self: *AnsiArt) *Focus { 1994 return self.focus; 1995 } 1996 1997 fn applySgr(style: *Grid.Style, params: []const u8) void { 1998 var nums: [16]u32 = undefined; 1999 var n: usize = 0; 2000 var it = std.mem.splitScalar(u8, params, ';'); 2001 while (it.next()) |tok| { 2002 if (n >= nums.len) break; 2003 // an empty parameter (e.g. bare "\x1b[m") means 0 2004 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2005 n += 1; 2006 } 2007 if (n == 0) { 2008 style.* = .{}; 2009 return; 2010 } 2011 var i: usize = 0; 2012 while (i < n) : (i += 1) { 2013 switch (nums[i]) { 2014 0 => style.* = .{}, 2015 7 => style.inverted = true, 2016 27 => style.inverted = false, 2017 39 => style.fg = null, 2018 49 => style.bg = null, 2019 38, 48 => { 2020 // truecolor form: 38;2;r;g;b — anything else is ignored 2021 if (i + 4 < n and nums[i + 1] == 2) { 2022 const c = Grid.Color{ 2023 .r = @truncate(nums[i + 2]), 2024 .g = @truncate(nums[i + 3]), 2025 .b = @truncate(nums[i + 4]), 2026 }; 2027 if (nums[i] == 38) style.fg = c else style.bg = c; 2028 i += 4; 2029 } 2030 }, 2031 else => {}, 2032 } 2033 } 2034 } 2035 }; 2036 2037 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2038 pub const AnsiBackground = struct { 2039 grid: ?Grid, 2040 child: *Widget, 2041 art: AnsiArt, 2042 session: *Session, 2043 2044 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2045 var cw = child_widget; 2046 const child = allocator.create(Widget) catch |e| { 2047 cw.deinit(allocator); 2048 return e; 2049 }; 2050 child.* = cw; 2051 errdefer { 2052 child.deinit(allocator); 2053 allocator.destroy(child); 2054 } 2055 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2056 } 2057 2058 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2059 if (self.grid) |*grid| grid.deinit(); 2060 self.art.deinit(allocator); 2061 self.child.deinit(allocator); 2062 allocator.destroy(self.child); 2063 } 2064 2065 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2066 self.clearGrid(); 2067 2068 // make the wrapped page fill the whole available area 2069 try self.child.build(allocator, .{ 2070 .min_size = .{ 2071 .width = constraint.max_size.width orelse constraint.min_size.width, 2072 .height = constraint.max_size.height orelse constraint.min_size.height, 2073 }, 2074 .max_size = constraint.max_size, 2075 }, root_focus); 2076 2077 if (self.session.data.enable_ansi) { 2078 if (self.child.getGrid()) |fg| { 2079 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2080 } 2081 } 2082 } 2083 2084 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2085 try self.child.input(allocator, key, root_focus); 2086 } 2087 2088 pub fn clearGrid(self: *AnsiBackground) void { 2089 if (self.grid) |*grid| { 2090 grid.deinit(); 2091 self.grid = null; 2092 } 2093 self.child.clearGrid(); 2094 self.art.clearGrid(); 2095 } 2096 2097 pub fn getGrid(self: AnsiBackground) ?Grid { 2098 return self.grid orelse self.child.getGrid(); 2099 } 2100 2101 pub fn getFocus(self: *AnsiBackground) *Focus { 2102 return self.child.getFocus(); 2103 } 2104 2105 // which top corner the art hugs. 2106 const ArtAnchor = enum { top_left, top_right }; 2107 2108 // a foreground cell counts as blank if it's empty or a bare space without styling 2109 fn cellIsBlank(cell: Grid.Cell) bool { 2110 if (cell.rune) |rune| { 2111 return std.mem.eql(u8, rune, " ") and 2112 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2113 } 2114 return true; 2115 } 2116 2117 // the art is dimmed to this fraction of its brightness so foreground text 2118 // stays legible over it 2119 const art_brightness = 45; // percent 2120 2121 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2122 const c = color orelse return null; 2123 return .{ 2124 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2125 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2126 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2127 }; 2128 } 2129 2130 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2131 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2132 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2133 // snap such channels clear of that range so the backdrop doesn't blink or 2134 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2135 // actually render truecolor. 2136 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2137 const c = color orelse return null; 2138 const snap = struct { 2139 fn f(v: u8) u8 { 2140 return if (v >= 1 and v <= 9) 10 else v; 2141 } 2142 }.f; 2143 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2144 } 2145 2146 // composites ANSI art behind a foreground grid 2147 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2148 art.clearGrid(); 2149 try art.build(allocator, .{ 2150 .min_size = .{ .width = null, .height = null }, 2151 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2152 }, root_focus); 2153 2154 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2155 errdefer out.deinit(); 2156 2157 if (art.getGrid()) |art_grid| { 2158 const anchor_x = switch (anchor) { 2159 .top_left => 0, 2160 .top_right => foreground.size.width -| art_grid.size.width, 2161 }; 2162 for (0..art_grid.size.height) |y| { 2163 for (0..art_grid.size.width) |x| { 2164 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2165 if (src.rune == null) continue; 2166 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2167 const dst = &out.cells.items[idx]; 2168 // composite the dimmed art behind every cell so it shows 2169 // through the whole UI: a blank cell becomes the art, while a 2170 // cell with a glyph keeps the glyph but takes the dimmed art as 2171 // its background (replacing whatever background it had, which 2172 // would otherwise obscure the art). 2173 if (cellIsBlank(dst.*)) { 2174 dst.* = src; 2175 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2176 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2177 } else { 2178 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2179 } 2180 } 2181 } 2182 } 2183 return out; 2184 } 2185 }; 2186