╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1504 errdefer { 1505 child.deinit(allocator); 1506 allocator.destroy(child); 1507 } 1508 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1509 } 1510 1511 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1512 if (self.grid) |*grid| grid.deinit(); 1513 self.art.deinit(allocator); 1514 self.child.deinit(allocator); 1515 allocator.destroy(self.child); 1516 } 1517 1518 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1519 self.clearGrid(); 1520 1521 // make the wrapped page fill the whole available area 1522 try self.child.build(allocator, .{ 1523 .min_size = .{ 1524 .width = constraint.max_size.width orelse constraint.min_size.width, 1525 .height = constraint.max_size.height orelse constraint.min_size.height, 1526 }, 1527 .max_size = constraint.max_size, 1528 }, root_focus); 1529 1530 if (self.session.data.enable_ansi) { 1531 if (self.child.getGrid()) |fg| { 1532 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1533 } 1534 } 1535 } 1536 1537 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1538 try self.child.input(allocator, key, root_focus); 1539 } 1540 1541 pub fn clearGrid(self: *AnsiBackground) void { 1542 if (self.grid) |*grid| { 1543 grid.deinit(); 1544 self.grid = null; 1545 } 1546 self.child.clearGrid(); 1547 self.art.clearGrid(); 1548 } 1549 1550 pub fn getGrid(self: AnsiBackground) ?Grid { 1551 return self.grid orelse self.child.getGrid(); 1552 } 1553 1554 pub fn getFocus(self: *AnsiBackground) *Focus { 1555 return self.child.getFocus(); 1556 } 1557 1558 // which top corner the art hugs. 1559 const ArtAnchor = enum { top_left, top_right }; 1560 1561 // a foreground cell counts as blank if it's empty or a bare space without styling 1562 fn cellIsBlank(cell: Grid.Cell) bool { 1563 if (cell.rune) |rune| { 1564 return std.mem.eql(u8, rune, " ") and 1565 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1566 } 1567 return true; 1568 } 1569 1570 // a single representative color for an art cell 1571 fn artCellColor(style: Grid.Style) ?Grid.Color { 1572 const f = style.fg orelse return style.bg; 1573 const b = style.bg orelse return f; 1574 return .{ 1575 .r = @intCast((@as(u16, f.r) + b.r) / 2), 1576 .g = @intCast((@as(u16, f.g) + b.g) / 2), 1577 .b = @intCast((@as(u16, f.b) + b.b) / 2), 1578 }; 1579 } 1580 1581 // behind a visible glyph the art color is dimmed to this fraction of its 1582 // brightness so light terminal text stays legible on top of it 1583 const glyph_bg_brightness = 45; // percent 1584 1585 // a glyph sitting over the art also gets its foreground pinned to this color 1586 // (unless it set its own), so the text stays legible 1587 const glyph_fg_over_art = Grid.Color{ .r = 235, .g = 235, .b = 235 }; 1588 1589 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1590 const c = color orelse return null; 1591 return .{ 1592 .r = @intCast(@as(u16, c.r) * glyph_bg_brightness / 100), 1593 .g = @intCast(@as(u16, c.g) * glyph_bg_brightness / 100), 1594 .b = @intCast(@as(u16, c.b) * glyph_bg_brightness / 100), 1595 }; 1596 } 1597 1598 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1599 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1600 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1601 // snap such channels clear of that range so the backdrop doesn't blink or 1602 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1603 // actually render truecolor. 1604 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1605 const c = color orelse return null; 1606 const snap = struct { 1607 fn f(v: u8) u8 { 1608 return if (v >= 1 and v <= 9) 10 else v; 1609 } 1610 }.f; 1611 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1612 } 1613 1614 // composites ANSI art behind a foreground grid 1615 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1616 art.clearGrid(); 1617 try art.build(allocator, .{ 1618 .min_size = .{ .width = null, .height = null }, 1619 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1620 }, root_focus); 1621 1622 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1623 errdefer out.deinit(); 1624 1625 if (art.getGrid()) |art_grid| { 1626 const anchor_x = switch (anchor) { 1627 .top_left => 0, 1628 .top_right => foreground.size.width -| art_grid.size.width, 1629 }; 1630 for (0..art_grid.size.height) |y| { 1631 for (0..art_grid.size.width) |x| { 1632 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1633 if (src.rune == null) continue; 1634 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1635 const dst = &out.cells.items[idx]; 1636 if (cellIsBlank(dst.*)) { 1637 dst.* = src; 1638 dst.style.fg = sgrSafe(dst.style.fg); 1639 dst.style.bg = sgrSafe(dst.style.bg); 1640 } else if (dst.style.bg == null) { 1641 dst.style.bg = sgrSafe(dimColor(artCellColor(src.style))); 1642 if (dst.style.fg == null) dst.style.fg = glyph_fg_over_art; 1643 } 1644 } 1645 } 1646 } 1647 return out; 1648 } 1649 }; 1650