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