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