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