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