╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ widget.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1513 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1514 self.clearGrid(); 1515 self.getFocus().clear(); 1516 1517 // build the child without forcing it to fill min_size; it sizes 1518 // itself to its content within the available max. 1519 try self.child.build(allocator, .{ 1520 .min_size = .{ .width = null, .height = null }, 1521 .max_size = constraint.max_size, 1522 }, root_focus); 1523 1524 const child_grid = self.child.getGrid() orelse return; 1525 1526 // prefer max when given; otherwise grow to max(min, child) so that 1527 // when only a min is set (e.g. the wasm path passes viewport rows 1528 // as min_height with no max), we can still vertically center while 1529 // letting taller content extend past the min. 1530 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; 1531 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; 1532 if (width == 0 or height == 0) return; 1533 1534 const offset_x: usize = (width -| child_grid.size.width) / 2; 1535 const offset_y: usize = (height -| child_grid.size.height) / 2; 1536 1537 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1538 errdefer grid.deinit(); 1539 try grid.drawGrid(child_grid, offset_x, offset_y); 1540 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1541 self.getFocus().child_id = self.child.getFocus().id; 1542 1543 self.grid = grid; 1544 } 1545 1546 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1547 try self.child.input(allocator, key, root_focus); 1548 } 1549 1550 pub fn clearGrid(self: *Center) void { 1551 if (self.grid) |*grid| { 1552 grid.deinit(); 1553 self.grid = null; 1554 } 1555 self.child.clearGrid(); 1556 } 1557 1558 pub fn getGrid(self: Center) ?Grid { 1559 return self.grid; 1560 } 1561 1562 pub fn getFocus(self: *Center) *Focus { 1563 return self.focus; 1564 } 1565 }; 1566 1567 // renders truecolor ANSI art 1568 pub const AnsiArt = struct { 1569 focus: *Focus, 1570 grid: ?Grid, 1571 content: []const u8, 1572 1573 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1574 return .{ 1575 .focus = try Focus.create(allocator, .container), 1576 .grid = null, 1577 .content = content, 1578 }; 1579 } 1580 1581 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1582 self.focus.destroy(allocator); 1583 if (self.grid) |*grid| { 1584 grid.deinit(); 1585 self.grid = null; 1586 } 1587 } 1588 1589 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1590 _ = root_focus; 1591 self.clearGrid(); 1592 1593 // parse into rows of cells, then copy into a rectangular grid 1594 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1595 defer { 1596 for (rows.items) |*row| row.deinit(allocator); 1597 rows.deinit(allocator); 1598 } 1599 var row: std.ArrayList(Grid.Cell) = .empty; 1600 errdefer row.deinit(allocator); 1601 1602 var style: Grid.Style = .{}; 1603 var width: usize = 0; 1604 const content = self.content; 1605 var i: usize = 0; 1606 while (i < content.len) { 1607 const byte = content[i]; 1608 if (byte == '\n') { 1609 width = @max(width, row.items.len); 1610 try rows.append(allocator, row); 1611 row = .empty; 1612 i += 1; 1613 } else if (byte == '\r') { 1614 // git may checkout the ansi art with crlf line endings on windows 1615 i += 1; 1616 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1617 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1618 var j = i + 2; 1619 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1620 if (j >= content.len) { 1621 i = content.len; // malformed trailing escape; stop 1622 } else { 1623 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1624 i = j + 1; 1625 } 1626 } else { 1627 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1628 const end = @min(content.len, i + len); 1629 const rune = content[i..end]; 1630 const codepoint = std.unicode.utf8Decode(rune) catch '�'; 1631 // a foreground color does not make a space visible. only an 1632 // effective background makes it opaque; foreground state often 1633 // remains active through the padding at the end of a row. 1634 const effective_background = if (style.inverted) style.fg else style.bg; 1635 const transparent = end - i == 1 and rune[0] == ' ' and effective_background == null; 1636 try row.append(allocator, .{ 1637 .rune = if (transparent) null else codepoint, 1638 .style = style, 1639 }); 1640 if (!transparent and xitui.width.cellWidth(codepoint) == 2) { 1641 try row.append(allocator, .{ .rune = null, .style = style, .continuation = true }); 1642 } 1643 i = end; 1644 } 1645 } 1646 // a trailing row with content but no closing newline 1647 if (row.items.len > 0) { 1648 width = @max(width, row.items.len); 1649 try rows.append(allocator, row); 1650 } else { 1651 row.deinit(allocator); 1652 } 1653 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1654 1655 const height = rows.items.len; 1656 if (width == 0 or height == 0) return; 1657 1658 const clamped_w = @min(width, constraint.max_size.width orelse width); 1659 const clamped_h = @min(height, constraint.max_size.height orelse height); 1660 1661 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1662 errdefer grid.deinit(); 1663 for (rows.items[0..clamped_h], 0..) |r, y| { 1664 const n = @min(r.items.len, clamped_w); 1665 for (r.items[0..n], 0..) |cell, x| { 1666 if (cell.continuation) continue; 1667 (try grid.cell(x, y)).style = cell.style; 1668 try grid.setRune(x, y, cell.rune); 1669 } 1670 } 1671 self.grid = grid; 1672 } 1673 1674 pub fn clearGrid(self: *AnsiArt) void { 1675 if (self.grid) |*grid| { 1676 grid.deinit(); 1677 self.grid = null; 1678 } 1679 } 1680 1681 pub fn getGrid(self: AnsiArt) ?Grid { 1682 return self.grid; 1683 } 1684 1685 pub fn getFocus(self: *AnsiArt) *Focus { 1686 return self.focus; 1687 } 1688 1689 fn applySgr(style: *Grid.Style, params: []const u8) void { 1690 var nums: [16]u32 = undefined; 1691 var n: usize = 0; 1692 var it = std.mem.splitScalar(u8, params, ';'); 1693 while (it.next()) |tok| { 1694 if (n >= nums.len) break; 1695 // an empty parameter (e.g. bare "\x1b[m") means 0 1696 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1697 n += 1; 1698 } 1699 if (n == 0) { 1700 style.* = .{}; 1701 return; 1702 } 1703 var i: usize = 0; 1704 while (i < n) : (i += 1) { 1705 switch (nums[i]) { 1706 0 => style.* = .{}, 1707 7 => style.inverted = true, 1708 27 => style.inverted = false, 1709 39 => style.fg = null, 1710 49 => style.bg = null, 1711 38, 48 => { 1712 // truecolor form: 38;2;r;g;b — anything else is ignored 1713 if (i + 4 < n and nums[i + 1] == 2) { 1714 const c = Grid.Color{ .rgb = .{ 1715 .r = @truncate(nums[i + 2]), 1716 .g = @truncate(nums[i + 3]), 1717 .b = @truncate(nums[i + 4]), 1718 } }; 1719 if (nums[i] == 38) style.fg = c else style.bg = c; 1720 i += 4; 1721 } 1722 }, 1723 else => {}, 1724 } 1725 } 1726 } 1727 }; 1728 1729 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 1730 pub const AnsiBackground = struct { 1731 grid: ?Grid, 1732 child: *Widget, 1733 art: AnsiArt, 1734 session: *ui.Session, 1735 1736 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, session: *ui.Session) !AnsiBackground { 1737 var cw = child_widget; 1738 const child = allocator.create(Widget) catch |e| { 1739 cw.deinit(allocator); 1740 return e; 1741 }; 1742 child.* = cw; 1743 errdefer { 1744 child.deinit(allocator); 1745 allocator.destroy(child); 1746 } 1747 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, session.data.ansi_art), .session = session }; 1748 } 1749 1750 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 1751 if (self.grid) |*grid| grid.deinit(); 1752 self.art.deinit(allocator); 1753 self.child.deinit(allocator); 1754 allocator.destroy(self.child); 1755 } 1756 1757 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1758 self.clearGrid(); 1759 1760 // make the wrapped page fill the whole available area 1761 try self.child.build(allocator, .{ 1762 .min_size = .{ 1763 .width = constraint.max_size.width orelse constraint.min_size.width, 1764 .height = constraint.max_size.height orelse constraint.min_size.height, 1765 }, 1766 .max_size = constraint.max_size, 1767 }, root_focus); 1768 1769 const foreground = self.child.getGrid() orelse return; 1770 if (!self.session.color) return; 1771 // terminals show the art only once they report a background to fade it toward 1772 const backdrop: ?Grid.Color.Rgb = if (self.session.is_terminal) self.session.terminal_background orelse return else null; 1773 self.art.content = self.session.data.ansi_art; 1774 try self.art.build(allocator, .{ 1775 .min_size = .{ .width = null, .height = null }, 1776 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 1777 }, root_focus); 1778 // the web fades the art with css opacity instead 1779 const art_backdrop = backdrop orelse return; 1780 if (self.art.grid) |*art_grid| { 1781 for (art_grid.cells) |*cell| { 1782 cell.style.fg = fadeColor(cell.style.fg, art_backdrop); 1783 cell.style.bg = fadeColor(cell.style.bg, art_backdrop); 1784 } 1785 self.grid = try artBehind(allocator, foreground, art_grid.*); 1786 } 1787 } 1788 1789 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1790 try self.child.input(allocator, key, root_focus); 1791 } 1792 1793 pub fn clearGrid(self: *AnsiBackground) void { 1794 if (self.grid) |*grid| { 1795 grid.deinit(); 1796 self.grid = null; 1797 } 1798 self.child.clearGrid(); 1799 self.art.clearGrid(); 1800 } 1801 1802 pub fn getGrid(self: AnsiBackground) ?Grid { 1803 return self.grid orelse self.child.getGrid(); 1804 } 1805 1806 pub fn getFocus(self: *AnsiBackground) *Focus { 1807 return self.child.getFocus(); 1808 } 1809 1810 // a foreground cell counts as blank if it's empty or a bare space without styling 1811 fn cellIsBlank(cell: Grid.Cell) bool { 1812 if (cell.continuation) return false; 1813 if (cell.rune) |rune| { 1814 return rune == ' ' and 1815 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 1816 } 1817 return true; 1818 } 1819 1820 // the art is faded toward the background at this opacity so foreground 1821 // text stays legible over it 1822 pub const art_opacity = 25; // percent 1823 1824 // the art parser only emits truecolor, so a palette color never occurs 1825 fn artRgb(color: ?Grid.Color) ?Grid.Color.Rgb { 1826 const c = color orelse return null; 1827 return switch (c) { 1828 .rgb => |v| v, 1829 else => null, 1830 }; 1831 } 1832 1833 fn fadeColor(color: ?Grid.Color, backdrop: Grid.Color.Rgb) ?Grid.Color { 1834 const v = artRgb(color) orelse return color; 1835 const blend = struct { 1836 fn f(c: u8, b: u8) u8 { 1837 return @intCast((@as(u16, c) * art_opacity + @as(u16, b) * (100 - art_opacity)) / 100); 1838 } 1839 }.f; 1840 return .{ .rgb = .{ .r = blend(v.r, backdrop.r), .g = blend(v.g, backdrop.g), .b = blend(v.b, backdrop.b) } }; 1841 } 1842 1843 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 1844 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 1845 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 1846 // snap such channels clear of that range so the backdrop doesn't blink or 1847 // hide text there; the ≤9/255 shift is imperceptible on terminals that 1848 // actually render truecolor. 1849 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 1850 const v = artRgb(color) orelse return color; 1851 const snap = struct { 1852 fn f(c: u8) u8 { 1853 return if (c >= 1 and c <= 9) 10 else c; 1854 } 1855 }.f; 1856 return .{ .rgb = .{ .r = snap(v.r), .g = snap(v.g), .b = snap(v.b) } }; 1857 } 1858 1859 // composites ANSI art behind a foreground grid 1860 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art_grid: Grid) !Grid { 1861 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 1862 errdefer out.deinit(); 1863 1864 const anchor_x = foreground.size.width -| art_grid.size.width; 1865 for (0..art_grid.size.height) |y| { 1866 for (0..art_grid.size.width) |x| { 1867 const src = (try art_grid.cell(x, y)).*; 1868 if (src.rune == null) continue; 1869 const dst = try out.cell(anchor_x + x, y); 1870 const wide = x + 1 < art_grid.size.width and (try art_grid.cell(x + 1, y)).continuation; 1871 const next_dst = if (wide) try out.cell(anchor_x + x + 1, y) else null; 1872 // blank cells take the art; occupied cells take its background 1873 if (cellIsBlank(dst.*) and (if (next_dst) |next| cellIsBlank(next.*) else true)) { 1874 dst.style = src.style; 1875 dst.style.fg = sgrSafe(dst.style.fg); 1876 dst.style.bg = sgrSafe(dst.style.bg); 1877 try out.setRune(anchor_x + x, y, src.rune); 1878 } else { 1879 const art_bg = sgrSafe(src.style.bg orelse src.style.fg); 1880 dst.style.bg = art_bg; 1881 if (next_dst) |next| next.style.bg = art_bg; 1882 } 1883 } 1884 } 1885 return out; 1886 } 1887 }; 1888 1889 pub const CopyableText = struct { 1890 pub const Choice = struct { 1891 selector: []const u8 = "", 1892 text: []const u8, 1893 copyable_text: ?[]const u8 = null, 1894 label: []const u8 = "", 1895 bottom_label: []const u8 = "", 1896 }; 1897 1898 box: wgt.Box(Widget), 1899 session: *ui.Session, 1900 choices: []Choice, 1901 selected: usize = 0, 1902 1903 pub fn init(allocator: std.mem.Allocator, session: *ui.Session, choices: []const Choice, initial: usize) !CopyableText { 1904 if (choices.len == 0) return error.MissingCopyableText; 1905 1906 const owned_choices = try allocator.dupe(Choice, choices); 1907 errdefer allocator.free(owned_choices); 1908 1909 var box = try wgt.Box(Widget).init(allocator, .{ .border = null, .direction = .horiz }); 1910 errdefer box.deinit(allocator); 1911 1912 if (choices.len > 1) for (choices) |choice| { 1913 var selector_box = try wgt.TextBox.init(allocator, choice.selector, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 1914 errdefer selector_box.deinit(allocator); 1915 selector_box.getFocus().mode = .all; 1916 try box.children.put(allocator, selector_box.getFocus().id, .{ .widget = .{ .text_box = selector_box }, .rect = null, .min_size = .{ .width = choice.selector.len + 2, .height = 3 } }); 1917 }; 1918 1919 var text_input = try wgt.TextInput.init(allocator, .{ 1920 .border = .single, 1921 .round_corners = true, 1922 .top_label = .{ .text = choices[initial].label }, 1923 .bottom_label = .{ .text = choices[initial].bottom_label }, 1924 .read_only = true, 1925 .render_content = session.is_terminal, 1926 .visible_width = null, 1927 }); 1928 errdefer text_input.deinit(allocator); 1929 text_input.getFocus().mode = .all; 1930 try text_input.setContent(allocator, choices[initial].text); 1931 box.getFocus().child_id = text_input.getFocus().id; 1932 try box.children.put(allocator, text_input.getFocus().id, .{ .widget = .{ .text_input = text_input }, .rect = null, .min_size = null, .flex = .shrink }); 1933 1934 return .{ .box = box, .session = session, .choices = owned_choices, .selected = initial }; 1935 } 1936 1937 pub fn initClone(allocator: std.mem.Allocator, session: *ui.Session, handle: ui.RepoHandle) !?CopyableText { 1938 const aa = session.page_arena.allocator(); 1939 var choices: [2]Choice = undefined; 1940 var count: usize = 0; 1941 var ssh_index: ?usize = null; 1942 switch (handle.location) { 1943 .repo => |identity| { 1944 if (session.data.git_http_port) |port| { 1945 const url = try aa.print("http://localhost:{d}/{s}", .{ port, identity }); 1946 choices[count] = .{ 1947 .selector = "http", 1948 .text = url, 1949 .copyable_text = try aa.print("git clone {s}", .{url}), 1950 .label = " clone ", 1951 }; 1952 count += 1; 1953 } 1954 if (session.data.git_ssh_port) |port| { 1955 ssh_index = count; 1956 const url = try aa.print("ssh://localhost:{d}/{s}", .{ port, identity }); 1957 choices[count] = .{ 1958 .selector = "ssh", 1959 .text = url, 1960 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1961 .label = " clone ", 1962 }; 1963 count += 1; 1964 } 1965 }, 1966 .fork => |f| if (session.data.git_ssh_port) |port| { 1967 const url = try aa.print("ssh://localhost:{d}/{s}+{s}", .{ port, f.identity, f.id }); 1968 choices[count] = .{ 1969 .text = url, 1970 .copyable_text = try aa.print("{s}git clone {s}", .{ session.data.git_ssh_prefix, url }), 1971 .label = " clone ", 1972 }; 1973 count += 1; 1974 }, 1975 } 1976 if (count == 0) return null; 1977 // a viewer who can push starts on the ssh url 1978 const initial = if (handle.canWrite()) ssh_index orelse 0 else 0; 1979 return @as(?CopyableText, try init(allocator, session, choices[0..count], initial)); 1980 } 1981 1982 pub fn minWidth(self: *const CopyableText) usize { 1983 var width = maxWidth(self.choices) + 2; 1984 if (self.choices.len > 1) { 1985 for (self.choices) |choice| width += choice.selector.len + 2; 1986 } 1987 return width; 1988 } 1989 1990 pub fn deinit(self: *CopyableText, allocator: std.mem.Allocator) void { 1991 self.box.deinit(allocator); 1992 allocator.free(self.choices); 1993 } 1994 1995 pub fn build(self: *CopyableText, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1996 self.clearGrid(); 1997 1998 if (self.choices.len > 1) { 1999 if (root_focus.grandchild_id) |id| { 2000 for (0..self.choices.len) |i| { 2001 if (id != self.selector(i).getFocus().id) continue; 2002 if (i != self.selected) try self.select(allocator, i); 2003 root_focus.setFocus(self.textInput().getFocus().id); 2004 break; 2005 } 2006 } 2007 2008 const focused = root_focus.grandchild_id == self.textInput().getFocus().id; 2009 for (0..self.choices.len) |i| { 2010 self.selector(i).options.border = if (i == self.selected) 2011 (if (focused) .double else .single) 2012 else 2013 .hidden; 2014 } 2015 } 2016 self.textInput().options.border = .single; 2017 self.textInput().cursor = 0; 2018 self.textInput().scroll_offset = 0; 2019 try self.box.build(allocator, constraint, root_focus); 2020 2021 if (!self.session.is_terminal) { 2022 const aa = self.session.arena.allocator(); 2023 const id = self.textInput().getFocus().id; 2024 try self.session.text_inputs.put(aa, id, self.textInput()); 2025 } 2026 } 2027 2028 pub fn input(self: *CopyableText, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2029 switch (key) { 2030 .arrow_left => if (self.selected > 0) try self.select(allocator, self.selected - 1), 2031 .arrow_right => if (self.selected + 1 < self.choices.len) try self.select(allocator, self.selected + 1), 2032 .enter => if (self.session.is_terminal) self.show(), 2033 .mouse => |mouse| { 2034 if (self.choices.len > 1) for (0..self.choices.len) |i| { 2035 if (!inp.leftClickOn(root_focus, self.selector(i).getFocus().id, mouse)) continue; 2036 try self.select(allocator, i); 2037 root_focus.setFocus(self.textInput().getFocus().id); 2038 return; 2039 }; 2040 if (self.session.is_terminal and !mouse.ctrl and inp.leftClickOn(root_focus, self.textInput().getFocus().id, mouse)) self.show(); 2041 }, 2042 else => {}, 2043 } 2044 } 2045 2046 fn select(self: *CopyableText, allocator: std.mem.Allocator, selected: usize) !void { 2047 self.selected = selected; 2048 const choice = self.choices[selected]; 2049 const text_input = self.textInput(); 2050 text_input.options.top_label.text = choice.label; 2051 text_input.options.bottom_label.text = choice.bottom_label; 2052 try text_input.setContent(allocator, choice.text); 2053 } 2054 2055 fn show(self: *CopyableText) void { 2056 const choice = self.choices[self.selected]; 2057 self.session.host_request = .{ .show_copyable_text = choice.copyable_text orelse choice.text }; 2058 } 2059 2060 fn textInput(self: *CopyableText) *wgt.TextInput { 2061 return &self.box.children.values()[if (self.choices.len > 1) self.choices.len else 0].widget.text_input; 2062 } 2063 2064 fn selector(self: *CopyableText, index: usize) *wgt.TextBox { 2065 return &self.box.children.values()[index].widget.text_box; 2066 } 2067 2068 fn maxWidth(choices: []const Choice) usize { 2069 var width: usize = 0; 2070 for (choices) |choice| width = @max(width, choice.text.len, choice.label.len, choice.bottom_label.len); 2071 return width; 2072 } 2073 2074 pub fn clearGrid(self: *CopyableText) void { 2075 self.box.clearGrid(); 2076 } 2077 2078 pub fn getGrid(self: CopyableText) ?Grid { 2079 return self.box.getGrid(); 2080 } 2081 2082 pub fn getFocus(self: *CopyableText) *Focus { 2083 return self.box.getFocus(); 2084 } 2085 }; 2086