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