╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1465 var width: usize = 0; 1466 const content = self.content; 1467 var i: usize = 0; 1468 while (i < content.len) { 1469 const byte = content[i]; 1470 if (byte == '\n') { 1471 width = @max(width, row.items.len); 1472 try rows.append(allocator, row); 1473 row = .empty; 1474 i += 1; 1475 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1476 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1477 var j = i + 2; 1478 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1479 if (j >= content.len) { 1480 i = content.len; // malformed trailing escape; stop 1481 } else { 1482 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1483 i = j + 1; 1484 } 1485 } else { 1486 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1487 const end = @min(content.len, i + len); 1488 const rune = content[i..end]; 1489 const transparent = end - i == 1 and rune[0] == ' ' and 1490 style.fg == null and style.bg == null and !style.inverted; 1491 try row.append(allocator, .{ 1492 .rune = if (transparent) null else rune, 1493 .style = style, 1494 }); 1495 i = end; 1496 } 1497 } 1498 // a trailing row with content but no closing newline 1499 if (row.items.len > 0) { 1500 width = @max(width, row.items.len); 1501 try rows.append(allocator, row); 1502 } else { 1503 row.deinit(allocator); 1504 } 1505 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1506 1507 const height = rows.items.len; 1508 if (width == 0 or height == 0) return; 1509 1510 const clamped_w = @min(width, constraint.max_size.width orelse width); 1511 const clamped_h = @min(height, constraint.max_size.height orelse height); 1512 1513 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1514 errdefer grid.deinit(); 1515 for (rows.items[0..clamped_h], 0..) |r, y| { 1516 const n = @min(r.items.len, clamped_w); 1517 for (r.items[0..n], 0..) |cell, x| { 1518 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1519 } 1520 } 1521 self.grid = grid; 1522 } 1523 1524 pub fn input(self: *AnsiArt, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1525 _ = self; 1526 _ = allocator; 1527 _ = key; 1528 _ = root_focus; 1529 } 1530 1531 pub fn clearGrid(self: *AnsiArt) void { 1532 if (self.grid) |*grid| { 1533 grid.deinit(); 1534 self.grid = null; 1535 } 1536 } 1537 1538 pub fn getGrid(self: AnsiArt) ?Grid { 1539 return self.grid; 1540 } 1541 1542 pub fn getFocus(self: *AnsiArt) *Focus { 1543 return self.focus; 1544 } 1545 1546 fn applySgr(style: *Grid.Style, params: []const u8) void { 1547 var nums: [16]u32 = undefined; 1548 var n: usize = 0; 1549 var it = std.mem.splitScalar(u8, params, ';'); 1550 while (it.next()) |tok| { 1551 if (n >= nums.len) break; 1552 // an empty parameter (e.g. bare "\x1b[m") means 0 1553 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1554 n += 1; 1555 } 1556 if (n == 0) { 1557 style.* = .{}; 1558 return; 1559 } 1560 var i: usize = 0; 1561 while (i < n) : (i += 1) { 1562 switch (nums[i]) { 1563 0 => style.* = .{}, 1564 7 => style.inverted = true, 1565 27 => style.inverted = false, 1566 39 => style.fg = null, 1567 49 => style.bg = null, 1568 38, 48 => { 1569 // truecolor form: 38;2;r;g;b — anything else is ignored 1570 if (i + 4 < n and nums[i + 1] == 2) { 1571 const c = Grid.Color{ 1572 .r = @truncate(nums[i + 2]), 1573 .g = @truncate(nums[i + 3]), 1574 .b = @truncate(nums[i + 4]), 1575 }; 1576 if (nums[i] == 38) style.fg = c else style.bg = c; 1577 i += 4; 1578 } 1579 }, 1580 else => {}, 1581 } 1582 } 1583 } 1584 }; 1585 1586 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1587 pub const AnsiBackground = struct { 1588 grid: ?Grid, 1589 child: *Widget, 1590 art: AnsiArt, 1591 session: *Session, 1592 1593 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1594 var cw = child_widget; 1595 const child = allocator.create(Widget) catch |e| { 1596 cw.deinit(allocator); 1597 return e; 1598 }; 1599 child.* = cw; 1600 errdefer { 1601 child.deinit(allocator); 1602 allocator.destroy(child); 1603 } 1604 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1605 } 1606 1607 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1608 if (self.grid) |*grid| grid.deinit(); 1609 self.art.deinit(allocator); 1610 self.child.deinit(allocator); 1611 allocator.destroy(self.child); 1612 } 1613 1614 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1615 self.clearGrid(); 1616 1617 // make the wrapped page fill the whole available area 1618 try self.child.build(allocator, .{ 1619 .min_size = .{ 1620 .width = constraint.max_size.width orelse constraint.min_size.width, 1621 .height = constraint.max_size.height orelse constraint.min_size.height, 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