╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1557 } 1558 } 1559 // a trailing row with content but no closing newline 1560 if (row.items.len > 0) { 1561 width = @max(width, row.items.len); 1562 try rows.append(allocator, row); 1563 } else { 1564 row.deinit(allocator); 1565 } 1566 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1567 1568 const height = rows.items.len; 1569 if (width == 0 or height == 0) return; 1570 1571 const clamped_w = @min(width, constraint.max_size.width orelse width); 1572 const clamped_h = @min(height, constraint.max_size.height orelse height); 1573 1574 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1575 errdefer grid.deinit(); 1576 for (rows.items[0..clamped_h], 0..) |r, y| { 1577 const n = @min(r.items.len, clamped_w); 1578 for (r.items[0..n], 0..) |cell, x| { 1579 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1580 } 1581 } 1582 self.grid = grid; 1583 } 1584 1585 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1586 _ = self; 1587 _ = allocator; 1588 _ = key; 1589 _ = root_focus; 1590 } 1591 1592 pub fn clearGrid(self: *AnsiArt) void { 1593 if (self.grid) |*grid| { 1594 grid.deinit(); 1595 self.grid = null; 1596 } 1597 } 1598 1599 pub fn getGrid(self: AnsiArt) ?Grid { 1600 return self.grid; 1601 } 1602 1603 pub fn getFocus(self: *AnsiArt) *Focus { 1604 return self.focus; 1605 } 1606 1607 fn applySgr(style: *Grid.Style, params: []const u8) void { 1608 var nums: [16]u32 = undefined; 1609 var n: usize = 0; 1610 var it = std.mem.splitScalar(u8, params, ';'); 1611 while (it.next()) |tok| { 1612 if (n >= nums.len) break; 1613 // an empty parameter (e.g. bare "\x1b[m") means 0 1614 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1615 n += 1; 1616 } 1617 if (n == 0) { 1618 style.* = .{}; 1619 return; 1620 } 1621 var i: usize = 0; 1622 while (i < n) : (i += 1) { 1623 switch (nums[i]) { 1624 0 => style.* = .{}, 1625 7 => style.inverted = true, 1626 27 => style.inverted = false, 1627 39 => style.fg = null, 1628 49 => style.bg = null, 1629 38, 48 => { 1630 // truecolor form: 38;2;r;g;b — anything else is ignored 1631 if (i + 4 < n and nums[i + 1] == 2) { 1632 const c = Grid.Color{ 1633 .r = @truncate(nums[i + 2]), 1634 .g = @truncate(nums[i + 3]), 1635 .b = @truncate(nums[i + 4]), 1636 }; 1637 if (nums[i] == 38) style.fg = c else style.bg = c; 1638 i += 4; 1639 } 1640 }, 1641 else => {}, 1642 } 1643 } 1644 } 1645 }; 1646 1647 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1648 pub const AnsiBackground = struct { 1649 grid: ?Grid, 1650 child: *Widget, 1651 art: AnsiArt, 1652 session: *Session, 1653 1654 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1655 var cw = child_widget; 1656 const child = allocator.create(Widget) catch |e| { 1657 cw.deinit(allocator); 1658 return e; 1659 }; 1660 child.* = cw; 1661 errdefer { 1662 child.deinit(allocator); 1663 allocator.destroy(child); 1664 } 1665 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1666 } 1667 1668 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1669 if (self.grid) |*grid| grid.deinit(); 1670 self.art.deinit(allocator); 1671 self.child.deinit(allocator); 1672 allocator.destroy(self.child); 1673 } 1674 1675 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1676 self.clearGrid(); 1677 1678 // make the wrapped page fill the whole available area 1679 try self.child.build(allocator, .{ 1680 .min_size = .{ 1681 .width = constraint.max_size.width orelse constraint.min_size.width, 1682 .height = constraint.max_size.height orelse constraint.min_size.height, 1683 }, 1684 .max_size = constraint.max_size, 1685 }, root_focus); 1686 1687 if (self.session.data.enable_ansi) { 1688 if (self.child.getGrid()) |fg| { 1689 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1690 } 1691 } 1692 } 1693 1694 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1695 try self.child.input(allocator, key, root_focus); 1696 } 1697 1698 pub fn clearGrid(self: *AnsiBackground) void { 1699 if (self.grid) |*grid| { 1700 grid.deinit(); 1701 self.grid = null; 1702 } 1703 self.child.clearGrid(); 1704 self.art.clearGrid(); 1705 } 1706 1707 pub fn getGrid(self: AnsiBackground) ?Grid { 1708 return self.grid orelse self.child.getGrid(); 1709 } 1710 1711 pub fn getFocus(self: *AnsiBackground) *Focus { 1712 return self.child.getFocus(); 1713 } 1714 1715 // which top corner the art hugs. 1716 const ArtAnchor = enum { top_left, top_right }; 1717 1718 // a foreground cell counts as blank if it's empty or a bare space without styling 1719 fn cellIsBlank(cell: Grid.Cell) bool { 1720 if (cell.rune) |rune| { 1721 return std.mem.eql(u8, rune, " ") and 1722 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1723 } 1724 return true; 1725 } 1726 1727 // the art is dimmed to this fraction of its brightness so foreground text 1728 // stays legible over it 1729 const art_brightness = 45; // percent 1730 1731 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1732 const c = color orelse return null; 1733 return .{ 1734 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1735 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1736 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1737 }; 1738 } 1739 1740 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1741 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1742 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1743 // snap such channels clear of that range so the backdrop doesn't blink or 1744 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1745 // actually render truecolor. 1746 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1747 const c = color orelse return null; 1748 const snap = struct { 1749 fn f(v: u8) u8 { 1750 return if (v >= 1 and v <= 9) 10 else v; 1751 } 1752 }.f; 1753 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1754 } 1755 1756 // composites ANSI art behind a foreground grid 1757 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1758 art.clearGrid(); 1759 try art.build(allocator, .{ 1760 .min_size = .{ .width = null, .height = null }, 1761 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1762 }, root_focus); 1763 1764 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1765 errdefer out.deinit(); 1766 1767 if (art.getGrid()) |art_grid| { 1768 const anchor_x = switch (anchor) { 1769 .top_left => 0, 1770 .top_right => foreground.size.width -| art_grid.size.width, 1771 }; 1772 for (0..art_grid.size.height) |y| { 1773 for (0..art_grid.size.width) |x| { 1774 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1775 if (src.rune == null) continue; 1776 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1777 const dst = &out.cells.items[idx]; 1778 // composite the dimmed art behind every cell so it shows 1779 // through the whole UI: a blank cell becomes the art, while a 1780 // cell with a glyph keeps the glyph but takes the dimmed art as 1781 // its background (replacing whatever background it had, which 1782 // would otherwise obscure the art). 1783 if (cellIsBlank(dst.*)) { 1784 dst.* = src; 1785 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1786 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1787 } else { 1788 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1789 } 1790 } 1791 } 1792 } 1793 return out; 1794 } 1795 }; 1796