╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1393 1394 pub fn init(allocator: std.mem.Allocator) !Spacer { 1395 return .{ 1396 .focus = try Focus.create(allocator, .container), 1397 .grid = null, 1398 }; 1399 } 1400 1401 pub fn deinit(self: *Spacer, allocator: std.mem.Allocator) void { 1402 self.focus.destroy(allocator); 1403 if (self.grid) |*grid| { 1404 grid.deinit(); 1405 self.grid = null; 1406 } 1407 } 1408 1409 pub fn build(self: *Spacer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1410 _ = root_focus; 1411 self.clearGrid(); 1412 const width = constraint.max_size.width orelse return; 1413 if (width == 0) return; 1414 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1415 } 1416 1417 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1418 _ = self; 1419 _ = allocator; 1420 _ = key; 1421 _ = root_focus; 1422 } 1423 1424 pub fn clearGrid(self: *Spacer) void { 1425 if (self.grid) |*grid| { 1426 grid.deinit(); 1427 self.grid = null; 1428 } 1429 } 1430 1431 pub fn getGrid(self: Spacer) ?Grid { 1432 return self.grid; 1433 } 1434 1435 pub fn getFocus(self: *Spacer) *Focus { 1436 return self.focus; 1437 } 1438 }; 1439 1440 // a one-row, non-focusable status bar showing the current page's url 1441 pub const Footer = struct { 1442 focus: *Focus, 1443 grid: ?Grid, 1444 session: *Session, 1445 arena: std.heap.ArenaAllocator, 1446 1447 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1448 return .{ 1449 .focus = try Focus.create(allocator, .container), 1450 .grid = null, 1451 .session = session, 1452 .arena = std.heap.ArenaAllocator.init(allocator), 1453 }; 1454 } 1455 1456 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1457 self.focus.destroy(allocator); 1458 if (self.grid) |*grid| grid.deinit(); 1459 self.arena.deinit(); 1460 } 1461 1462 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1463 _ = root_focus; 1464 self.clearGrid(); 1465 const width = constraint.max_size.width orelse return; 1466 if (width == 0) return; 1467 1468 _ = self.arena.reset(.retain_capacity); 1469 const aa = self.arena.allocator(); 1470 const path = self.session.data.current_page.urlAlloc(&self.arena) catch return; 1471 const text = if (self.session.web_port) |port| 1472 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1473 else 1474 path; 1475 1476 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1477 errdefer grid.deinit(); 1478 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1479 var i: usize = 0; 1480 while (utf8.nextCodepointSlice()) |ch| { 1481 if (i == width) break; 1482 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1483 i += 1; 1484 } 1485 self.grid = grid; 1486 } 1487 1488 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1489 _ = self; 1490 _ = allocator; 1491 _ = key; 1492 _ = root_focus; 1493 } 1494 1495 pub fn clearGrid(self: *Footer) void { 1496 if (self.grid) |*grid| { 1497 grid.deinit(); 1498 self.grid = null; 1499 } 1500 } 1501 1502 pub fn getGrid(self: Footer) ?Grid { 1503 return self.grid; 1504 } 1505 1506 pub fn getFocus(self: *Footer) *Focus { 1507 return self.focus; 1508 } 1509 }; 1510 1511 // a single-child wrapper that builds the child at its natural size and 1512 // positions its grid in the middle of the area granted by the parent 1513 pub const Center = struct { 1514 focus: *Focus, 1515 grid: ?Grid, 1516 child: *Widget, 1517 1518 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1519 const child = try allocator.create(Widget); 1520 errdefer allocator.destroy(child); 1521 child.* = child_widget; 1522 return .{ 1523 .focus = try Focus.create(allocator, .container), 1524 .grid = null, 1525 .child = child, 1526 }; 1527 } 1528 1529 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1530 self.focus.destroy(allocator); 1531 if (self.grid) |*grid| { 1532 grid.deinit(); 1533 self.grid = null; 1534 } 1535 self.child.deinit(allocator); 1536 allocator.destroy(self.child); 1537 } 1538 1539 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1540 self.clearGrid(); 1541 self.getFocus().clear(); 1542 1543 // build the child without forcing it to fill min_size; it sizes 1544 // itself to its content within the available max. 1545 try self.child.build(allocator, .{ 1546 .min_size = .{ .width = null, .height = null }, 1547 .max_size = constraint.max_size, 1548 }, root_focus); 1549 1550 const child_grid = self.child.getGrid() orelse return; 1551 1552 // prefer max when given; otherwise grow to max(min, child) so that 1553 // when only a min is set (e.g. the wasm path passes viewport rows 1554 // as min_height with no max), we can still vertically center while 1555 // letting taller content extend past the min. 1556 const width = if (constraint.max_size.width) |w| w else if (constraint.min_size.width) |min_w| @max(min_w, child_grid.size.width) else child_grid.size.width; 1557 const height = if (constraint.max_size.height) |h| h else if (constraint.min_size.height) |min_h| @max(min_h, child_grid.size.height) else child_grid.size.height; 1558 if (width == 0 or height == 0) return; 1559 1560 const offset_x: usize = (width -| child_grid.size.width) / 2; 1561 const offset_y: usize = (height -| child_grid.size.height) / 2; 1562 1563 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1564 errdefer grid.deinit(); 1565 try grid.drawGrid(child_grid, offset_x, offset_y); 1566 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1567 self.getFocus().child_id = self.child.getFocus().id; 1568 1569 self.grid = grid; 1570 } 1571 1572 pub fn input(self: *Center, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1573 try self.child.input(allocator, key, root_focus); 1574 } 1575 1576 pub fn clearGrid(self: *Center) void { 1577 if (self.grid) |*grid| { 1578 grid.deinit(); 1579 self.grid = null; 1580 } 1581 self.child.clearGrid(); 1582 } 1583 1584 pub fn getGrid(self: Center) ?Grid { 1585 return self.grid; 1586 } 1587 1588 pub fn getFocus(self: *Center) *Focus { 1589 return self.focus; 1590 } 1591 }; 1592 1593 // renders truecolor ANSI art 1594 pub const AnsiArt = struct { 1595 focus: *Focus, 1596 grid: ?Grid, 1597 content: []const u8, 1598 1599 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1600 return .{ 1601 .focus = try Focus.create(allocator, .container), 1602 .grid = null, 1603 .content = content, 1604 }; 1605 } 1606 1607 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1608 self.focus.destroy(allocator); 1609 if (self.grid) |*grid| { 1610 grid.deinit(); 1611 self.grid = null; 1612 } 1613 } 1614 1615 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1616 _ = root_focus; 1617 self.clearGrid(); 1618 1619 // parse into rows of cells, then copy into a rectangular grid 1620 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1621 defer { 1622 for (rows.items) |*row| row.deinit(allocator); 1623 rows.deinit(allocator); 1624 } 1625 var row: std.ArrayList(Grid.Cell) = .empty; 1626 errdefer row.deinit(allocator); 1627 1628 var style: Grid.Style = .{}; 1629 var width: usize = 0; 1630 const content = self.content; 1631 var i: usize = 0; 1632 while (i < content.len) { 1633 const byte = content[i]; 1634 if (byte == '\n') { 1635 width = @max(width, row.items.len); 1636 try rows.append(allocator, row); 1637 row = .empty; 1638 i += 1; 1639 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1640 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1641 var j = i + 2; 1642 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1643 if (j >= content.len) { 1644 i = content.len; // malformed trailing escape; stop 1645 } else { 1646 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1647 i = j + 1; 1648 } 1649 } else { 1650 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1651 const end = @min(content.len, i + len); 1652 const rune = content[i..end]; 1653 const transparent = end - i == 1 and rune[0] == ' ' and 1654 style.fg == null and style.bg == null and !style.inverted; 1655 try row.append(allocator, .{ 1656 .rune = if (transparent) null else rune, 1657 .style = style, 1658 }); 1659 i = end; 1660 } 1661 } 1662 // a trailing row with content but no closing newline 1663 if (row.items.len > 0) { 1664 width = @max(width, row.items.len); 1665 try rows.append(allocator, row); 1666 } else { 1667 row.deinit(allocator); 1668 } 1669 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1670 1671 const height = rows.items.len; 1672 if (width == 0 or height == 0) return; 1673 1674 const clamped_w = @min(width, constraint.max_size.width orelse width); 1675 const clamped_h = @min(height, constraint.max_size.height orelse height); 1676 1677 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1678 errdefer grid.deinit(); 1679 for (rows.items[0..clamped_h], 0..) |r, y| { 1680 const n = @min(r.items.len, clamped_w); 1681 for (r.items[0..n], 0..) |cell, x| { 1682 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1683 } 1684 } 1685 self.grid = grid; 1686 } 1687 1688 pub fn clearGrid(self: *AnsiArt) void { 1689 if (self.grid) |*grid| { 1690 grid.deinit(); 1691 self.grid = null; 1692 } 1693 } 1694 1695 pub fn getGrid(self: AnsiArt) ?Grid { 1696 return self.grid; 1697 } 1698 1699 pub fn getFocus(self: *AnsiArt) *Focus { 1700 return self.focus; 1701 } 1702 1703 fn applySgr(style: *Grid.Style, params: []const u8) void { 1704 var nums: [16]u32 = undefined; 1705 var n: usize = 0; 1706 var it = std.mem.splitScalar(u8, params, ';'); 1707 while (it.next()) |tok| { 1708 if (n >= nums.len) break; 1709 // an empty parameter (e.g. bare "\x1b[m") means 0 1710 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1711 n += 1; 1712 } 1713 if (n == 0) { 1714 style.* = .{}; 1715 return; 1716 } 1717 var i: usize = 0; 1718 while (i < n) : (i += 1) { 1719 switch (nums[i]) { 1720 0 => style.* = .{}, 1721 7 => style.inverted = true, 1722 27 => style.inverted = false, 1723 39 => style.fg = null, 1724 49 => style.bg = null, 1725 38, 48 => { 1726 // truecolor form: 38;2;r;g;b — anything else is ignored 1727 if (i + 4 < n and nums[i + 1] == 2) { 1728 const c = Grid.Color{ 1729 .r = @truncate(nums[i + 2]), 1730 .g = @truncate(nums[i + 3]), 1731 .b = @truncate(nums[i + 4]), 1732 }; 1733 if (nums[i] == 38) style.fg = c else style.bg = c; 1734 i += 4; 1735 } 1736 }, 1737 else => {}, 1738 } 1739 } 1740 } 1741 }; 1742 1743 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1744 pub const AnsiBackground = struct { 1745 grid: ?Grid, 1746 child: *Widget, 1747 art: AnsiArt, 1748 session: *Session, 1749 1750 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 1751 var cw = child_widget; 1752 const child = allocator.create(Widget) catch |e| { 1753 cw.deinit(allocator); 1754 return e; 1755 }; 1756 child.* = cw; 1757 errdefer { 1758 child.deinit(allocator); 1759 allocator.destroy(child); 1760 } 1761 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 1762 } 1763 1764 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1765 if (self.grid) |*grid| grid.deinit(); 1766 self.art.deinit(allocator); 1767 self.child.deinit(allocator); 1768 allocator.destroy(self.child); 1769 } 1770 1771 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1772 self.clearGrid(); 1773 1774 // make the wrapped page fill the whole available area 1775 try self.child.build(allocator, .{ 1776 .min_size = .{ 1777 .width = constraint.max_size.width orelse constraint.min_size.width, 1778 .height = constraint.max_size.height orelse constraint.min_size.height, 1779 }, 1780 .max_size = constraint.max_size, 1781 }, root_focus); 1782 1783 if (self.session.data.enable_ansi) { 1784 if (self.child.getGrid()) |fg| { 1785 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 1786 } 1787 } 1788 } 1789 1790 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 1791 try self.child.input(allocator, key, root_focus); 1792 } 1793 1794 pub fn clearGrid(self: *AnsiBackground) void { 1795 if (self.grid) |*grid| { 1796 grid.deinit(); 1797 self.grid = null; 1798 } 1799 self.child.clearGrid(); 1800 self.art.clearGrid(); 1801 } 1802 1803 pub fn getGrid(self: AnsiBackground) ?Grid { 1804 return self.grid orelse self.child.getGrid(); 1805 } 1806 1807 pub fn getFocus(self: *AnsiBackground) *Focus { 1808 return self.child.getFocus(); 1809 } 1810 1811 // which top corner the art hugs. 1812 const ArtAnchor = enum { top_left, top_right }; 1813 1814 // a foreground cell counts as blank if it's empty or a bare space without styling 1815 fn cellIsBlank(cell: Grid.Cell) bool { 1816 if (cell.rune) |rune| { 1817 return std.mem.eql(u8, rune, " ") and 1818 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1819 } 1820 return true; 1821 } 1822 1823 // the art is dimmed to this fraction of its brightness so foreground text 1824 // stays legible over it 1825 const art_brightness = 45; // percent 1826 1827 fn dimColor(color: ?Grid.Color) ?Grid.Color { 1828 const c = color orelse return null; 1829 return .{ 1830 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 1831 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 1832 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 1833 }; 1834 } 1835 1836 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1837 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1838 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1839 // snap such channels clear of that range so the backdrop doesn't blink or 1840 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1841 // actually render truecolor. 1842 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1843 const c = color orelse return null; 1844 const snap = struct { 1845 fn f(v: u8) u8 { 1846 return if (v >= 1 and v <= 9) 10 else v; 1847 } 1848 }.f; 1849 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 1850 } 1851 1852 // composites ANSI art behind a foreground grid 1853 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 1854 art.clearGrid(); 1855 try art.build(allocator, .{ 1856 .min_size = .{ .width = null, .height = null }, 1857 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1858 }, root_focus); 1859 1860 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1861 errdefer out.deinit(); 1862 1863 if (art.getGrid()) |art_grid| { 1864 const anchor_x = switch (anchor) { 1865 .top_left => 0, 1866 .top_right => foreground.size.width -| art_grid.size.width, 1867 }; 1868 for (0..art_grid.size.height) |y| { 1869 for (0..art_grid.size.width) |x| { 1870 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 1871 if (src.rune == null) continue; 1872 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 1873 const dst = &out.cells.items[idx]; 1874 // composite the dimmed art behind every cell so it shows 1875 // through the whole UI: a blank cell becomes the art, while a 1876 // cell with a glyph keeps the glyph but takes the dimmed art as 1877 // its background (replacing whatever background it had, which 1878 // would otherwise obscure the art). 1879 if (cellIsBlank(dst.*)) { 1880 dst.* = src; 1881 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 1882 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 1883 } else { 1884 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 1885 } 1886 } 1887 } 1888 } 1889 return out; 1890 } 1891 }; 1892