╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1622 }, 1623 .max_size = constraint.max_size, 1624 }, root_focus); 1625 1626 if (self.session.data.enable_ansi) { 1627 if (self.child.getGrid()) |fg| { 1628 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1629 } 1630 } 1631 } 1632 1633 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1634 try self.child.input(allocator, key, root_focus); 1635 } 1636 1637 pub fn clearGrid(self: *AnsiBackground) void { 1638 if (self.grid) |*grid| { 1639 grid.deinit(); 1640 self.grid = null; 1641 } 1642 self.child.clearGrid(); 1643 self.art.clearGrid(); 1644 } 1645 1646 pub fn getGrid(self: AnsiBackground) ?Grid { 1647 return self.grid orelse self.child.getGrid(); 1648 } 1649 1650 pub fn getFocus(self: *AnsiBackground) *Focus { 1651 return self.child.getFocus(); 1652 } 1653 1654 // which top corner the art hugs. 1655 const ArtAnchor = enum { top_left, top_right }; 1656 1657 // a foreground cell counts as blank if it's empty or a bare space without styling 1658 fn cellIsBlank(cell: Grid.Cell) bool { 1659 if (cell.rune) |rune| { 1660 return std.mem.eql(u8, rune, " ") and 1661 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1662 } 1663 return true; 1664 } 1665 1666 // the art is dimmed to this fraction of its brightness so foreground text 1667 // stays legible over it 1668 const art_brightness = 45; // percent 1669 1670 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1671 const c = color orelse return null; 1672 return .{ 1673 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1674 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1675 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1676 }; 1677 } 1678 1679 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1680 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1681 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1682 // snap such channels clear of that range so the backdrop doesn't blink or 1683 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1684 // actually render truecolor. 1685 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1686 const c = color orelse return null; 1687 const snap = struct { 1688 fn f(v: u8) u8 { 1689 return if (v >= 1 and v <= 9) 10 else v; 1690 } 1691 }.f; 1692 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1693 } 1694 1695 // composites ANSI art behind a foreground grid 1696 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1697 art.clearGrid(); 1698 try art.build(allocator, .{ 1699 .min_size = .{ .width = null, .height = null }, 1700 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1701 }, root_focus); 1702 1703 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1704 errdefer out.deinit(); 1705 1706 if (art.getGrid()) |art_grid| { 1707 const anchor_x = switch (anchor) { 1708 .top_left => 0, 1709 .top_right => foreground.size.width -| art_grid.size.width, 1710 }; 1711 for (0..art_grid.size.height) |y| { 1712 for (0..art_grid.size.width) |x| { 1713 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1714 if (src.rune == null) continue; 1715 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1716 const dst = &out.cells.items[idx]; 1717 // composite the dimmed art behind every cell so it shows 1718 // through the whole UI: a blank cell becomes the art, while a 1719 // cell with a glyph keeps the glyph but takes the dimmed art as 1720 // its background (replacing whatever background it had, which 1721 // would otherwise obscure the art). 1722 if (cellIsBlank(dst.*)) { 1723 dst.* = src; 1724 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1725 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1726 } else { 1727 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1728 } 1729 } 1730 } 1731 } 1732 return out; 1733 } 1734 }; 1735