╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3131 // a foreground cell counts as blank if it's empty or a bare space without styling 3132 fn cellIsBlank(cell: Grid.Cell) bool { 3133 if (cell.rune) |rune| { 3134 return std.mem.eql(u8, rune, " ") and 3135 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 3136 } 3137 return true; 3138 } 3139 3140 // the art is dimmed to this fraction of its brightness so foreground text 3141 // stays legible over it 3142 const art_brightness = 35; // percent 3143 3144 fn dimColor(color: ?Grid.Color) ?Grid.Color { 3145 const c = color orelse return null; 3146 return .{ 3147 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3148 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3149 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3150 }; 3151 } 3152 3153 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3154 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3155 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3156 // snap such channels clear of that range so the backdrop doesn't blink or 3157 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3158 // actually render truecolor. 3159 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3160 const c = color orelse return null; 3161 const snap = struct { 3162 fn f(v: u8) u8 { 3163 return if (v >= 1 and v <= 9) 10 else v; 3164 } 3165 }.f; 3166 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3167 } 3168 3169 // composites ANSI art behind a foreground grid 3170 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, is_terminal: bool, root_focus: *Focus) !Grid { 3171 art.clearGrid(); 3172 try art.build(allocator, .{ 3173 .min_size = .{ .width = null, .height = null }, 3174 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 3175 }, root_focus); 3176 3177 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3178 errdefer out.deinit(); 3179 3180 if (art.getGrid()) |art_grid| { 3181 const anchor_x = switch (anchor) { 3182 .top_left => 0, 3183 .top_right => foreground.size.width -| art_grid.size.width, 3184 }; 3185 for (0..art_grid.size.height) |y| { 3186 for (0..art_grid.size.width) |x| { 3187 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3188 if (src.rune == null) continue; 3189 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 3190 const dst = &out.cells.items[idx]; 3191 // composite the dimmed art behind every cell so it shows 3192 // through the whole UI: a blank cell becomes the art, while a 3193 // cell with a glyph keeps the glyph but takes the dimmed art as 3194 // its background (replacing whatever background it had, which 3195 // would otherwise obscure the art). 3196 if (cellIsBlank(dst.*)) { 3197 dst.* = src; 3198 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 3199 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 3200 } else { 3201 const background_maybe = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 3202 dst.style.bg = background_maybe; 3203 // change the color of text overlaying ANSI art to ensure 3204 // it is readable. don't do this in the web UI, since we 3205 // have direct control of the theme there (also, inner 3206 // scroll panes allow text to move without a re-render 3207 // by the TUI, which would break this). also don't do this 3208 // if the text is explicitly styled. 3209 if (is_terminal and dst.style.fg == null) { 3210 if (background_maybe) |background| { 3211 // use near-black and near-white, which is easier on the eyes 3212 // while retaining high contrast 3213 const luminance = (@as(u32, background.r) * 299 + 3214 @as(u32, background.g) * 587 + 3215 @as(u32, background.b) * 114) / 1000; 3216 dst.style.fg = if (luminance >= 128) 3217 .{ .r = 16, .g = 16, .b = 16 } 3218 else 3219 .{ .r = 240, .g = 240, .b = 240 }; 3220 } 3221 } 3222 } 3223 } 3224 } 3225 } 3226 return out; 3227 } 3228 }; 3229