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