╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2900 } 2901 2902 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2903 try self.child.input(allocator, key, root_focus); 2904 } 2905 2906 pub fn clearGrid(self: *AnsiBackground) void { 2907 if (self.grid) |*grid| { 2908 grid.deinit(); 2909 self.grid = null; 2910 } 2911 self.child.clearGrid(); 2912 self.art.clearGrid(); 2913 } 2914 2915 pub fn getGrid(self: AnsiBackground) ?Grid { 2916 return self.grid orelse self.child.getGrid(); 2917 } 2918 2919 pub fn getFocus(self: *AnsiBackground) *Focus { 2920 return self.child.getFocus(); 2921 } 2922 2923 // which top corner the art hugs. 2924 const ArtAnchor = enum { top_left, top_right }; 2925 2926 // a foreground cell counts as blank if it's empty or a bare space without styling 2927 fn cellIsBlank(cell: Grid.Cell) bool { 2928 if (cell.rune) |rune| { 2929 return std.mem.eql(u8, rune, " ") and 2930 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2931 } 2932 return true; 2933 } 2934 2935 // the art is dimmed to this fraction of its brightness so foreground text 2936 // stays legible over it 2937 const art_brightness = 45; // percent 2938 2939 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2940 const c = color orelse return null; 2941 return .{ 2942 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2943 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2944 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2945 }; 2946 } 2947 2948 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2949 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2950 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2951 // snap such channels clear of that range so the backdrop doesn't blink or 2952 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2953 // actually render truecolor. 2954 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2955 const c = color orelse return null; 2956 const snap = struct { 2957 fn f(v: u8) u8 { 2958 return if (v >= 1 and v <= 9) 10 else v; 2959 } 2960 }.f; 2961 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2962 } 2963 2964 // composites ANSI art behind a foreground grid 2965 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 2966 art.clearGrid(); 2967 try art.build(allocator, .{ 2968 .min_size = .{ .width = null, .height = null }, 2969 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2970 }, root_focus); 2971 2972 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2973 errdefer out.deinit(); 2974 2975 if (art.getGrid()) |art_grid| { 2976 const anchor_x = switch (anchor) { 2977 .top_left => 0, 2978 .top_right => foreground.size.width -| art_grid.size.width, 2979 }; 2980 for (0..art_grid.size.height) |y| { 2981 for (0..art_grid.size.width) |x| { 2982 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2983 if (src.rune == null) continue; 2984 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2985 const dst = &out.cells.items[idx]; 2986 // composite the dimmed art behind every cell so it shows 2987 // through the whole UI: a blank cell becomes the art, while a 2988 // cell with a glyph keeps the glyph but takes the dimmed art as 2989 // its background (replacing whatever background it had, which 2990 // would otherwise obscure the art). 2991 if (cellIsBlank(dst.*)) { 2992 dst.* = src; 2993 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2994 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2995 } else { 2996 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2997 dst.style.bg = background_maybe; 2998 // change the color of text overlaying ANSI art to ensure 2999 // it is readable. don't do this in the web UI, since we 3000 // have direct control of the theme there (also, inner 3001 // scroll panes allow text to move without a re-render 3002 // by the TUI, which would break this). also don't do this 3003 // if the text is explicitly styled. 3004 if (is_terminal and dst.style.fg == null) { 3005 if (background_maybe) |background| { 3006 // use near-black and near-white, which is easier on the eyes 3007 // while retaining high contrast 3008 const luminance = (@as(u32, background.r) * 299 + 3009 @as(u32, background.g) * 587 + 3010 @as(u32, background.b) * 114) / 1000; 3011 dst.style.fg = if (luminance >= 128) 3012 .{ .r = 16, .g = 16, .b = 16 } 3013 else 3014 .{ .r = 240, .g = 240, .b = 240 }; 3015 } 3016 } 3017 } 3018 } 3019 } 3020 } 3021 return out; 3022 } 3023 }; 3024