╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1885 1886 var grid = try Grid.init(allocator, .{ .width = total_width, .height = total_height }); 1887 errdefer grid.deinit(); 1888 1889 for (self.text_boxes.items, self.rects.items) |*tb, rect| { 1890 const tb_grid = tb.getGrid() orelse continue; 1891 try self.focus.addChild(allocator, tb.getFocus(), tb_grid.size, @intCast(rect.x), @intCast(rect.y)); 1892 try grid.drawGrid(tb_grid, @intCast(rect.x), @intCast(rect.y)); 1893 } 1894 1895 self.grid = grid; 1896 } 1897 1898 pub fn input(self: *TagFlow, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1899 _ = self; 1900 _ = allocator; 1901 _ = key; 1902 _ = root_focus; 1903 } 1904 1905 pub fn clearGrid(self: *TagFlow) void { 1906 if (self.grid) |*grid| { 1907 grid.deinit(); 1908 self.grid = null; 1909 } 1910 for (self.text_boxes.items) |*tb| tb.clearGrid(); 1911 } 1912 1913 pub fn getGrid(self: TagFlow) ?Grid { 1914 return self.grid; 1915 } 1916 1917 pub fn getFocus(self: *TagFlow) *Focus { 1918 return self.focus; 1919 } 1920 1921 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 1922 for (self.text_boxes.items, 0..) |tb, i| { 1923 if (tb.box.focus.id == focus_id) return i; 1924 } 1925 return null; 1926 } 1927 1928 // the first item of the row below (when `downward`) or above, or null at 1929 // the flow's edge. items are laid out in order, so y is nondecreasing. 1930 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 1931 const rects = self.rects.items; 1932 if (index >= rects.len) return null; 1933 const cur_y = rects[index].y; 1934 if (downward) { 1935 for (rects[index + 1 ..], index + 1..) |r, i| { 1936 if (r.y > cur_y) return i; 1937 } 1938 return null; 1939 } 1940 // walk the earlier rows; the last one visited is the row above. 1941 var prev_first: ?usize = null; 1942 var i: usize = 0; 1943 while (i < rects.len and rects[i].y < cur_y) { 1944 prev_first = i; 1945 const y = rects[i].y; 1946 while (i < rects.len and rects[i].y == y) i += 1; 1947 } 1948 return prev_first; 1949 } 1950 }; 1951 1952 // an invisible widget that fills the horizontal space granted by its parent. 1953 // used inside Box(horiz) with a min_size so the box reserves space for the 1954 // children that follow, pushing them to the right. 1955 pub const Spacer = struct { 1956 focus: *Focus, 1957 grid: ?Grid, 1958 1959 pub fn init(allocator: std.mem.Allocator) !Spacer { 1960 return .{ 1961 .focus = try Focus.create(allocator, .container), 1962 .grid = null, 1963 }; 1964 } 1965 1966 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1967 self.focus.destroy(allocator); 1968 if (self.grid) |*grid| { 1969 grid.deinit(); 1970 self.grid = null; 1971 } 1972 } 1973 1974 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1975 _ = root_focus; 1976 self.clearGrid(); 1977 const width = constraint.max_size.width orelse return; 1978 if (width == 0) return; 1979 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1980 } 1981 1982 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1983 _ = self; 1984 _ = allocator; 1985 _ = key; 1986 _ = root_focus; 1987 } 1988 1989 pub fn clearGrid(self: *Spacer) void { 1990 if (self.grid) |*grid| { 1991 grid.deinit(); 1992 self.grid = null; 1993 } 1994 } 1995 1996 pub fn getGrid(self: Spacer) ?Grid { 1997 return self.grid; 1998 } 1999 2000 pub fn getFocus(self: *Spacer) *Focus { 2001 return self.focus; 2002 } 2003 }; 2004 2005 // a one-row, non-focusable status bar showing the current page's url 2006 pub const Footer = struct { 2007 focus: *Focus, 2008 grid: ?Grid, 2009 session: *Session, 2010 arena: std.heap.ArenaAllocator, 2011 2012 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2013 return .{ 2014 .focus = try Focus.create(allocator, .container), 2015 .grid = null, 2016 .session = session, 2017 .arena = std.heap.ArenaAllocator.init(allocator), 2018 }; 2019 } 2020 2021 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2022 self.focus.destroy(allocator); 2023 if (self.grid) |*grid| grid.deinit(); 2024 self.arena.deinit(); 2025 } 2026 2027 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2028 _ = root_focus; 2029 self.clearGrid(); 2030 const width = constraint.max_size.width orelse return; 2031 if (width == 0) return; 2032 2033 _ = self.arena.reset(.retain_capacity); 2034 const aa = self.arena.allocator(); 2035 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2036 const text = if (self.session.web_port) |port| 2037 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2038 else 2039 path; 2040 2041 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2042 errdefer grid.deinit(); 2043 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2044 var i: usize = 0; 2045 while (utf8.nextCodepointSlice()) |ch| { 2046 if (i == width) break; 2047 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2048 i += 1; 2049 } 2050 self.grid = grid; 2051 } 2052 2053 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2054 _ = self; 2055 _ = allocator; 2056 _ = key; 2057 _ = root_focus; 2058 } 2059 2060 pub fn clearGrid(self: *Footer) void { 2061 if (self.grid) |*grid| { 2062 grid.deinit(); 2063 self.grid = null; 2064 } 2065 } 2066 2067 pub fn getGrid(self: Footer) ?Grid { 2068 return self.grid; 2069 } 2070 2071 pub fn getFocus(self: *Footer) *Focus { 2072 return self.focus; 2073 } 2074 }; 2075 2076 // a single-child wrapper that builds the child at its natural size and 2077 // positions its grid in the middle of the area granted by the parent 2078 // a bordered label, indented past the content it precedes 2079 pub const SectionLabel = struct { 2080 box: wgt.Box(Widget), 2081 2082 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2083 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2084 errdefer box.deinit(allocator); 2085 2086 { 2087 var gap = try wgt.Text(Widget).init(allocator, " "); 2088 errdefer gap.deinit(allocator); 2089 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2090 } 2091 2092 { 2093 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2094 errdefer tb.deinit(allocator); 2095 tb.getFocus().focusable = true; 2096 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2097 } 2098 2099 box.getFocus().child_id = box.children.keys()[1]; 2100 return .{ .box = box }; 2101 } 2102 2103 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2104 self.box.deinit(allocator); 2105 } 2106 2107 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2108 try self.box.build(allocator, constraint, root_focus); 2109 } 2110 2111 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2112 _ = self; 2113 _ = allocator; 2114 _ = key; 2115 _ = root_focus; 2116 } 2117 2118 pub fn clearGrid(self: *SectionLabel) void { 2119 self.box.clearGrid(); 2120 } 2121 2122 pub fn getGrid(self: SectionLabel) ?Grid { 2123 return self.box.getGrid(); 2124 } 2125 2126 pub fn getFocus(self: *SectionLabel) *Focus { 2127 return self.box.getFocus(); 2128 } 2129 }; 2130 2131 // a submit button, indented past its form's content. the web overlay POSTs 2132 // the button's form; terminal hosts detect presses through `buttonId`. 2133 pub const SubmitButton = struct { 2134 box: wgt.Box(Widget), 2135 2136 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2137 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2138 errdefer box.deinit(allocator); 2139 2140 { 2141 var gap = try wgt.Text(Widget).init(allocator, " "); 2142 errdefer gap.deinit(allocator); 2143 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2144 } 2145 2146 { 2147 const label = "submit"; 2148 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2149 errdefer button.deinit(allocator); 2150 button.getFocus().focusable = true; 2151 // the renderer distinguishes plain clickables from buttons that 2152 // should POST to a server route by this kind. 2153 button.getFocus().kind = .{ .custom = "submit" }; 2154 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2155 } 2156 2157 box.getFocus().child_id = box.children.keys()[1]; 2158 return .{ .box = box }; 2159 } 2160 2161 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2162 self.box.deinit(allocator); 2163 } 2164 2165 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2166 try self.box.build(allocator, constraint, root_focus); 2167 } 2168 2169 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2170 _ = self; 2171 _ = allocator; 2172 _ = key; 2173 _ = root_focus; 2174 } 2175 2176 pub fn clearGrid(self: *SubmitButton) void { 2177 self.box.clearGrid(); 2178 } 2179 2180 pub fn getGrid(self: SubmitButton) ?Grid { 2181 return self.box.getGrid(); 2182 } 2183 2184 pub fn getFocus(self: *SubmitButton) *Focus { 2185 return self.box.getFocus(); 2186 } 2187 2188 // the button's focus id, for click hit-testing 2189 pub fn buttonId(self: *const SubmitButton) usize { 2190 return self.box.children.keys()[1]; 2191 } 2192 }; 2193 2194 pub const Center = struct { 2195 focus: *Focus, 2196 grid: ?Grid, 2197 child: *Widget, 2198 2199 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2200 const child = try allocator.create(Widget); 2201 errdefer allocator.destroy(child); 2202 child.* = child_widget; 2203 return .{ 2204 .focus = try Focus.create(allocator, .container), 2205 .grid = null, 2206 .child = child, 2207 }; 2208 } 2209 2210 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2211 self.focus.destroy(allocator); 2212 if (self.grid) |*grid| { 2213 grid.deinit(); 2214 self.grid = null; 2215 } 2216 self.child.deinit(allocator); 2217 allocator.destroy(self.child); 2218 } 2219 2220 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2221 self.clearGrid(); 2222 self.getFocus().clear(); 2223 2224 // build the child without forcing it to fill min_size; it sizes 2225 // itself to its content within the available max. 2226 try self.child.build(allocator, .{ 2227 .min_size = .{ .width = null, .height = null }, 2228 .max_size = constraint.max_size, 2229 }, root_focus); 2230 2231 const child_grid = self.child.getGrid() orelse return; 2232 2233 // prefer max when given; otherwise grow to max(min, child) so that 2234 // when only a min is set (e.g. the wasm path passes viewport rows 2235 // as min_height with no max), we can still vertically center while 2236 // letting taller content extend past the min. 2237 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; 2238 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; 2239 if (width == 0 or height == 0) return; 2240 2241 const offset_x: usize = (width -| child_grid.size.width) / 2; 2242 const offset_y: usize = (height -| child_grid.size.height) / 2; 2243 2244 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2245 errdefer grid.deinit(); 2246 try grid.drawGrid(child_grid, offset_x, offset_y); 2247 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2248 self.getFocus().child_id = self.child.getFocus().id; 2249 2250 self.grid = grid; 2251 } 2252 2253 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2254 try self.child.input(allocator, key, root_focus); 2255 } 2256 2257 pub fn clearGrid(self: *Center) void { 2258 if (self.grid) |*grid| { 2259 grid.deinit(); 2260 self.grid = null; 2261 } 2262 self.child.clearGrid(); 2263 } 2264 2265 pub fn getGrid(self: Center) ?Grid { 2266 return self.grid; 2267 } 2268 2269 pub fn getFocus(self: *Center) *Focus { 2270 return self.focus; 2271 } 2272 }; 2273 2274 // renders truecolor ANSI art 2275 pub const AnsiArt = struct { 2276 focus: *Focus, 2277 grid: ?Grid, 2278 content: []const u8, 2279 2280 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2281 return .{ 2282 .focus = try Focus.create(allocator, .container), 2283 .grid = null, 2284 .content = content, 2285 }; 2286 } 2287 2288 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2289 self.focus.destroy(allocator); 2290 if (self.grid) |*grid| { 2291 grid.deinit(); 2292 self.grid = null; 2293 } 2294 } 2295 2296 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2297 _ = root_focus; 2298 self.clearGrid(); 2299 2300 // parse into rows of cells, then copy into a rectangular grid 2301 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2302 defer { 2303 for (rows.items) |*row| row.deinit(allocator); 2304 rows.deinit(allocator); 2305 } 2306 var row: std.ArrayList(Grid.Cell) = .empty; 2307 errdefer row.deinit(allocator); 2308 2309 var style: Grid.Style = .{}; 2310 var width: usize = 0; 2311 const content = self.content; 2312 var i: usize = 0; 2313 while (i < content.len) { 2314 const byte = content[i]; 2315 if (byte == '\n') { 2316 width = @max(width, row.items.len); 2317 try rows.append(allocator, row); 2318 row = .empty; 2319 i += 1; 2320 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2321 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2322 var j = i + 2; 2323 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2324 if (j >= content.len) { 2325 i = content.len; // malformed trailing escape; stop 2326 } else { 2327 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2328 i = j + 1; 2329 } 2330 } else { 2331 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2332 const end = @min(content.len, i + len); 2333 const rune = content[i..end]; 2334 const transparent = end - i == 1 and rune[0] == ' ' and 2335 style.fg == null and style.bg == null and !style.inverted; 2336 try row.append(allocator, .{ 2337 .rune = if (transparent) null else rune, 2338 .style = style, 2339 }); 2340 i = end; 2341 } 2342 } 2343 // a trailing row with content but no closing newline 2344 if (row.items.len > 0) { 2345 width = @max(width, row.items.len); 2346 try rows.append(allocator, row); 2347 } else { 2348 row.deinit(allocator); 2349 } 2350 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2351 2352 const height = rows.items.len; 2353 if (width == 0 or height == 0) return; 2354 2355 const clamped_w = @min(width, constraint.max_size.width orelse width); 2356 const clamped_h = @min(height, constraint.max_size.height orelse height); 2357 2358 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2359 errdefer grid.deinit(); 2360 for (rows.items[0..clamped_h], 0..) |r, y| { 2361 const n = @min(r.items.len, clamped_w); 2362 for (r.items[0..n], 0..) |cell, x| { 2363 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2364 } 2365 } 2366 self.grid = grid; 2367 } 2368 2369 pub fn clearGrid(self: *AnsiArt) void { 2370 if (self.grid) |*grid| { 2371 grid.deinit(); 2372 self.grid = null; 2373 } 2374 } 2375 2376 pub fn getGrid(self: AnsiArt) ?Grid { 2377 return self.grid; 2378 } 2379 2380 pub fn getFocus(self: *AnsiArt) *Focus { 2381 return self.focus; 2382 } 2383 2384 fn applySgr(style: *Grid.Style, params: []const u8) void { 2385 var nums: [16]u32 = undefined; 2386 var n: usize = 0; 2387 var it = std.mem.splitScalar(u8, params, ';'); 2388 while (it.next()) |tok| { 2389 if (n >= nums.len) break; 2390 // an empty parameter (e.g. bare "\x1b[m") means 0 2391 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2392 n += 1; 2393 } 2394 if (n == 0) { 2395 style.* = .{}; 2396 return; 2397 } 2398 var i: usize = 0; 2399 while (i < n) : (i += 1) { 2400 switch (nums[i]) { 2401 0 => style.* = .{}, 2402 7 => style.inverted = true, 2403 27 => style.inverted = false, 2404 39 => style.fg = null, 2405 49 => style.bg = null, 2406 38, 48 => { 2407 // truecolor form: 38;2;r;g;b — anything else is ignored 2408 if (i + 4 < n and nums[i + 1] == 2) { 2409 const c = Grid.Color{ 2410 .r = @truncate(nums[i + 2]), 2411 .g = @truncate(nums[i + 3]), 2412 .b = @truncate(nums[i + 4]), 2413 }; 2414 if (nums[i] == 38) style.fg = c else style.bg = c; 2415 i += 4; 2416 } 2417 }, 2418 else => {}, 2419 } 2420 } 2421 } 2422 }; 2423 2424 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2425 pub const AnsiBackground = struct { 2426 grid: ?Grid, 2427 child: *Widget, 2428 art: AnsiArt, 2429 session: *Session, 2430 2431 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2432 var cw = child_widget; 2433 const child = allocator.create(Widget) catch |e| { 2434 cw.deinit(allocator); 2435 return e; 2436 }; 2437 child.* = cw; 2438 errdefer { 2439 child.deinit(allocator); 2440 allocator.destroy(child); 2441 } 2442 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2443 } 2444 2445 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2446 if (self.grid) |*grid| grid.deinit(); 2447 self.art.deinit(allocator); 2448 self.child.deinit(allocator); 2449 allocator.destroy(self.child); 2450 } 2451 2452 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2453 self.clearGrid(); 2454 2455 // make the wrapped page fill the whole available area 2456 try self.child.build(allocator, .{ 2457 .min_size = .{ 2458 .width = constraint.max_size.width orelse constraint.min_size.width, 2459 .height = constraint.max_size.height orelse constraint.min_size.height, 2460 }, 2461 .max_size = constraint.max_size, 2462 }, root_focus); 2463 2464 if (self.session.data.enable_ansi) { 2465 if (self.child.getGrid()) |fg| { 2466 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2467 } 2468 } 2469 } 2470 2471 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2472 try self.child.input(allocator, key, root_focus); 2473 } 2474 2475 pub fn clearGrid(self: *AnsiBackground) void { 2476 if (self.grid) |*grid| { 2477 grid.deinit(); 2478 self.grid = null; 2479 } 2480 self.child.clearGrid(); 2481 self.art.clearGrid(); 2482 } 2483 2484 pub fn getGrid(self: AnsiBackground) ?Grid { 2485 return self.grid orelse self.child.getGrid(); 2486 } 2487 2488 pub fn getFocus(self: *AnsiBackground) *Focus { 2489 return self.child.getFocus(); 2490 } 2491 2492 // which top corner the art hugs. 2493 const ArtAnchor = enum { top_left, top_right }; 2494 2495 // a foreground cell counts as blank if it's empty or a bare space without styling 2496 fn cellIsBlank(cell: Grid.Cell) bool { 2497 if (cell.rune) |rune| { 2498 return std.mem.eql(u8, rune, " ") and 2499 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2500 } 2501 return true; 2502 } 2503 2504 // the art is dimmed to this fraction of its brightness so foreground text 2505 // stays legible over it 2506 const art_brightness = 45; // percent 2507 2508 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2509 const c = color orelse return null; 2510 return .{ 2511 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2512 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2513 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2514 }; 2515 } 2516 2517 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2518 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2519 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2520 // snap such channels clear of that range so the backdrop doesn't blink or 2521 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2522 // actually render truecolor. 2523 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2524 const c = color orelse return null; 2525 const snap = struct { 2526 fn f(v: u8) u8 { 2527 return if (v >= 1 and v <= 9) 10 else v; 2528 } 2529 }.f; 2530 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2531 } 2532 2533 // composites ANSI art behind a foreground grid 2534 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2535 art.clearGrid(); 2536 try art.build(allocator, .{ 2537 .min_size = .{ .width = null, .height = null }, 2538 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2539 }, root_focus); 2540 2541 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2542 errdefer out.deinit(); 2543 2544 if (art.getGrid()) |art_grid| { 2545 const anchor_x = switch (anchor) { 2546 .top_left => 0, 2547 .top_right => foreground.size.width -| art_grid.size.width, 2548 }; 2549 for (0..art_grid.size.height) |y| { 2550 for (0..art_grid.size.width) |x| { 2551 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2552 if (src.rune == null) continue; 2553 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2554 const dst = &out.cells.items[idx]; 2555 // composite the dimmed art behind every cell so it shows 2556 // through the whole UI: a blank cell becomes the art, while a 2557 // cell with a glyph keeps the glyph but takes the dimmed art as 2558 // its background (replacing whatever background it had, which 2559 // would otherwise obscure the art). 2560 if (cellIsBlank(dst.*)) { 2561 dst.* = src; 2562 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2563 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2564 } else { 2565 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2566 } 2567 } 2568 } 2569 } 2570 return out; 2571 } 2572 }; 2573