╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1439 } 1440 return true; 1441 } 1442 1443 // a single representative color for an art cell 1444 fn artCellColor(style: Grid.Style) ?Grid.Color { 1445 const f = style.fg orelse return style.bg; 1446 const b = style.bg orelse return f; 1447 return .{ 1448 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1449 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1450 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1451 }; 1452 } 1453 1454 // behind a visible glyph the art color is dimmed to this fraction of its 1455 // brightness so light terminal text stays legible on top of it 1456 const glyph_bg_brightness = 45; // percent 1457 1458 // a glyph sitting over the art also gets its foreground pinned to this color 1459 // (unless it set its own), so the text stays legible 1460 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1461 1462 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1463 const c = color orelse return null; 1464 return .{ 1465 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1466 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1467 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1468 }; 1469 } 1470 1471 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1472 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1473 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1474 // snap such channels clear of that range so the backdrop doesn't blink or 1475 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1476 // actually render truecolor. 1477 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1478 const c = color orelse return null; 1479 const snap = struct { 1480 fn f(v: u8) u8 { 1481 return if (v >= 1 and v <= 9) 10 else v; 1482 } 1483 }.f; 1484 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1485 } 1486 1487 // composites ANSI art behind a foreground grid 1488 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1489 art.clearGrid(); 1490 try art.build(allocator, .{ 1491 .min_size = .{ .width = null, .height = null }, 1492 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1493 }, root_focus); 1494 1495 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1496 errdefer out.deinit(); 1497 1498 if (art.getGrid()) |art_grid| { 1499 const anchor_x = switch (anchor) { 1500 .top_left => 0, 1501 .top_right => foreground.size.width -| art_grid.size.width, 1502 }; 1503 for (0..art_grid.size.height) |y| { 1504 for (0..art_grid.size.width) |x| { 1505 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1506 if (src.rune == null) continue; 1507 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1508 const dst = &out.cells.items[idx]; 1509 if (cellIsBlank(dst.*)) { 1510 dst.* = src; 1511 dst.style.fg = sgrSafe(dst.style.fg); 1512 dst.style.bg = sgrSafe(dst.style.bg); 1513 } else if (dst.style.bg == null) { 1514 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1515 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1516 } 1517 } 1518 } 1519 } 1520 return out; 1521 } 1522 }; 1523