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