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