╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1541 } 1542 1543 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1544 if (self.grid) |*grid| grid.deinit(); 1545 self.art.deinit(allocator); 1546 self.child.deinit(allocator); 1547 allocator.destroy(self.child); 1548 } 1549 1550 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1551 self.clearGrid(); 1552 1553 // make the wrapped page fill the whole available area 1554 try self.child.build(allocator, .{ 1555 .min_size = .{ 1556 .width = constraint.max_size.width orelse constraint.min_size.width, 1557 .height = constraint.max_size.height orelse constraint.min_size.height, 1558 }, 1559 .max_size = constraint.max_size, 1560 }, root_focus); 1561 1562 if (self.session.data.enable_ansi) { 1563 if (self.child.getGrid()) |fg| { 1564 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1565 } 1566 } 1567 } 1568 1569 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1570 try self.child.input(allocator, key, root_focus); 1571 } 1572 1573 pub fn clearGrid(self: *AnsiBackground) void { 1574 if (self.grid) |*grid| { 1575 grid.deinit(); 1576 self.grid = null; 1577 } 1578 self.child.clearGrid(); 1579 self.art.clearGrid(); 1580 } 1581 1582 pub fn getGrid(self: AnsiBackground) ?Grid { 1583 return self.grid orelse self.child.getGrid(); 1584 } 1585 1586 pub fn getFocus(self: *AnsiBackground) *Focus { 1587 return self.child.getFocus(); 1588 } 1589 1590 // which top corner the art hugs. 1591 const ArtAnchor = enum { top_left, top_right }; 1592 1593 // a foreground cell counts as blank if it's empty or a bare space without styling 1594 fn cellIsBlank(cell: Grid.Cell) bool { 1595 if (cell.rune) |rune| { 1596 return std.mem.eql(u8, rune, " ") and 1597 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1598 } 1599 return true; 1600 } 1601 1602 // the art is dimmed to this fraction of its brightness so foreground text 1603 // stays legible over it 1604 const art_brightness = 45; // percent 1605 1606 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1607 const c = color orelse return null; 1608 return .{ 1609 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1610 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1611 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1612 }; 1613 } 1614 1615 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1616 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1617 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1618 // snap such channels clear of that range so the backdrop doesn't blink or 1619 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1620 // actually render truecolor. 1621 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1622 const c = color orelse return null; 1623 const snap = struct { 1624 fn f(v: u8) u8 { 1625 return if (v >= 1 and v <= 9) 10 else v; 1626 } 1627 }.f; 1628 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1629 } 1630 1631 // composites ANSI art behind a foreground grid 1632 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1633 art.clearGrid(); 1634 try art.build(allocator, .{ 1635 .min_size = .{ .width = null, .height = null }, 1636 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1637 }, root_focus); 1638 1639 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1640 errdefer out.deinit(); 1641 1642 if (art.getGrid()) |art_grid| { 1643 const anchor_x = switch (anchor) { 1644 .top_left => 0, 1645 .top_right => foreground.size.width -| art_grid.size.width, 1646 }; 1647 for (0..art_grid.size.height) |y| { 1648 for (0..art_grid.size.width) |x| { 1649 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1650 if (src.rune == null) continue; 1651 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1652 const dst = &out.cells.items[idx]; 1653 // composite the dimmed art behind every cell so it shows 1654 // through the whole UI: a blank cell becomes the art, while a 1655 // cell with a glyph keeps the glyph but takes the dimmed art as 1656 // its background (replacing whatever background it had, which 1657 // would otherwise obscure the art). 1658 if (cellIsBlank(dst.*)) { 1659 dst.* = src; 1660 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1661 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1662 } else { 1663 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1664 } 1665 } 1666 } 1667 } 1668 return out; 1669 } 1670 }; 1671