╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2173 2174 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2175 _ = self; 2176 _ = allocator; 2177 _ = key; 2178 _ = root_focus; 2179 } 2180 2181 pub fn clearGrid(self: *SectionLabel) void { 2182 self.box.clearGrid(); 2183 } 2184 2185 pub fn getGrid(self: SectionLabel) ?Grid { 2186 return self.box.getGrid(); 2187 } 2188 2189 pub fn getFocus(self: *SectionLabel) *Focus { 2190 return self.box.getFocus(); 2191 } 2192 }; 2193 2194 // a submit button, indented past its form's content. the web overlay POSTs 2195 // the button's form; terminal hosts detect presses through `buttonId`. 2196 pub const SubmitButton = struct { 2197 box: wgt.Box(Widget), 2198 2199 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2200 return initLabeled(allocator, "submit"); 2201 } 2202 2203 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2204 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2205 errdefer box.deinit(allocator); 2206 2207 { 2208 var gap = try wgt.Text(Widget).init(allocator, " "); 2209 errdefer gap.deinit(allocator); 2210 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2211 } 2212 2213 { 2214 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2215 errdefer button.deinit(allocator); 2216 button.getFocus().focusable = true; 2217 // the renderer distinguishes plain clickables from buttons that 2218 // should POST to a server route by this kind. 2219 button.getFocus().kind = .{ .custom = "submit" }; 2220 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2221 } 2222 2223 box.getFocus().child_id = box.children.keys()[1]; 2224 return .{ .box = box }; 2225 } 2226 2227 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2228 self.box.deinit(allocator); 2229 } 2230 2231 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2232 try self.box.build(allocator, constraint, root_focus); 2233 } 2234 2235 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2236 _ = self; 2237 _ = allocator; 2238 _ = key; 2239 _ = root_focus; 2240 } 2241 2242 pub fn clearGrid(self: *SubmitButton) void { 2243 self.box.clearGrid(); 2244 } 2245 2246 pub fn getGrid(self: SubmitButton) ?Grid { 2247 return self.box.getGrid(); 2248 } 2249 2250 pub fn getFocus(self: *SubmitButton) *Focus { 2251 return self.box.getFocus(); 2252 } 2253 2254 // the button's focus id, for click hit-testing 2255 pub fn buttonId(self: *const SubmitButton) usize { 2256 return self.box.children.keys()[1]; 2257 } 2258 }; 2259 2260 pub const Center = struct { 2261 focus: *Focus, 2262 grid: ?Grid, 2263 child: *Widget, 2264 2265 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2266 const child = try allocator.create(Widget); 2267 errdefer allocator.destroy(child); 2268 child.* = child_widget; 2269 return .{ 2270 .focus = try Focus.create(allocator, .container), 2271 .grid = null, 2272 .child = child, 2273 }; 2274 } 2275 2276 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2277 self.focus.destroy(allocator); 2278 if (self.grid) |*grid| { 2279 grid.deinit(); 2280 self.grid = null; 2281 } 2282 self.child.deinit(allocator); 2283 allocator.destroy(self.child); 2284 } 2285 2286 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2287 self.clearGrid(); 2288 self.getFocus().clear(); 2289 2290 // build the child without forcing it to fill min_size; it sizes 2291 // itself to its content within the available max. 2292 try self.child.build(allocator, .{ 2293 .min_size = .{ .width = null, .height = null }, 2294 .max_size = constraint.max_size, 2295 }, root_focus); 2296 2297 const child_grid = self.child.getGrid() orelse return; 2298 2299 // prefer max when given; otherwise grow to max(min, child) so that 2300 // when only a min is set (e.g. the wasm path passes viewport rows 2301 // as min_height with no max), we can still vertically center while 2302 // letting taller content extend past the min. 2303 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; 2304 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; 2305 if (width == 0 or height == 0) return; 2306 2307 const offset_x: usize = (width -| child_grid.size.width) / 2; 2308 const offset_y: usize = (height -| child_grid.size.height) / 2; 2309 2310 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2311 errdefer grid.deinit(); 2312 try grid.drawGrid(child_grid, offset_x, offset_y); 2313 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2314 self.getFocus().child_id = self.child.getFocus().id; 2315 2316 self.grid = grid; 2317 } 2318 2319 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2320 try self.child.input(allocator, key, root_focus); 2321 } 2322 2323 pub fn clearGrid(self: *Center) void { 2324 if (self.grid) |*grid| { 2325 grid.deinit(); 2326 self.grid = null; 2327 } 2328 self.child.clearGrid(); 2329 } 2330 2331 pub fn getGrid(self: Center) ?Grid { 2332 return self.grid; 2333 } 2334 2335 pub fn getFocus(self: *Center) *Focus { 2336 return self.focus; 2337 } 2338 }; 2339 2340 // renders truecolor ANSI art 2341 pub const AnsiArt = struct { 2342 focus: *Focus, 2343 grid: ?Grid, 2344 content: []const u8, 2345 2346 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2347 return .{ 2348 .focus = try Focus.create(allocator, .container), 2349 .grid = null, 2350 .content = content, 2351 }; 2352 } 2353 2354 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2355 self.focus.destroy(allocator); 2356 if (self.grid) |*grid| { 2357 grid.deinit(); 2358 self.grid = null; 2359 } 2360 } 2361 2362 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2363 _ = root_focus; 2364 self.clearGrid(); 2365 2366 // parse into rows of cells, then copy into a rectangular grid 2367 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2368 defer { 2369 for (rows.items) |*row| row.deinit(allocator); 2370 rows.deinit(allocator); 2371 } 2372 var row: std.ArrayList(Grid.Cell) = .empty; 2373 errdefer row.deinit(allocator); 2374 2375 var style: Grid.Style = .{}; 2376 var width: usize = 0; 2377 const content = self.content; 2378 var i: usize = 0; 2379 while (i < content.len) { 2380 const byte = content[i]; 2381 if (byte == '\n') { 2382 width = @max(width, row.items.len); 2383 try rows.append(allocator, row); 2384 row = .empty; 2385 i += 1; 2386 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2387 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2388 var j = i + 2; 2389 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2390 if (j >= content.len) { 2391 i = content.len; // malformed trailing escape; stop 2392 } else { 2393 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2394 i = j + 1; 2395 } 2396 } else { 2397 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2398 const end = @min(content.len, i + len); 2399 const rune = content[i..end]; 2400 const transparent = end - i == 1 and rune[0] == ' ' and 2401 style.fg == null and style.bg == null and !style.inverted; 2402 try row.append(allocator, .{ 2403 .rune = if (transparent) null else rune, 2404 .style = style, 2405 }); 2406 i = end; 2407 } 2408 } 2409 // a trailing row with content but no closing newline 2410 if (row.items.len > 0) { 2411 width = @max(width, row.items.len); 2412 try rows.append(allocator, row); 2413 } else { 2414 row.deinit(allocator); 2415 } 2416 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2417 2418 const height = rows.items.len; 2419 if (width == 0 or height == 0) return; 2420 2421 const clamped_w = @min(width, constraint.max_size.width orelse width); 2422 const clamped_h = @min(height, constraint.max_size.height orelse height); 2423 2424 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2425 errdefer grid.deinit(); 2426 for (rows.items[0..clamped_h], 0..) |r, y| { 2427 const n = @min(r.items.len, clamped_w); 2428 for (r.items[0..n], 0..) |cell, x| { 2429 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2430 } 2431 } 2432 self.grid = grid; 2433 } 2434 2435 pub fn clearGrid(self: *AnsiArt) void { 2436 if (self.grid) |*grid| { 2437 grid.deinit(); 2438 self.grid = null; 2439 } 2440 } 2441 2442 pub fn getGrid(self: AnsiArt) ?Grid { 2443 return self.grid; 2444 } 2445 2446 pub fn getFocus(self: *AnsiArt) *Focus { 2447 return self.focus; 2448 } 2449 2450 fn applySgr(style: *Grid.Style, params: []const u8) void { 2451 var nums: [16]u32 = undefined; 2452 var n: usize = 0; 2453 var it = std.mem.splitScalar(u8, params, ';'); 2454 while (it.next()) |tok| { 2455 if (n >= nums.len) break; 2456 // an empty parameter (e.g. bare "\x1b[m") means 0 2457 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2458 n += 1; 2459 } 2460 if (n == 0) { 2461 style.* = .{}; 2462 return; 2463 } 2464 var i: usize = 0; 2465 while (i < n) : (i += 1) { 2466 switch (nums[i]) { 2467 0 => style.* = .{}, 2468 7 => style.inverted = true, 2469 27 => style.inverted = false, 2470 39 => style.fg = null, 2471 49 => style.bg = null, 2472 38, 48 => { 2473 // truecolor form: 38;2;r;g;b — anything else is ignored 2474 if (i + 4 < n and nums[i + 1] == 2) { 2475 const c = Grid.Color{ 2476 .r = @truncate(nums[i + 2]), 2477 .g = @truncate(nums[i + 3]), 2478 .b = @truncate(nums[i + 4]), 2479 }; 2480 if (nums[i] == 38) style.fg = c else style.bg = c; 2481 i += 4; 2482 } 2483 }, 2484 else => {}, 2485 } 2486 } 2487 } 2488 }; 2489 2490 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2491 pub const AnsiBackground = struct { 2492 grid: ?Grid, 2493 child: *Widget, 2494 art: AnsiArt, 2495 session: *Session, 2496 2497 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2498 var cw = child_widget; 2499 const child = allocator.create(Widget) catch |e| { 2500 cw.deinit(allocator); 2501 return e; 2502 }; 2503 child.* = cw; 2504 errdefer { 2505 child.deinit(allocator); 2506 allocator.destroy(child); 2507 } 2508 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2509 } 2510 2511 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2512 if (self.grid) |*grid| grid.deinit(); 2513 self.art.deinit(allocator); 2514 self.child.deinit(allocator); 2515 allocator.destroy(self.child); 2516 } 2517 2518 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2519 self.clearGrid(); 2520 2521 // make the wrapped page fill the whole available area 2522 try self.child.build(allocator, .{ 2523 .min_size = .{ 2524 .width = constraint.max_size.width orelse constraint.min_size.width, 2525 .height = constraint.max_size.height orelse constraint.min_size.height, 2526 }, 2527 .max_size = constraint.max_size, 2528 }, root_focus); 2529 2530 if (self.session.data.enable_ansi) { 2531 if (self.child.getGrid()) |fg| { 2532 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2533 } 2534 } 2535 } 2536 2537 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2538 try self.child.input(allocator, key, root_focus); 2539 } 2540 2541 pub fn clearGrid(self: *AnsiBackground) void { 2542 if (self.grid) |*grid| { 2543 grid.deinit(); 2544 self.grid = null; 2545 } 2546 self.child.clearGrid(); 2547 self.art.clearGrid(); 2548 } 2549 2550 pub fn getGrid(self: AnsiBackground) ?Grid { 2551 return self.grid orelse self.child.getGrid(); 2552 } 2553 2554 pub fn getFocus(self: *AnsiBackground) *Focus { 2555 return self.child.getFocus(); 2556 } 2557 2558 // which top corner the art hugs. 2559 const ArtAnchor = enum { top_left, top_right }; 2560 2561 // a foreground cell counts as blank if it's empty or a bare space without styling 2562 fn cellIsBlank(cell: Grid.Cell) bool { 2563 if (cell.rune) |rune| { 2564 return std.mem.eql(u8, rune, " ") and 2565 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2566 } 2567 return true; 2568 } 2569 2570 // the art is dimmed to this fraction of its brightness so foreground text 2571 // stays legible over it 2572 const art_brightness = 45; // percent 2573 2574 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2575 const c = color orelse return null; 2576 return .{ 2577 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2578 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2579 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2580 }; 2581 } 2582 2583 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2584 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2585 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2586 // snap such channels clear of that range so the backdrop doesn't blink or 2587 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2588 // actually render truecolor. 2589 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2590 const c = color orelse return null; 2591 const snap = struct { 2592 fn f(v: u8) u8 { 2593 return if (v >= 1 and v <= 9) 10 else v; 2594 } 2595 }.f; 2596 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2597 } 2598 2599 // composites ANSI art behind a foreground grid 2600 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2601 art.clearGrid(); 2602 try art.build(allocator, .{ 2603 .min_size = .{ .width = null, .height = null }, 2604 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2605 }, root_focus); 2606 2607 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2608 errdefer out.deinit(); 2609 2610 if (art.getGrid()) |art_grid| { 2611 const anchor_x = switch (anchor) { 2612 .top_left => 0, 2613 .top_right => foreground.size.width -| art_grid.size.width, 2614 }; 2615 for (0..art_grid.size.height) |y| { 2616 for (0..art_grid.size.width) |x| { 2617 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2618 if (src.rune == null) continue; 2619 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2620 const dst = &out.cells.items[idx]; 2621 // composite the dimmed art behind every cell so it shows 2622 // through the whole UI: a blank cell becomes the art, while a 2623 // cell with a glyph keeps the glyph but takes the dimmed art as 2624 // its background (replacing whatever background it had, which 2625 // would otherwise obscure the art). 2626 if (cellIsBlank(dst.*)) { 2627 dst.* = src; 2628 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2629 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2630 } else { 2631 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2632 dst.style.bg = background_maybe; 2633 // change the color of text overlaying ANSI art to ensure 2634 // it is readable. don't do this in the web UI, since we 2635 // have direct control of the theme there (also, inner 2636 // scroll panes allow text to move without a re-render 2637 // by the TUI, which would break this). also don't do this 2638 // if the text is explicitly styled. 2639 if (is_terminal and dst.style.fg == null) { 2640 if (background_maybe) |background| { 2641 // use near-black and near-white, which is easier on the eyes 2642 // while retaining high contrast 2643 const luminance = (@as(u32, background.r) * 299 + 2644 @as(u32, background.g) * 587 + 2645 @as(u32, background.b) * 114) / 1000; 2646 dst.style.fg = if (luminance >= 128) 2647 .{ .r = 16, .g = 16, .b = 16 } 2648 else 2649 .{ .r = 240, .g = 240, .b = 240 }; 2650 } 2651 } 2652 } 2653 } 2654 } 2655 } 2656 return out; 2657 } 2658 }; 2659