╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2090 } 2091 2092 pub fn getFocus(self: *TagFlow) *Focus { 2093 return self.focus; 2094 } 2095 2096 pub fn indexOfFocusId(self: TagFlow, focus_id: usize) ?usize { 2097 for (self.text_boxes.items, 0..) |tb, i| { 2098 if (tb.box.focus.id == focus_id) return i; 2099 } 2100 return null; 2101 } 2102 2103 // the first item of the row below (when `downward`) or above, or null at 2104 // the flow's edge. items are laid out in order, so y is nondecreasing. 2105 pub fn rowStep(self: TagFlow, index: usize, downward: bool) ?usize { 2106 const rects = self.rects.items; 2107 if (index >= rects.len) return null; 2108 const cur_y = rects[index].y; 2109 if (downward) { 2110 for (rects[index + 1 ..], index + 1..) |r, i| { 2111 if (r.y > cur_y) return i; 2112 } 2113 return null; 2114 } 2115 // walk the earlier rows; the last one visited is the row above. 2116 var prev_first: ?usize = null; 2117 var i: usize = 0; 2118 while (i < rects.len and rects[i].y < cur_y) { 2119 prev_first = i; 2120 const y = rects[i].y; 2121 while (i < rects.len and rects[i].y == y) i += 1; 2122 } 2123 return prev_first; 2124 } 2125 }; 2126 2127 // an invisible widget that fills the horizontal space granted by its parent. 2128 // used inside Box(horiz) with a min_size so the box reserves space for the 2129 // children that follow, pushing them to the right. 2130 pub const Spacer = struct { 2131 focus: *Focus, 2132 grid: ?Grid, 2133 2134 pub fn init(allocator: std.mem.Allocator) !Spacer { 2135 return .{ 2136 .focus = try Focus.create(allocator, .container), 2137 .grid = null, 2138 }; 2139 } 2140 2141 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 2142 self.focus.destroy(allocator); 2143 if (self.grid) |*grid| { 2144 grid.deinit(); 2145 self.grid = null; 2146 } 2147 } 2148 2149 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2150 _ = root_focus; 2151 self.clearGrid(); 2152 const width = constraint.max_size.width orelse return; 2153 if (width == 0) return; 2154 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2155 } 2156 2157 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2158 _ = self; 2159 _ = allocator; 2160 _ = key; 2161 _ = root_focus; 2162 } 2163 2164 pub fn clearGrid(self: *Spacer) void { 2165 if (self.grid) |*grid| { 2166 grid.deinit(); 2167 self.grid = null; 2168 } 2169 } 2170 2171 pub fn getGrid(self: Spacer) ?Grid { 2172 return self.grid; 2173 } 2174 2175 pub fn getFocus(self: *Spacer) *Focus { 2176 return self.focus; 2177 } 2178 }; 2179 2180 // a one-row, non-focusable status bar showing the current page's url 2181 pub const Footer = struct { 2182 focus: *Focus, 2183 grid: ?Grid, 2184 session: *Session, 2185 arena: std.heap.ArenaAllocator, 2186 url: []const u8 = "", 2187 url_width: usize = 0, 2188 2189 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 2190 return .{ 2191 .focus = try Focus.create(allocator, .container), 2192 .grid = null, 2193 .session = session, 2194 .arena = std.heap.ArenaAllocator.init(allocator), 2195 }; 2196 } 2197 2198 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 2199 self.focus.destroy(allocator); 2200 if (self.grid) |*grid| grid.deinit(); 2201 self.arena.deinit(); 2202 } 2203 2204 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2205 _ = root_focus; 2206 self.clearGrid(); 2207 const width = constraint.max_size.width orelse return; 2208 if (width == 0) return; 2209 2210 _ = self.arena.reset(.retain_capacity); 2211 self.url = ""; 2212 self.url_width = 0; 2213 const aa = self.arena.allocator(); 2214 const path = self.session.data.current_page.toUrl(&self.arena) catch return; 2215 const text = if (self.session.web_port) |port| 2216 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 2217 else 2218 path; 2219 self.url = text; 2220 2221 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 2222 errdefer grid.deinit(); 2223 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 2224 var i: usize = 0; 2225 while (utf8.nextCodepointSlice()) |ch| { 2226 if (i == width) break; 2227 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 2228 i += 1; 2229 } 2230 self.url_width = i; 2231 self.grid = grid; 2232 } 2233 2234 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2235 _ = allocator; 2236 switch (key) { 2237 .mouse => |mouse| { 2238 if (mouse.ctrl or !inp.leftClickOn(root_focus, self.getFocus().id, mouse)) return; 2239 const rect = (root_focus.children.get(self.getFocus().id) orelse return).rect; 2240 if (mouse.x < rect.x + self.url_width) self.session.host_request = .{ .show_copyable_text = self.url }; 2241 }, 2242 else => {}, 2243 } 2244 } 2245 2246 pub fn clearGrid(self: *Footer) void { 2247 if (self.grid) |*grid| { 2248 grid.deinit(); 2249 self.grid = null; 2250 } 2251 } 2252 2253 pub fn getGrid(self: Footer) ?Grid { 2254 return self.grid; 2255 } 2256 2257 pub fn getFocus(self: *Footer) *Focus { 2258 return self.focus; 2259 } 2260 }; 2261 2262 // a single-child wrapper that builds the child at its natural size and 2263 // positions its grid in the middle of the area granted by the parent 2264 // a bordered label, indented past the content it precedes 2265 pub const SectionLabel = struct { 2266 box: wgt.Box(Widget), 2267 2268 pub fn init(allocator: std.mem.Allocator, content: []const u8) !SectionLabel { 2269 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2270 errdefer box.deinit(allocator); 2271 2272 { 2273 var gap = try wgt.Text(Widget).init(allocator, " "); 2274 errdefer gap.deinit(allocator); 2275 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2276 } 2277 2278 { 2279 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2280 errdefer tb.deinit(allocator); 2281 tb.getFocus().focusable = true; 2282 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2283 } 2284 2285 box.getFocus().child_id = box.children.keys()[1]; 2286 return .{ .box = box }; 2287 } 2288 2289 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2290 self.box.deinit(allocator); 2291 } 2292 2293 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2294 try self.box.build(allocator, constraint, root_focus); 2295 } 2296 2297 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2298 _ = self; 2299 _ = allocator; 2300 _ = key; 2301 _ = root_focus; 2302 } 2303 2304 pub fn clearGrid(self: *SectionLabel) void { 2305 self.box.clearGrid(); 2306 } 2307 2308 pub fn getGrid(self: SectionLabel) ?Grid { 2309 return self.box.getGrid(); 2310 } 2311 2312 pub fn getFocus(self: *SectionLabel) *Focus { 2313 return self.box.getFocus(); 2314 } 2315 }; 2316 2317 // a submit button, indented past its form's content. the web overlay POSTs 2318 // the button's form; terminal hosts detect presses through `buttonId`. 2319 pub const SubmitButton = struct { 2320 box: wgt.Box(Widget), 2321 2322 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2323 return initLabeled(allocator, "submit"); 2324 } 2325 2326 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2327 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2328 errdefer box.deinit(allocator); 2329 2330 { 2331 var gap = try wgt.Text(Widget).init(allocator, " "); 2332 errdefer gap.deinit(allocator); 2333 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2334 } 2335 2336 { 2337 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2338 errdefer button.deinit(allocator); 2339 button.getFocus().focusable = true; 2340 // the renderer distinguishes plain clickables from buttons that 2341 // should POST to a server route by this kind. 2342 button.getFocus().kind = .{ .custom = "submit" }; 2343 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2344 } 2345 2346 box.getFocus().child_id = box.children.keys()[1]; 2347 return .{ .box = box }; 2348 } 2349 2350 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2351 self.box.deinit(allocator); 2352 } 2353 2354 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2355 try self.box.build(allocator, constraint, root_focus); 2356 } 2357 2358 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2359 _ = self; 2360 _ = allocator; 2361 _ = key; 2362 _ = root_focus; 2363 } 2364 2365 pub fn clearGrid(self: *SubmitButton) void { 2366 self.box.clearGrid(); 2367 } 2368 2369 pub fn getGrid(self: SubmitButton) ?Grid { 2370 return self.box.getGrid(); 2371 } 2372 2373 pub fn getFocus(self: *SubmitButton) *Focus { 2374 return self.box.getFocus(); 2375 } 2376 2377 // the button's focus id, for click hit-testing 2378 pub fn buttonId(self: *const SubmitButton) usize { 2379 return self.box.children.keys()[1]; 2380 } 2381 }; 2382 2383 pub const Center = struct { 2384 focus: *Focus, 2385 grid: ?Grid, 2386 child: *Widget, 2387 2388 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2389 const child = try allocator.create(Widget); 2390 errdefer allocator.destroy(child); 2391 child.* = child_widget; 2392 return .{ 2393 .focus = try Focus.create(allocator, .container), 2394 .grid = null, 2395 .child = child, 2396 }; 2397 } 2398 2399 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2400 self.focus.destroy(allocator); 2401 if (self.grid) |*grid| { 2402 grid.deinit(); 2403 self.grid = null; 2404 } 2405 self.child.deinit(allocator); 2406 allocator.destroy(self.child); 2407 } 2408 2409 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2410 self.clearGrid(); 2411 self.getFocus().clear(); 2412 2413 // build the child without forcing it to fill min_size; it sizes 2414 // itself to its content within the available max. 2415 try self.child.build(allocator, .{ 2416 .min_size = .{ .width = null, .height = null }, 2417 .max_size = constraint.max_size, 2418 }, root_focus); 2419 2420 const child_grid = self.child.getGrid() orelse return; 2421 2422 // prefer max when given; otherwise grow to max(min, child) so that 2423 // when only a min is set (e.g. the wasm path passes viewport rows 2424 // as min_height with no max), we can still vertically center while 2425 // letting taller content extend past the min. 2426 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; 2427 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; 2428 if (width == 0 or height == 0) return; 2429 2430 const offset_x: usize = (width -| child_grid.size.width) / 2; 2431 const offset_y: usize = (height -| child_grid.size.height) / 2; 2432 2433 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2434 errdefer grid.deinit(); 2435 try grid.drawGrid(child_grid, offset_x, offset_y); 2436 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2437 self.getFocus().child_id = self.child.getFocus().id; 2438 2439 self.grid = grid; 2440 } 2441 2442 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2443 try self.child.input(allocator, key, root_focus); 2444 } 2445 2446 pub fn clearGrid(self: *Center) void { 2447 if (self.grid) |*grid| { 2448 grid.deinit(); 2449 self.grid = null; 2450 } 2451 self.child.clearGrid(); 2452 } 2453 2454 pub fn getGrid(self: Center) ?Grid { 2455 return self.grid; 2456 } 2457 2458 pub fn getFocus(self: *Center) *Focus { 2459 return self.focus; 2460 } 2461 }; 2462 2463 // renders truecolor ANSI art 2464 pub const AnsiArt = struct { 2465 focus: *Focus, 2466 grid: ?Grid, 2467 content: []const u8, 2468 2469 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2470 return .{ 2471 .focus = try Focus.create(allocator, .container), 2472 .grid = null, 2473 .content = content, 2474 }; 2475 } 2476 2477 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2478 self.focus.destroy(allocator); 2479 if (self.grid) |*grid| { 2480 grid.deinit(); 2481 self.grid = null; 2482 } 2483 } 2484 2485 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2486 _ = root_focus; 2487 self.clearGrid(); 2488 2489 // parse into rows of cells, then copy into a rectangular grid 2490 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2491 defer { 2492 for (rows.items) |*row| row.deinit(allocator); 2493 rows.deinit(allocator); 2494 } 2495 var row: std.ArrayList(Grid.Cell) = .empty; 2496 errdefer row.deinit(allocator); 2497 2498 var style: Grid.Style = .{}; 2499 var width: usize = 0; 2500 const content = self.content; 2501 var i: usize = 0; 2502 while (i < content.len) { 2503 const byte = content[i]; 2504 if (byte == '\n') { 2505 width = @max(width, row.items.len); 2506 try rows.append(allocator, row); 2507 row = .empty; 2508 i += 1; 2509 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2510 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2511 var j = i + 2; 2512 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2513 if (j >= content.len) { 2514 i = content.len; // malformed trailing escape; stop 2515 } else { 2516 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2517 i = j + 1; 2518 } 2519 } else { 2520 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2521 const end = @min(content.len, i + len); 2522 const rune = content[i..end]; 2523 const transparent = end - i == 1 and rune[0] == ' ' and 2524 style.fg == null and style.bg == null and !style.inverted; 2525 try row.append(allocator, .{ 2526 .rune = if (transparent) null else rune, 2527 .style = style, 2528 }); 2529 i = end; 2530 } 2531 } 2532 // a trailing row with content but no closing newline 2533 if (row.items.len > 0) { 2534 width = @max(width, row.items.len); 2535 try rows.append(allocator, row); 2536 } else { 2537 row.deinit(allocator); 2538 } 2539 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2540 2541 const height = rows.items.len; 2542 if (width == 0 or height == 0) return; 2543 2544 const clamped_w = @min(width, constraint.max_size.width orelse width); 2545 const clamped_h = @min(height, constraint.max_size.height orelse height); 2546 2547 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2548 errdefer grid.deinit(); 2549 for (rows.items[0..clamped_h], 0..) |r, y| { 2550 const n = @min(r.items.len, clamped_w); 2551 for (r.items[0..n], 0..) |cell, x| { 2552 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2553 } 2554 } 2555 self.grid = grid; 2556 } 2557 2558 pub fn clearGrid(self: *AnsiArt) void { 2559 if (self.grid) |*grid| { 2560 grid.deinit(); 2561 self.grid = null; 2562 } 2563 } 2564 2565 pub fn getGrid(self: AnsiArt) ?Grid { 2566 return self.grid; 2567 } 2568 2569 pub fn getFocus(self: *AnsiArt) *Focus { 2570 return self.focus; 2571 } 2572 2573 fn applySgr(style: *Grid.Style, params: []const u8) void { 2574 var nums: [16]u32 = undefined; 2575 var n: usize = 0; 2576 var it = std.mem.splitScalar(u8, params, ';'); 2577 while (it.next()) |tok| { 2578 if (n >= nums.len) break; 2579 // an empty parameter (e.g. bare "\x1b[m") means 0 2580 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2581 n += 1; 2582 } 2583 if (n == 0) { 2584 style.* = .{}; 2585 return; 2586 } 2587 var i: usize = 0; 2588 while (i < n) : (i += 1) { 2589 switch (nums[i]) { 2590 0 => style.* = .{}, 2591 7 => style.inverted = true, 2592 27 => style.inverted = false, 2593 39 => style.fg = null, 2594 49 => style.bg = null, 2595 38, 48 => { 2596 // truecolor form: 38;2;r;g;b — anything else is ignored 2597 if (i + 4 < n and nums[i + 1] == 2) { 2598 const c = Grid.Color{ 2599 .r = @truncate(nums[i + 2]), 2600 .g = @truncate(nums[i + 3]), 2601 .b = @truncate(nums[i + 4]), 2602 }; 2603 if (nums[i] == 38) style.fg = c else style.bg = c; 2604 i += 4; 2605 } 2606 }, 2607 else => {}, 2608 } 2609 } 2610 } 2611 }; 2612 2613 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2614 pub const AnsiBackground = struct { 2615 grid: ?Grid, 2616 child: *Widget, 2617 art: AnsiArt, 2618 session: *Session, 2619 2620 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2621 var cw = child_widget; 2622 const child = allocator.create(Widget) catch |e| { 2623 cw.deinit(allocator); 2624 return e; 2625 }; 2626 child.* = cw; 2627 errdefer { 2628 child.deinit(allocator); 2629 allocator.destroy(child); 2630 } 2631 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2632 } 2633 2634 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2635 if (self.grid) |*grid| grid.deinit(); 2636 self.art.deinit(allocator); 2637 self.child.deinit(allocator); 2638 allocator.destroy(self.child); 2639 } 2640 2641 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2642 self.clearGrid(); 2643 2644 // make the wrapped page fill the whole available area 2645 try self.child.build(allocator, .{ 2646 .min_size = .{ 2647 .width = constraint.max_size.width orelse constraint.min_size.width, 2648 .height = constraint.max_size.height orelse constraint.min_size.height, 2649 }, 2650 .max_size = constraint.max_size, 2651 }, root_focus); 2652 2653 if (self.session.data.enable_ansi) { 2654 if (self.child.getGrid()) |fg| { 2655 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2656 } 2657 } 2658 } 2659 2660 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2661 try self.child.input(allocator, key, root_focus); 2662 } 2663 2664 pub fn clearGrid(self: *AnsiBackground) void { 2665 if (self.grid) |*grid| { 2666 grid.deinit(); 2667 self.grid = null; 2668 } 2669 self.child.clearGrid(); 2670 self.art.clearGrid(); 2671 } 2672 2673 pub fn getGrid(self: AnsiBackground) ?Grid { 2674 return self.grid orelse self.child.getGrid(); 2675 } 2676 2677 pub fn getFocus(self: *AnsiBackground) *Focus { 2678 return self.child.getFocus(); 2679 } 2680 2681 // which top corner the art hugs. 2682 const ArtAnchor = enum { top_left, top_right }; 2683 2684 // a foreground cell counts as blank if it's empty or a bare space without styling 2685 fn cellIsBlank(cell: Grid.Cell) bool { 2686 if (cell.rune) |rune| { 2687 return std.mem.eql(u8, rune, " ") and 2688 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2689 } 2690 return true; 2691 } 2692 2693 // the art is dimmed to this fraction of its brightness so foreground text 2694 // stays legible over it 2695 const art_brightness = 45; // percent 2696 2697 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2698 const c = color orelse return null; 2699 return .{ 2700 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2701 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2702 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2703 }; 2704 } 2705 2706 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2707 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2708 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2709 // snap such channels clear of that range so the backdrop doesn't blink or 2710 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2711 // actually render truecolor. 2712 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2713 const c = color orelse return null; 2714 const snap = struct { 2715 fn f(v: u8) u8 { 2716 return if (v >= 1 and v <= 9) 10 else v; 2717 } 2718 }.f; 2719 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2720 } 2721 2722 // composites ANSI art behind a foreground grid 2723 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2724 art.clearGrid(); 2725 try art.build(allocator, .{ 2726 .min_size = .{ .width = null, .height = null }, 2727 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2728 }, root_focus); 2729 2730 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2731 errdefer out.deinit(); 2732 2733 if (art.getGrid()) |art_grid| { 2734 const anchor_x = switch (anchor) { 2735 .top_left => 0, 2736 .top_right => foreground.size.width -| art_grid.size.width, 2737 }; 2738 for (0..art_grid.size.height) |y| { 2739 for (0..art_grid.size.width) |x| { 2740 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2741 if (src.rune == null) continue; 2742 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2743 const dst = &out.cells.items[idx]; 2744 // composite the dimmed art behind every cell so it shows 2745 // through the whole UI: a blank cell becomes the art, while a 2746 // cell with a glyph keeps the glyph but takes the dimmed art as 2747 // its background (replacing whatever background it had, which 2748 // would otherwise obscure the art). 2749 if (cellIsBlank(dst.*)) { 2750 dst.* = src; 2751 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2752 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2753 } else { 2754 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2755 dst.style.bg = background_maybe; 2756 // change the color of text overlaying ANSI art to ensure 2757 // it is readable. don't do this in the web UI, since we 2758 // have direct control of the theme there (also, inner 2759 // scroll panes allow text to move without a re-render 2760 // by the TUI, which would break this). also don't do this 2761 // if the text is explicitly styled. 2762 if (is_terminal and dst.style.fg == null) { 2763 if (background_maybe) |background| { 2764 // use near-black and near-white, which is easier on the eyes 2765 // while retaining high contrast 2766 const luminance = (@as(u32, background.r) * 299 + 2767 @as(u32, background.g) * 587 + 2768 @as(u32, background.b) * 114) / 1000; 2769 dst.style.fg = if (luminance >= 128) 2770 .{ .r = 16, .g = 16, .b = 16 } 2771 else 2772 .{ .r = 240, .g = 240, .b = 240 }; 2773 } 2774 } 2775 } 2776 } 2777 } 2778 } 2779 return out; 2780 } 2781 }; 2782