╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1672 _ = root_focus; 1673 self.clearGrid(); 1674 const width = constraint.max_size.width orelse return; 1675 if (width == 0) return; 1676 self.grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1677 } 1678 1679 pub fn input(self: *Spacer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1680 _ = self; 1681 _ = allocator; 1682 _ = key; 1683 _ = root_focus; 1684 } 1685 1686 pub fn clearGrid(self: *Spacer) void { 1687 if (self.grid) |*grid| { 1688 grid.deinit(); 1689 self.grid = null; 1690 } 1691 } 1692 1693 pub fn getGrid(self: Spacer) ?Grid { 1694 return self.grid; 1695 } 1696 1697 pub fn getFocus(self: *Spacer) *Focus { 1698 return self.focus; 1699 } 1700 }; 1701 1702 // a one-row, non-focusable status bar showing the current page's url 1703 pub const Footer = struct { 1704 focus: *Focus, 1705 grid: ?Grid, 1706 session: *Session, 1707 arena: std.heap.ArenaAllocator, 1708 1709 pub fn init(allocator: std.mem.Allocator, session: *Session) !Footer { 1710 return .{ 1711 .focus = try Focus.create(allocator, .container), 1712 .grid = null, 1713 .session = session, 1714 .arena = std.heap.ArenaAllocator.init(allocator), 1715 }; 1716 } 1717 1718 pub fn deinit(self: *Footer, allocator: std.mem.Allocator) void { 1719 self.focus.destroy(allocator); 1720 if (self.grid) |*grid| grid.deinit(); 1721 self.arena.deinit(); 1722 } 1723 1724 pub fn build(self: *Footer, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1725 _ = root_focus; 1726 self.clearGrid(); 1727 const width = constraint.max_size.width orelse return; 1728 if (width == 0) return; 1729 1730 _ = self.arena.reset(.retain_capacity); 1731 const aa = self.arena.allocator(); 1732 const page = self.session.data.current_page; 1733 const path = (if (self.session.local != null) page.urlAllocLocal(&self.arena) else page.urlAlloc(&self.arena)) catch return; 1734 const text = if (self.session.web_port) |port| 1735 std.fmt.allocPrint(aa, "http://localhost:{d}{s}", .{ port, path }) catch return 1736 else 1737 path; 1738 1739 var grid = try Grid.init(allocator, .{ .width = width, .height = 1 }); 1740 errdefer grid.deinit(); 1741 var utf8 = (std.unicode.Utf8View.init(text) catch return).iterator(); 1742 var i: usize = 0; 1743 while (utf8.nextCodepointSlice()) |ch| { 1744 if (i == width) break; 1745 grid.cells.items[try grid.cells.at(.{ 0, i })].rune = ch; 1746 i += 1; 1747 } 1748 self.grid = grid; 1749 } 1750 1751 pub fn input(self: *Footer, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1752 _ = self; 1753 _ = allocator; 1754 _ = key; 1755 _ = root_focus; 1756 } 1757 1758 pub fn clearGrid(self: *Footer) void { 1759 if (self.grid) |*grid| { 1760 grid.deinit(); 1761 self.grid = null; 1762 } 1763 } 1764 1765 pub fn getGrid(self: Footer) ?Grid { 1766 return self.grid; 1767 } 1768 1769 pub fn getFocus(self: *Footer) *Focus { 1770 return self.focus; 1771 } 1772 }; 1773 1774 // a single-child wrapper that builds the child at its natural size and 1775 // positions its grid in the middle of the area granted by the parent 1776 pub const Center = struct { 1777 focus: *Focus, 1778 grid: ?Grid, 1779 child: *Widget, 1780 1781 pub fn init(allocator: std.mem.Allocator, child_widget: Widget) !Center { 1782 const child = try allocator.create(Widget); 1783 errdefer allocator.destroy(child); 1784 child.* = child_widget; 1785 return .{ 1786 .focus = try Focus.create(allocator, .container), 1787 .grid = null, 1788 .child = child, 1789 }; 1790 } 1791 1792 pub fn deinit(self: *Center, allocator: std.mem.Allocator) void { 1793 self.focus.destroy(allocator); 1794 if (self.grid) |*grid| { 1795 grid.deinit(); 1796 self.grid = null; 1797 } 1798 self.child.deinit(allocator); 1799 allocator.destroy(self.child); 1800 } 1801 1802 pub fn build(self: *Center, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1803 self.clearGrid(); 1804 self.getFocus().clear(); 1805 1806 // build the child without forcing it to fill min_size; it sizes 1807 // itself to its content within the available max. 1808 try self.child.build(allocator, .{ 1809 .min_size = .{ .width = null, .height = null }, 1810 .max_size = constraint.max_size, 1811 }, root_focus); 1812 1813 const child_grid = self.child.getGrid() orelse return; 1814 1815 // prefer max when given; otherwise grow to max(min, child) so that 1816 // when only a min is set (e.g. the wasm path passes viewport rows 1817 // as min_height with no max), we can still vertically center while 1818 // letting taller content extend past the min. 1819 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; 1820 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; 1821 if (width == 0 or height == 0) return; 1822 1823 const offset_x: usize = (width -| child_grid.size.width) / 2; 1824 const offset_y: usize = (height -| child_grid.size.height) / 2; 1825 1826 var grid = try Grid.init(allocator, .{ .width = width, .height = height }); 1827 errdefer grid.deinit(); 1828 try grid.drawGrid(child_grid, offset_x, offset_y); 1829 try self.getFocus().addChild(allocator, self.child.getFocus(), child_grid.size, offset_x, offset_y); 1830 self.getFocus().child_id = self.child.getFocus().id; 1831 1832 self.grid = grid; 1833 } 1834 1835 pub fn input(self: *Center, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1836 try self.child.input(allocator, key, root_focus); 1837 } 1838 1839 pub fn clearGrid(self: *Center) void { 1840 if (self.grid) |*grid| { 1841 grid.deinit(); 1842 self.grid = null; 1843 } 1844 self.child.clearGrid(); 1845 } 1846 1847 pub fn getGrid(self: Center) ?Grid { 1848 return self.grid; 1849 } 1850 1851 pub fn getFocus(self: *Center) *Focus { 1852 return self.focus; 1853 } 1854 }; 1855 1856 // renders truecolor ANSI art 1857 pub const AnsiArt = struct { 1858 focus: *Focus, 1859 grid: ?Grid, 1860 content: []const u8, 1861 1862 pub fn init(allocator: std.mem.Allocator, content: []const u8) !AnsiArt { 1863 return .{ 1864 .focus = try Focus.create(allocator, .container), 1865 .grid = null, 1866 .content = content, 1867 }; 1868 } 1869 1870 pub fn deinit(self: *AnsiArt, allocator: std.mem.Allocator) void { 1871 self.focus.destroy(allocator); 1872 if (self.grid) |*grid| { 1873 grid.deinit(); 1874 self.grid = null; 1875 } 1876 } 1877 1878 pub fn build(self: *AnsiArt, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1879 _ = root_focus; 1880 self.clearGrid(); 1881 1882 // parse into rows of cells, then copy into a rectangular grid 1883 var rows: std.ArrayList(std.ArrayList(Grid.Cell)) = .empty; 1884 defer { 1885 for (rows.items) |*row| row.deinit(allocator); 1886 rows.deinit(allocator); 1887 } 1888 var row: std.ArrayList(Grid.Cell) = .empty; 1889 errdefer row.deinit(allocator); 1890 1891 var style: Grid.Style = .{}; 1892 var width: usize = 0; 1893 const content = self.content; 1894 var i: usize = 0; 1895 while (i < content.len) { 1896 const byte = content[i]; 1897 if (byte == '\n') { 1898 width = @max(width, row.items.len); 1899 try rows.append(allocator, row); 1900 row = .empty; 1901 i += 1; 1902 } else if (byte == 0x1B and i + 1 < content.len and content[i + 1] == '[') { 1903 // CSI: scan to the final byte (0x40..0x7E); apply it if it's 'm' 1904 var j = i + 2; 1905 while (j < content.len and !(content[j] >= 0x40 and content[j] <= 0x7E)) j += 1; 1906 if (j >= content.len) { 1907 i = content.len; // malformed trailing escape; stop 1908 } else { 1909 if (content[j] == 'm') applySgr(&style, content[i + 2 .. j]); 1910 i = j + 1; 1911 } 1912 } else { 1913 const len = std.unicode.utf8ByteSequenceLength(byte) catch 1; 1914 const end = @min(content.len, i + len); 1915 const rune = content[i..end]; 1916 const transparent = end - i == 1 and rune[0] == ' ' and 1917 style.fg == null and style.bg == null and !style.inverted; 1918 try row.append(allocator, .{ 1919 .rune = if (transparent) null else rune, 1920 .style = style, 1921 }); 1922 i = end; 1923 } 1924 } 1925 // a trailing row with content but no closing newline 1926 if (row.items.len > 0) { 1927 width = @max(width, row.items.len); 1928 try rows.append(allocator, row); 1929 } else { 1930 row.deinit(allocator); 1931 } 1932 row = .empty; // ownership moved into rows (or freed); keep errdefer safe 1933 1934 const height = rows.items.len; 1935 if (width == 0 or height == 0) return; 1936 1937 const clamped_w = @min(width, constraint.max_size.width orelse width); 1938 const clamped_h = @min(height, constraint.max_size.height orelse height); 1939 1940 var grid = try Grid.init(allocator, .{ .width = clamped_w, .height = clamped_h }); 1941 errdefer grid.deinit(); 1942 for (rows.items[0..clamped_h], 0..) |r, y| { 1943 const n = @min(r.items.len, clamped_w); 1944 for (r.items[0..n], 0..) |cell, x| { 1945 grid.cells.items[try grid.cells.at(.{ y, x })] = cell; 1946 } 1947 } 1948 self.grid = grid; 1949 } 1950 1951 pub fn clearGrid(self: *AnsiArt) void { 1952 if (self.grid) |*grid| { 1953 grid.deinit(); 1954 self.grid = null; 1955 } 1956 } 1957 1958 pub fn getGrid(self: AnsiArt) ?Grid { 1959 return self.grid; 1960 } 1961 1962 pub fn getFocus(self: *AnsiArt) *Focus { 1963 return self.focus; 1964 } 1965 1966 fn applySgr(style: *Grid.Style, params: []const u8) void { 1967 var nums: [16]u32 = undefined; 1968 var n: usize = 0; 1969 var it = std.mem.splitScalar(u8, params, ';'); 1970 while (it.next()) |tok| { 1971 if (n >= nums.len) break; 1972 // an empty parameter (e.g. bare "\x1b[m") means 0 1973 nums[n] = std.fmt.parseInt(u32, tok, 10) catch 0; 1974 n += 1; 1975 } 1976 if (n == 0) { 1977 style.* = .{}; 1978 return; 1979 } 1980 var i: usize = 0; 1981 while (i < n) : (i += 1) { 1982 switch (nums[i]) { 1983 0 => style.* = .{}, 1984 7 => style.inverted = true, 1985 27 => style.inverted = false, 1986 39 => style.fg = null, 1987 49 => style.bg = null, 1988 38, 48 => { 1989 // truecolor form: 38;2;r;g;b — anything else is ignored 1990 if (i + 4 < n and nums[i + 1] == 2) { 1991 const c = Grid.Color{ 1992 .r = @truncate(nums[i + 2]), 1993 .g = @truncate(nums[i + 3]), 1994 .b = @truncate(nums[i + 4]), 1995 }; 1996 if (nums[i] == 38) style.fg = c else style.bg = c; 1997 i += 4; 1998 } 1999 }, 2000 else => {}, 2001 } 2002 } 2003 } 2004 }; 2005 2006 // a full-screen wrapper that renders ANSI art behind whatever page it wraps 2007 pub const AnsiBackground = struct { 2008 grid: ?Grid, 2009 child: *Widget, 2010 art: AnsiArt, 2011 session: *Session, 2012 2013 pub fn init(allocator: std.mem.Allocator, child_widget: Widget, art_content: []const u8, session: *Session) !AnsiBackground { 2014 var cw = child_widget; 2015 const child = allocator.create(Widget) catch |e| { 2016 cw.deinit(allocator); 2017 return e; 2018 }; 2019 child.* = cw; 2020 errdefer { 2021 child.deinit(allocator); 2022 allocator.destroy(child); 2023 } 2024 return .{ .grid = null, .child = child, .art = try AnsiArt.init(allocator, art_content), .session = session }; 2025 } 2026 2027 pub fn deinit(self: *AnsiBackground, allocator: std.mem.Allocator) void { 2028 if (self.grid) |*grid| grid.deinit(); 2029 self.art.deinit(allocator); 2030 self.child.deinit(allocator); 2031 allocator.destroy(self.child); 2032 } 2033 2034 pub fn build(self: *AnsiBackground, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 2035 self.clearGrid(); 2036 2037 // make the wrapped page fill the whole available area 2038 try self.child.build(allocator, .{ 2039 .min_size = .{ 2040 .width = constraint.max_size.width orelse constraint.min_size.width, 2041 .height = constraint.max_size.height orelse constraint.min_size.height, 2042 }, 2043 .max_size = constraint.max_size, 2044 }, root_focus); 2045 2046 if (self.session.data.enable_ansi) { 2047 if (self.child.getGrid()) |fg| { 2048 self.grid = try artBehind(allocator, fg, &self.art, .top_right, root_focus); 2049 } 2050 } 2051 } 2052 2053 pub fn input(self: *AnsiBackground, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 2054 try self.child.input(allocator, key, root_focus); 2055 } 2056 2057 pub fn clearGrid(self: *AnsiBackground) void { 2058 if (self.grid) |*grid| { 2059 grid.deinit(); 2060 self.grid = null; 2061 } 2062 self.child.clearGrid(); 2063 self.art.clearGrid(); 2064 } 2065 2066 pub fn getGrid(self: AnsiBackground) ?Grid { 2067 return self.grid orelse self.child.getGrid(); 2068 } 2069 2070 pub fn getFocus(self: *AnsiBackground) *Focus { 2071 return self.child.getFocus(); 2072 } 2073 2074 // which top corner the art hugs. 2075 const ArtAnchor = enum { top_left, top_right }; 2076 2077 // a foreground cell counts as blank if it's empty or a bare space without styling 2078 fn cellIsBlank(cell: Grid.Cell) bool { 2079 if (cell.rune) |rune| { 2080 return std.mem.eql(u8, rune, " ") and 2081 cell.style.fg == null and cell.style.bg == null and !cell.style.inverted; 2082 } 2083 return true; 2084 } 2085 2086 // the art is dimmed to this fraction of its brightness so foreground text 2087 // stays legible over it 2088 const art_brightness = 45; // percent 2089 2090 fn dimColor(color: ?Grid.Color) ?Grid.Color { 2091 const c = color orelse return null; 2092 return .{ 2093 .r = @intCast(@as(u16, c.r) * art_brightness / 100), 2094 .g = @intCast(@as(u16, c.g) * art_brightness / 100), 2095 .b = @intCast(@as(u16, c.b) * art_brightness / 100), 2096 }; 2097 } 2098 2099 // terminals that don't support truecolor misparse a "38;2;r;g;b"/"48;2;…" 2100 // SGR as a list of plain SGR codes, so any channel value in 1..9 turns on 2101 // a text attribute (5/6 = blink, 7 = inverse, 8 = conceal, …). 2102 // snap such channels clear of that range so the backdrop doesn't blink or 2103 // hide text there; the ≤9/255 shift is imperceptible on terminals that 2104 // actually render truecolor. 2105 fn sgrSafe(color: ?Grid.Color) ?Grid.Color { 2106 const c = color orelse return null; 2107 const snap = struct { 2108 fn f(v: u8) u8 { 2109 return if (v >= 1 and v <= 9) 10 else v; 2110 } 2111 }.f; 2112 return .{ .r = snap(c.r), .g = snap(c.g), .b = snap(c.b) }; 2113 } 2114 2115 // composites ANSI art behind a foreground grid 2116 fn artBehind(allocator: std.mem.Allocator, foreground: Grid, art: *AnsiArt, anchor: ArtAnchor, root_focus: *Focus) !Grid { 2117 art.clearGrid(); 2118 try art.build(allocator, .{ 2119 .min_size = .{ .width = null, .height = null }, 2120 .max_size = .{ .width = foreground.size.width, .height = foreground.size.height }, 2121 }, root_focus); 2122 2123 var out = try Grid.initFromGrid(allocator, foreground, foreground.size, 0, 0); 2124 errdefer out.deinit(); 2125 2126 if (art.getGrid()) |art_grid| { 2127 const anchor_x = switch (anchor) { 2128 .top_left => 0, 2129 .top_right => foreground.size.width -| art_grid.size.width, 2130 }; 2131 for (0..art_grid.size.height) |y| { 2132 for (0..art_grid.size.width) |x| { 2133 const src = art_grid.cells.items[try art_grid.cells.at(.{ y, x })]; 2134 if (src.rune == null) continue; 2135 const idx = out.cells.at(.{ y, anchor_x + x }) catch continue; 2136 const dst = &out.cells.items[idx]; 2137 // composite the dimmed art behind every cell so it shows 2138 // through the whole UI: a blank cell becomes the art, while a 2139 // cell with a glyph keeps the glyph but takes the dimmed art as 2140 // its background (replacing whatever background it had, which 2141 // would otherwise obscure the art). 2142 if (cellIsBlank(dst.*)) { 2143 dst.* = src; 2144 dst.style.fg = sgrSafe(dimColor(dst.style.fg)); 2145 dst.style.bg = sgrSafe(dimColor(dst.style.bg)); 2146 } else { 2147 dst.style.bg = sgrSafe(dimColor(src.style.bg orelse src.style.fg)); 2148 } 2149 } 2150 } 2151 } 2152 return out; 2153 } 2154 }; 2155