╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2522 { 2523 var tb = try wgt.TextBox(Widget).init(allocator, content, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2524 errdefer tb.deinit(allocator); 2525 tb.getFocus().focusable = true; 2526 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 2527 } 2528 2529 box.getFocus().child_id = box.children.keys()[1]; 2530 return .{ .box = box }; 2531 } 2532 2533 pub fn deinit(self: *SectionLabel, allocator: std.mem.Allocator) void { 2534 self.box.deinit(allocator); 2535 } 2536 2537 pub fn build(self: *SectionLabel, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2538 try self.box.build(allocator, constraint, root_focus); 2539 } 2540 2541 pub fn input(self: *SectionLabel, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2542 _ = self; 2543 _ = allocator; 2544 _ = key; 2545 _ = root_focus; 2546 } 2547 2548 pub fn clearGrid(self: *SectionLabel) void { 2549 self.box.clearGrid(); 2550 } 2551 2552 pub fn getGrid(self: SectionLabel) ?Grid { 2553 return self.box.getGrid(); 2554 } 2555 2556 pub fn getFocus(self: *SectionLabel) *Focus { 2557 return self.box.getFocus(); 2558 } 2559 }; 2560 2561 // a submit button, indented past its form's content. the web overlay POSTs 2562 // the button's form; terminal hosts detect presses through `buttonId`. 2563 pub const SubmitButton = struct { 2564 box: wgt.Box(Widget), 2565 2566 pub fn init(allocator: std.mem.Allocator) !SubmitButton { 2567 return initLabeled(allocator, "submit"); 2568 } 2569 2570 pub fn initLabeled(allocator: std.mem.Allocator, label: []const u8) !SubmitButton { 2571 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 2572 errdefer box.deinit(allocator); 2573 2574 { 2575 var gap = try wgt.Text(Widget).init(allocator, " "); 2576 errdefer gap.deinit(allocator); 2577 try box.children.put(allocator, gap.getFocus().id, .{ .widget = .{ .text = gap }, .rect = null, .min_size = null }); 2578 } 2579 2580 { 2581 var button = try wgt.TextBox(Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 2582 errdefer button.deinit(allocator); 2583 button.getFocus().focusable = true; 2584 // the renderer distinguishes plain clickables from buttons that 2585 // should POST to a server route by this kind. 2586 button.getFocus().kind = .{ .custom = "submit" }; 2587 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 2588 } 2589 2590 box.getFocus().child_id = box.children.keys()[1]; 2591 return .{ .box = box }; 2592 } 2593 2594 pub fn deinit(self: *SubmitButton, allocator: std.mem.Allocator) void { 2595 self.box.deinit(allocator); 2596 } 2597 2598 pub fn build(self: *SubmitButton, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2599 try self.box.build(allocator, constraint, root_focus); 2600 } 2601 2602 pub fn input(self: *SubmitButton, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2603 _ = self; 2604 _ = allocator; 2605 _ = key; 2606 _ = root_focus; 2607 } 2608 2609 pub fn clearGrid(self: *SubmitButton) void { 2610 self.box.clearGrid(); 2611 } 2612 2613 pub fn getGrid(self: SubmitButton) ?Grid { 2614 return self.box.getGrid(); 2615 } 2616 2617 pub fn getFocus(self: *SubmitButton) *Focus { 2618 return self.box.getFocus(); 2619 } 2620 2621 // the button's focus id, for click hit-testing 2622 pub fn buttonId(self: *const SubmitButton) usize { 2623 return self.box.children.keys()[1]; 2624 } 2625 }; 2626 2627 pub const Center = struct { 2628 focus: *Focus, 2629 grid: ?Grid, 2630 child: *Widget, 2631 2632 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 2633 const child = try allocator.create(Widget); 2634 errdefer allocator.destroy(child); 2635 child.* = child_widget; 2636 return .{ 2637 .focus = try Focus.create(allocator, .container), 2638 .grid = null, 2639 .child = child, 2640 }; 2641 } 2642 2643 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 2644 self.focus.destroy(allocator); 2645 if (self.grid) |*grid| { 2646 grid.deinit(); 2647 self.grid = null; 2648 } 2649 self.child.deinit(allocator); 2650 allocator.destroy(self.child); 2651 } 2652 2653 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2654 self.clearGrid(); 2655 self.getFocus().clear(); 2656 2657 // build the child without forcing it to fill min_size; it sizes 2658 // itself to its content within the available max. 2659 try self.child.build(allocator, .{ 2660 .min_size = .{ .width = null, .height = null }, 2661 .max_size = constraint.max_size, 2662 }, root_focus); 2663 2664 const child_grid = self.child.getGrid() orelse return; 2665 2666 // prefer max when given; otherwise grow to max(min, child) so that 2667 // when only a min is set (e.g. the wasm path passes viewport rows 2668 // as min_height with no max), we can still vertically center while 2669 // letting taller content extend past the min. 2670 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; 2671 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; 2672 if (width == 0 or height == 0) return; 2673 2674 const offset_x: usize = (width -| child_grid.size.width) / 2; 2675 const offset_y: usize = (height -| child_grid.size.height) / 2; 2676 2677 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 2678 errdefer grid.deinit(); 2679 try grid.drawGrid(child_grid, offset_x, offset_y); 2680 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 2681 self.getFocus().child_id = self.child.getFocus().id; 2682 2683 self.grid = grid; 2684 } 2685 2686 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2687 try self.child.input(allocator, key, root_focus); 2688 } 2689 2690 pub fn clearGrid(self: *Center) void { 2691 if (self.grid) |*grid| { 2692 grid.deinit(); 2693 self.grid = null; 2694 } 2695 self.child.clearGrid(); 2696 } 2697 2698 pub fn getGrid(self: Center) ?Grid { 2699 return self.grid; 2700 } 2701 2702 pub fn getFocus(self: *Center) *Focus { 2703 return self.focus; 2704 } 2705 }; 2706 2707 // renders truecolor ANSI art 2708 pub const AnsiArt = struct { 2709 focus: *Focus, 2710 grid: ?Grid, 2711 content: []const u8, 2712 2713 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 2714 return .{ 2715 .focus = try Focus.create(allocator, .container), 2716 .grid = null, 2717 .content = content, 2718 }; 2719 } 2720 2721 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 2722 self.focus.destroy(allocator); 2723 if (self.grid) |*grid| { 2724 grid.deinit(); 2725 self.grid = null; 2726 } 2727 } 2728 2729 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2730 _ = root_focus; 2731 self.clearGrid(); 2732 2733 // parse into rows of cells, then copy into a rectangular grid 2734 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 2735 defer { 2736 for (rows.items) |*row| row.deinit(allocator); 2737 rows.deinit(allocator); 2738 } 2739 var row: std.ArrayList(Grid.Cell) = .empty; 2740 errdefer row.deinit(allocator); 2741 2742 var style: Grid.Style = .{}; 2743 var width: usize = 0; 2744 const content = self.content; 2745 var i: usize = 0; 2746 while (i < content.len) { 2747 const byte = content[i]; 2748 if (byte == '\n') { 2749 width = @max(width, row.items.len); 2750 try rows.append(allocator, row); 2751 row = .empty; 2752 i += 1; 2753 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 2754 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 2755 var j = i + 2; 2756 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 2757 if (j >= content.len) { 2758 i = content.len; // malformed trailing escape; stop 2759 } else { 2760 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 2761 i = j + 1; 2762 } 2763 } else { 2764 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 2765 const end = @min(content.len, i + len); 2766 const rune = content[i..end]; 2767 const transparent = end - i == 1 and rune[0] == ' ' and 2768 style.fg == null and style.bg == null and !style.inverted; 2769 try row.append(allocator, .{ 2770 .rune = if (transparent) null else rune, 2771 .style = style, 2772 }); 2773 i = end; 2774 } 2775 } 2776 // a trailing row with content but no closing newline 2777 if (row.items.len > 0) { 2778 width = @max(width, row.items.len); 2779 try rows.append(allocator, row); 2780 } else { 2781 row.deinit(allocator); 2782 } 2783 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 2784 2785 const height = rows.items.len; 2786 if (width == 0 or height == 0) return; 2787 2788 const clamped_w = @min(width, constraint.max_size.width orelse width); 2789 const clamped_h = @min(height, constraint.max_size.height orelse height); 2790 2791 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 2792 errdefer grid.deinit(); 2793 for (rows.items[0..clamped_h], 0..) |r, y| { 2794 const n = @min(r.items.len, clamped_w); 2795 for (r.items[0..n], 0..) |cell, x| { 2796 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 2797 } 2798 } 2799 self.grid = grid; 2800 } 2801 2802 pub fn clearGrid(self: *AnsiArt) void { 2803 if (self.grid) |*grid| { 2804 grid.deinit(); 2805 self.grid = null; 2806 } 2807 } 2808 2809 pub fn getGrid(self: AnsiArt) ?Grid { 2810 return self.grid; 2811 } 2812 2813 pub fn getFocus(self: *AnsiArt) *Focus { 2814 return self.focus; 2815 } 2816 2817 fn applySgr(style: *Grid.Style, params: []const u8) void { 2818 var nums: [16]u32 = undefined; 2819 var n: usize = 0; 2820 var it = std.mem.splitScalar(u8, params, ';'); 2821 while (it.next()) |tok| { 2822 if (n >= nums.len) break; 2823 // an empty parameter (e.g. bare "\x1b[m") means 0 2824 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 2825 n += 1; 2826 } 2827 if (n == 0) { 2828 style.* = .{}; 2829 return; 2830 } 2831 var i: usize = 0; 2832 while (i < n) : (i += 1) { 2833 switch (nums[i]) { 2834 0 => style.* = .{}, 2835 7 => style.inverted = true, 2836 27 => style.inverted = false, 2837 39 => style.fg = null, 2838 49 => style.bg = null, 2839 38, 48 => { 2840 // truecolor form: 38;2;r;g;b — anything else is ignored 2841 if (i + 4 < n and nums[i + 1] == 2) { 2842 const c = Grid.Color{ 2843 .r = @truncate(nums[i + 2]), 2844 .g = @truncate(nums[i + 3]), 2845 .b = @truncate(nums[i + 4]), 2846 }; 2847 if (nums[i] == 38) style.fg = c else style.bg = c; 2848 i += 4; 2849 } 2850 }, 2851 else => {}, 2852 } 2853 } 2854 } 2855 }; 2856 2857 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2858 pub const AnsiBackground = struct { 2859 grid: ?Grid, 2860 child: *Widget, 2861 art: AnsiArt, 2862 session: *Session, 2863 2864 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2865 var cw = child_widget; 2866 const child = allocator.create(Widget) catch |e| { 2867 cw.deinit(allocator); 2868 return e; 2869 }; 2870 child.* = cw; 2871 errdefer { 2872 child.deinit(allocator); 2873 allocator.destroy(child); 2874 } 2875 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2876 } 2877 2878 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2879 if (self.grid) |*grid| grid.deinit(); 2880 self.art.deinit(allocator); 2881 self.child.deinit(allocator); 2882 allocator.destroy(self.child); 2883 } 2884 2885 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2886 self.clearGrid(); 2887 2888 // make the wrapped page fill the whole available area 2889 try self.child.build(allocator, .{ 2890 .min_size = .{ 2891 .width = constraint.max_size.width orelse constraint.min_size.width, 2892 .height = constraint.max_size.height orelse constraint.min_size.height, 2893 }, 2894 .max_size = constraint.max_size, 2895 }, root_focus); 2896 2897 if (self.session.data.enable_ansi) { 2898 if (self.child.getGrid()) |fg| { 2899 self.grid = try artBehind(allocator, fg, &self.art, .top_right, self.session.is_terminal, root_focus); 2900 } 2901 } 2902 } 2903 2904 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2905 try self.child.input(allocator, key, root_focus); 2906 } 2907 2908 pub fn clearGrid(self: *AnsiBackground) void { 2909 if (self.grid) |*grid| { 2910 grid.deinit(); 2911 self.grid = null; 2912 } 2913 self.child.clearGrid(); 2914 self.art.clearGrid(); 2915 } 2916 2917 pub fn getGrid(self: AnsiBackground) ?Grid { 2918 return self.grid orelse self.child.getGrid(); 2919 } 2920 2921 pub fn getFocus(self: *AnsiBackground) *Focus { 2922 return self.child.getFocus(); 2923 } 2924 2925 // which top corner the art hugs. 2926 const ArtAnchor = enum { top_left, top_right }; 2927 2928 // a foreground cell counts as blank if it's empty or a bare space without styling 2929 fn cellIsBlank(cell: Grid.Cell) bool { 2930 if (cell.rune) |rune| { 2931 return std.mem.eql(u8, rune, " ") and 2932 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2933 } 2934 return true; 2935 } 2936 2937 // the art is dimmed to this fraction of its brightness so foreground text 2938 // stays legible over it 2939 const art_brightness = 45; // percent 2940 2941 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2942 const c = color orelse return null; 2943 return .{ 2944 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2945 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2946 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2947 }; 2948 } 2949 2950 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2951 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2952 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2953 // snap such channels clear of that range so the backdrop doesn't blink or 2954 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2955 // actually render truecolor. 2956 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2957 const c = color orelse return null; 2958 const snap = struct { 2959 fn f(v: u8) u8 { 2960 return if (v >= 1 and v <= 9) 10 else v; 2961 } 2962 }.f; 2963 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2964 } 2965 2966 // composites ANSI art behind a foreground grid 2967 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2968 art.clearGrid(); 2969 try art.build(allocator, .{ 2970 .min_size = .{ .width = null, .height = null }, 2971 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2972 }, root_focus); 2973 2974 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2975 errdefer out.deinit(); 2976 2977 if (art.getGrid()) |art_grid| { 2978 const anchor_x = switch (anchor) { 2979 .top_left => 0, 2980 .top_right => foreground.size.width -| art_grid.size.width, 2981 }; 2982 for (0..art_grid.size.height) |y| { 2983 for (0..art_grid.size.width) |x| { 2984 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2985 if (src.rune == null) continue; 2986 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2987 const dst = &out.cells.items[idx]; 2988 // composite the dimmed art behind every cell so it shows 2989 // through the whole UI: a blank cell becomes the art, while a 2990 // cell with a glyph keeps the glyph but takes the dimmed art as 2991 // its background (replacing whatever background it had, which 2992 // would otherwise obscure the art). 2993 if (cellIsBlank(dst.*)) { 2994 dst.* = src; 2995 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2996 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2997 } else { 2998 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2999 dst.style.bg = background_maybe; 3000 // change the color of text overlaying ANSI art to ensure 3001 // it is readable. don't do this in the web UI, since we 3002 // have direct control of the theme there (also, inner 3003 // scroll panes allow text to move without a re-render 3004 // by the TUI, which would break this). also don't do this 3005 // if the text is explicitly styled. 3006 if (is_terminal and dst.style.fg == null) { 3007 if (background_maybe) |background| { 3008 // use near-black and near-white, which is easier on the eyes 3009 // while retaining high contrast 3010 const luminance = (@as(u32, background.r) * 299 + 3011 @as(u32, background.g) * 587 + 3012 @as(u32, background.b) * 114) / 1000; 3013 dst.style.fg = if (luminance >= 128) 3014 .{ .r = 16, .g = 16, .b = 16 } 3015 else 3016 .{ .r = 240, .g = 240, .b = 240 }; 3017 } 3018 } 3019 } 3020 } 3021 } 3022 } 3023 return out; 3024 } 3025 }; 3026