╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1803 }, root_focus); 1804 const tb_grid_maybe = tb.getGrid(); 1805 const w: usize = if (tb_grid_maybe) |g| g.size.width else 0; 1806 const h: usize = if (tb_grid_maybe) |g| g.size.height else 0; 1807 if (x > 0 and x + w > max_width) { 1808 x = 0; 1809 y += row_height; 1810 row_height = 0; 1811 } 1812 try self.rects.append(allocator, .{ .x = @intCast(x), .y = @intCast(y), .size = .{ .width = w, .height = h } }); 1813 if (h > row_height) row_height = h; 1814 x += w; 1815 if (x > total_width) total_width = x; 1816 } 1817 1818 const total_height = y + row_height; 1819 if (total_width == 0 or total_height == 0) return; 1820 1821 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1822 errdefer grid.deinit(); 1823 1824 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1825 const tb_grid = tb.getGrid() orelse continue; 1826 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1827 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1828 } 1829 1830 self.grid = grid; 1831 } 1832 1833 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1834 _ = self; 1835 _ = allocator; 1836 _ = key; 1837 _ = root_focus; 1838 } 1839 1840 pub fn clearGrid(self: *TagFlow) void { 1841 if (self.grid) |*grid| { 1842 grid.deinit(); 1843 self.grid = null; 1844 } 1845 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1846 } 1847 1848 pub fn getGrid(self: TagFlow) ?Grid { 1849 return self.grid; 1850 } 1851 1852 pub fn getFocus(self: *TagFlow) *Focus { 1853 return self.focus; 1854 } 1855 1856 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1857 for (self.text_boxes.items, 0..) |tb, i| { 1858 if (tb.box.focus.id == focus_id) return i; 1859 } 1860 return null; 1861 } 1862 1863 // the first item of the row below (when `downward`) or above, or null at 1864 // the flow's edge. items are laid out in order, so y is nondecreasing. 1865 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1866 const rects = self.rects.items; 1867 if (index >= rects.len) return null; 1868 const cur_y = rects[index].y; 1869 if (downward) { 1870 for (rects[index + 1 ..], index + 1..) |r, i| { 1871 if (r.y > cur_y) return i; 1872 } 1873 return null; 1874 } 1875 // walk the earlier rows; the last one visited is the row above. 1876 var prev_first: ?usize = null; 1877 var i: usize = 0; 1878 while (i < rects.len and rects[i].y < cur_y) { 1879 prev_first = i; 1880 const y = rects[i].y; 1881 while (i < rects.len and rects[i].y == y) i += 1; 1882 } 1883 return prev_first; 1884 } 1885 }; 1886 1887 // an invisible widget that fills the horizontal space granted by its parent. 1888 // used inside Box(horiz) with a min_size so the box reserves space for the 1889 // children that follow, pushing them to the right. 1890 pub const Spacer = struct { 1891 focus: *Focus, 1892 grid: ?Grid, 1893 1894 pub fn init(allocator: std.mem.Allocator) !Spacer { 1895 return .{ 1896 .focus = try Focus.create(allocator, .container), 1897 .grid = null, 1898 }; 1899 } 1900 1901 pub fn deinit(self: *Spacer, 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: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1910 _ = root_focus; 1911 self.clearGrid(); 1912 const width = constraint.max_size.width orelse return; 1913 if (width == 0) return; 1914 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1915 } 1916 1917 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1918 _ = self; 1919 _ = allocator; 1920 _ = key; 1921 _ = root_focus; 1922 } 1923 1924 pub fn clearGrid(self: *Spacer) void { 1925 if (self.grid) |*grid| { 1926 grid.deinit(); 1927 self.grid = null; 1928 } 1929 } 1930 1931 pub fn getGrid(self: Spacer) ?Grid { 1932 return self.grid; 1933 } 1934 1935 pub fn getFocus(self: *Spacer) *Focus { 1936 return self.focus; 1937 } 1938 }; 1939 1940 // a one-row, non-focusable status bar showing the current page's url 1941 pub const Footer = struct { 1942 focus: *Focus, 1943 grid: ?Grid, 1944 session: *Session, 1945 arena: std.heap.ArenaAllocator, 1946 1947 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1948 return .{ 1949 .focus = try Focus.create(allocator, .container), 1950 .grid = null, 1951 .session = session, 1952 .arena = std.heap.ArenaAllocator.init(allocator), 1953 }; 1954 } 1955 1956 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1957 self.focus.destroy(allocator); 1958 if (self.grid) |*grid| grid.deinit(); 1959 self.arena.deinit(); 1960 } 1961 1962 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1963 _ = root_focus; 1964 self.clearGrid(); 1965 const width = constraint.max_size.width orelse return; 1966 if (width == 0) return; 1967 1968 _ = self.arena.reset(.retain_capacity); 1969 const aa = self.arena.allocator(); 1970 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 1971 const text = if (self.session.web_port) |port| 1972 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1973 else 1974 path; 1975 1976 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1977 errdefer grid.deinit(); 1978 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1979 var i: usize = 0; 1980 while (utf8.nextCodepointSlice()) |ch| { 1981 if (i == width) break; 1982 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1983 i += 1; 1984 } 1985 self.grid = grid; 1986 } 1987 1988 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1989 _ = self; 1990 _ = allocator; 1991 _ = key; 1992 _ = root_focus; 1993 } 1994 1995 pub fn clearGrid(self: *Footer) void { 1996 if (self.grid) |*grid| { 1997 grid.deinit(); 1998 self.grid = null; 1999 } 2000 } 2001 2002 pub fn getGrid(self: Footer) ?Grid { 2003 return self.grid; 2004 } 2005 2006 pub fn getFocus(self: *Footer) *Focus { 2007 return self.focus; 2008 } 2009 }; 2010 2011 // a single-child wrapper that builds the child at its natural size and 2012 // positions its grid in the middle of the area granted by the parent 2013 pub const Center = struct { 2014 focus: *Focus, 2015 grid: ?Grid, 2016 child: *Widget, 2017 2018 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2019 const child = try allocator.create(Widget); 2020 errdefer allocator.destroy(child); 2021 child.* = child_widget; 2022 return .{ 2023 .focus = try Focus.create(allocator, .container), 2024 .grid = null, 2025 .child = child, 2026 }; 2027 } 2028 2029 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2030 self.focus.destroy(allocator); 2031 if (self.grid) |*grid| { 2032 grid.deinit(); 2033 self.grid = null; 2034 } 2035 self.child.deinit(allocator); 2036 allocator.destroy(self.child); 2037 } 2038 2039 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2040 self.clearGrid(); 2041 self.getFocus().clear(); 2042 2043 // build the child without forcing it to fill min_size; it sizes 2044 // itself to its content within the available max. 2045 try self.child.build(allocator, .{ 2046 .min_size = .{ .width = null, .height = null }, 2047 .max_size = constraint.max_size, 2048 }, root_focus); 2049 2050 const child_grid = self.child.getGrid() orelse return; 2051 2052 // prefer max when given; otherwise grow to max(min, child) so that 2053 // when only a min is set (e.g. the wasm path passes viewport rows 2054 // as min_height with no max), we can still vertically center while 2055 // letting taller content extend past the min. 2056 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; 2057 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; 2058 if (width == 0 or height == 0) return; 2059 2060 const offset_x: usize = (width -| child_grid.size.width) / 2; 2061 const offset_y: usize = (height -| child_grid.size.height) / 2; 2062 2063 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2064 errdefer grid.deinit(); 2065 try grid.drawGrid(child_grid, offset_x, offset_y); 2066 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2067 self.getFocus().child_id = self.child.getFocus().id; 2068 2069 self.grid = grid; 2070 } 2071 2072 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2073 try self.child.input(allocator, key, root_focus); 2074 } 2075 2076 pub fn clearGrid(self: *Center) void { 2077 if (self.grid) |*grid| { 2078 grid.deinit(); 2079 self.grid = null; 2080 } 2081 self.child.clearGrid(); 2082 } 2083 2084 pub fn getGrid(self: Center) ?Grid { 2085 return self.grid; 2086 } 2087 2088 pub fn getFocus(self: *Center) *Focus { 2089 return self.focus; 2090 } 2091 }; 2092 2093 // renders truecolor ANSI art 2094 pub const AnsiArt = struct { 2095 focus: *Focus, 2096 grid: ?Grid, 2097 content: []const u8, 2098 2099 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2100 return .{ 2101 .focus = try Focus.create(allocator, .container), 2102 .grid = null, 2103 .content = content, 2104 }; 2105 } 2106 2107 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2108 self.focus.destroy(allocator); 2109 if (self.grid) |*grid| { 2110 grid.deinit(); 2111 self.grid = null; 2112 } 2113 } 2114 2115 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2116 _ = root_focus; 2117 self.clearGrid(); 2118 2119 // parse into rows of cells, then copy into a rectangular grid 2120 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2121 defer { 2122 for (rows.items) |*row| row.deinit(allocator); 2123 rows.deinit(allocator); 2124 } 2125 var row: std.ArrayList(Grid.Cell) = .empty; 2126 errdefer row.deinit(allocator); 2127 2128 var style: Grid.Style = .{}; 2129 var width: usize = 0; 2130 const content = self.content; 2131 var i: usize = 0; 2132 while (i < content.len) { 2133 const byte = content[i]; 2134 if (byte == '\n') { 2135 width = @max(width, row.items.len); 2136 try rows.append(allocator, row); 2137 row = .empty; 2138 i += 1; 2139 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2140 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2141 var j = i + 2; 2142 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2143 if (j >= content.len) { 2144 i = content.len; // malformed trailing escape; stop 2145 } else { 2146 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2147 i = j + 1; 2148 } 2149 } else { 2150 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2151 const end = @min(content.len, i + len); 2152 const rune = content[i..end]; 2153 const transparent = end - i == 1 and rune[0] == ' ' and 2154 style.fg == null and style.bg == null and !style.inverted; 2155 try row.append(allocator, .{ 2156 .rune = if (transparent) null else rune, 2157 .style = style, 2158 }); 2159 i = end; 2160 } 2161 } 2162 // a trailing row with content but no closing newline 2163 if (row.items.len > 0) { 2164 width = @max(width, row.items.len); 2165 try rows.append(allocator, row); 2166 } else { 2167 row.deinit(allocator); 2168 } 2169 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2170 2171 const height = rows.items.len; 2172 if (width == 0 or height == 0) return; 2173 2174 const clamped_w = @min(width, constraint.max_size.width orelse width); 2175 const clamped_h = @min(height, constraint.max_size.height orelse height); 2176 2177 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2178 errdefer grid.deinit(); 2179 for (rows.items[0..clamped_h], 0..) |r, y| { 2180 const n = @min(r.items.len, clamped_w); 2181 for (r.items[0..n], 0..) |cell, x| { 2182 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2183 } 2184 } 2185 self.grid = grid; 2186 } 2187 2188 pub fn clearGrid(self: *AnsiArt) void { 2189 if (self.grid) |*grid| { 2190 grid.deinit(); 2191 self.grid = null; 2192 } 2193 } 2194 2195 pub fn getGrid(self: AnsiArt) ?Grid { 2196 return self.grid; 2197 } 2198 2199 pub fn getFocus(self: *AnsiArt) *Focus { 2200 return self.focus; 2201 } 2202 2203 fn applySgr(style: *Grid.Style, params: []const u8) void { 2204 var nums: [16]u32 = undefined; 2205 var n: usize = 0; 2206 var it = std.mem.splitScalar(u8, params, ';'); 2207 while (it.next()) |tok| { 2208 if (n >= nums.len) break; 2209 // an empty parameter (e.g. bare "\x1b[m") means 0 2210 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2211 n += 1; 2212 } 2213 if (n == 0) { 2214 style.* = .{}; 2215 return; 2216 } 2217 var i: usize = 0; 2218 while (i < n) : (i += 1) { 2219 switch (nums[i]) { 2220 0 => style.* = .{}, 2221 7 => style.inverted = true, 2222 27 => style.inverted = false, 2223 39 => style.fg = null, 2224 49 => style.bg = null, 2225 38, 48 => { 2226 // truecolor form: 38;2;r;g;b — anything else is ignored 2227 if (i + 4 < n and nums[i + 1] == 2) { 2228 const c = Grid.Color{ 2229 .r = @truncate(nums[i + 2]), 2230 .g = @truncate(nums[i + 3]), 2231 .b = @truncate(nums[i + 4]), 2232 }; 2233 if (nums[i] == 38) style.fg = c else style.bg = c; 2234 i += 4; 2235 } 2236 }, 2237 else => {}, 2238 } 2239 } 2240 } 2241 }; 2242 2243 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2244 pub const AnsiBackground = struct { 2245 grid: ?Grid, 2246 child: *Widget, 2247 art: AnsiArt, 2248 session: *Session, 2249 2250 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2251 var cw = child_widget; 2252 const child = allocator.create(Widget) catch |e| { 2253 cw.deinit(allocator); 2254 return e; 2255 }; 2256 child.* = cw; 2257 errdefer { 2258 child.deinit(allocator); 2259 allocator.destroy(child); 2260 } 2261 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2262 } 2263 2264 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2265 if (self.grid) |*grid| grid.deinit(); 2266 self.art.deinit(allocator); 2267 self.child.deinit(allocator); 2268 allocator.destroy(self.child); 2269 } 2270 2271 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2272 self.clearGrid(); 2273 2274 // make the wrapped page fill the whole available area 2275 try self.child.build(allocator, .{ 2276 .min_size = .{ 2277 .width = constraint.max_size.width orelse constraint.min_size.width, 2278 .height = constraint.max_size.height orelse constraint.min_size.height, 2279 }, 2280 .max_size = constraint.max_size, 2281 }, root_focus); 2282 2283 if (self.session.data.enable_ansi) { 2284 if (self.child.getGrid()) |fg| { 2285 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2286 } 2287 } 2288 } 2289 2290 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2291 try self.child.input(allocator, key, root_focus); 2292 } 2293 2294 pub fn clearGrid(self: *AnsiBackground) void { 2295 if (self.grid) |*grid| { 2296 grid.deinit(); 2297 self.grid = null; 2298 } 2299 self.child.clearGrid(); 2300 self.art.clearGrid(); 2301 } 2302 2303 pub fn getGrid(self: AnsiBackground) ?Grid { 2304 return self.grid orelse self.child.getGrid(); 2305 } 2306 2307 pub fn getFocus(self: *AnsiBackground) *Focus { 2308 return self.child.getFocus(); 2309 } 2310 2311 // which top corner the art hugs. 2312 const ArtAnchor = enum { top_left, top_right }; 2313 2314 // a foreground cell counts as blank if it's empty or a bare space without styling 2315 fn cellIsBlank(cell: Grid.Cell) bool { 2316 if (cell.rune) |rune| { 2317 return std.mem.eql(u8, rune, " ") and 2318 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2319 } 2320 return true; 2321 } 2322 2323 // the art is dimmed to this fraction of its brightness so foreground text 2324 // stays legible over it 2325 const art_brightness = 45; // percent 2326 2327 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2328 const c = color orelse return null; 2329 return .{ 2330 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2331 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2332 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2333 }; 2334 } 2335 2336 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2337 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2338 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2339 // snap such channels clear of that range so the backdrop doesn't blink or 2340 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2341 // actually render truecolor. 2342 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2343 const c = color orelse return null; 2344 const snap = struct { 2345 fn f(v: u8) u8 { 2346 return if (v >= 1 and v <= 9) 10 else v; 2347 } 2348 }.f; 2349 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2350 } 2351 2352 // composites ANSI art behind a foreground grid 2353 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2354 art.clearGrid(); 2355 try art.build(allocator, .{ 2356 .min_size = .{ .width = null, .height = null }, 2357 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2358 }, root_focus); 2359 2360 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2361 errdefer out.deinit(); 2362 2363 if (art.getGrid()) |art_grid| { 2364 const anchor_x = switch (anchor) { 2365 .top_left => 0, 2366 .top_right => foreground.size.width -| art_grid.size.width, 2367 }; 2368 for (0..art_grid.size.height) |y| { 2369 for (0..art_grid.size.width) |x| { 2370 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2371 if (src.rune == null) continue; 2372 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2373 const dst = &out.cells.items[idx]; 2374 // composite the dimmed art behind every cell so it shows 2375 // through the whole UI: a blank cell becomes the art, while a 2376 // cell with a glyph keeps the glyph but takes the dimmed art as 2377 // its background (replacing whatever background it had, which 2378 // would otherwise obscure the art). 2379 if (cellIsBlank(dst.*)) { 2380 dst.* = src; 2381 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2382 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2383 } else { 2384 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2385 } 2386 } 2387 } 2388 } 2389 return out; 2390 } 2391 }; 2392