╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
3175 const c = color orelse return null; 3176 return .{ 3177 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 3178 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 3179 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 3180 }; 3181 } 3182 3183 fn buildArt(self: *AnsiBackground, allocator: std.mem.Allocator, size: layout.Size, root_focus: *Focus) !void { 3184 try self.art.build(allocator, .{ 3185 .min_size = .{ .width = null, .height = null }, 3186 .max_size = .{ .width = size.width, .height = size.height }, 3187 }, root_focus); 3188 if (self.art.grid) |*grid| { 3189 for (grid.cells.items) |*cell| { 3190 cell.style.fg = dimColor(cell.style.fg); 3191 cell.style.bg = dimColor(cell.style.bg); 3192 } 3193 } 3194 } 3195 3196 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 3197 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 3198 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 3199 // snap such channels clear of that range so the backdrop doesn't blink or 3200 // hide text there; the ≤9/255 shift is imperceptible on terminals that 3201 // actually render truecolor. 3202 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 3203 const c = color orelse return null; 3204 const snap = struct { 3205 fn f(v: u8) u8 { 3206 return if (v >= 1 and v <= 9) 10 else v; 3207 } 3208 }.f; 3209 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 3210 } 3211 3212 // composites ANSI art behind a foreground grid 3213 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 3214 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 3215 errdefer out.deinit(); 3216 3217 const anchor_x = foreground.size.width -| art_grid.size.width; 3218 for (0..art_grid.size.height) |y| { 3219 for (0..art_grid.size.width) |x| { 3220 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 3221 if (src.rune == null) continue; 3222 const idx = try out.cells.at(.{ y, anchor_x + x }); 3223 const dst = &out.cells.items[idx]; 3224 // blank cells take the art; occupied cells take its background 3225 if (cellIsBlank(dst.*)) { 3226 dst.* = src; 3227 dst.style.fg = sgrSafe(dst.style.fg); 3228 dst.style.bg = sgrSafe(dst.style.bg); 3229 } else { 3230 const background_maybe = sgrSafe(src.style.bg orelse src.style.fg); 3231 dst.style.bg = background_maybe; 3232 // change unstyled text to contrast with the art behind it 3233 if (dst.style.fg == null) { 3234 if (background_maybe) |background| { 3235 // use near-black and near-white, which is easier on the eyes 3236 // while retaining high contrast 3237 const luminance = (@as(u32, background.r) * 299 + 3238 @as(u32, background.g) * 587 + 3239 @as(u32, background.b) * 114) / 1000; 3240 dst.style.fg = if (luminance >= 128) 3241 .{ .r = 16, .g = 16, .b = 16 } 3242 else 3243 .{ .r = 240, .g = 240, .b = 240 }; 3244 } 3245 } 3246 } 3247 } 3248 } 3249 return out; 3250 } 3251 }; 3252