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