╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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