╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1701 } 1702 if (clicked) |focus_id| { 1703 // follow a cross-page link 1704 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 1705 return session.navigate(route); 1706 } 1707 if (rawLink(root_focus, focus_id)) |url| { 1708 if (!mouse.ctrl) try requestRawLink(session, url); 1709 return; 1710 } 1711 root_focus.setFocus(focus_id); 1712 activate_in_page_link = true; 1713 } 1714 // forward the press into the widget tree so buttons (and any 1715 // future click-aware widgets) can react. widgets that don't 1716 // care about presses ignore it. 1717 try root.input(allocator, key, root_focus); 1718 } else { 1719 try root.input(allocator, key, root_focus); 1720 } 1721 }, 1722 else => try root.input(allocator, key, root_focus), 1723 } 1724 1725 const focused_after = root_focus.grandchild_id; 1726 if (!activate_in_page_link and focused_before == focused_after) return; 1727 const focus_id = focused_after orelse return; 1728 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 1729 } 1730 1731 // a link to bytes the server serves directly rather than to a page route, so 1732 // it never resolves to a RoutablePage 1733 pub const raw_link_prefix = "ax:"; 1734 1735 // a route already loaded into the current page; focusing it changes the url 1736 // without rebuilding the page 1737 pub const in_page_link_prefix = "ai:"; 1738 1739 // an in-page tab's base route, or the exact current route when it is selected 1740 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 1741 const target = if (selected) session.data.current_page else route; 1742 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 1743 } 1744 1745 // a button the web overlay covers with a file picker that posts the chosen 1746 // file to the url after the prefix 1747 pub const file_input_prefix = "file:"; 1748 1749 // a submit button that overrides its enclosing form's action 1750 pub const submit_action_prefix = "submit:"; 1751 1752 // the url an `ax:` link points at, or null for anything else. a host that 1753 // suppresses the anchor's own navigation needs this to follow the link. 1754 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 1755 const child = root_focus.children.get(focus_id) orelse return null; 1756 const custom = switch (child.focus.kind) { 1757 .custom => |c| c, 1758 else => return null, 1759 }; 1760 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 1761 return custom[raw_link_prefix.len..]; 1762 } 1763 1764 fn requestRawLink(session: *Session, url: []const u8) !void { 1765 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 1766 } 1767 1768 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 1769 return if (session.web_port) |port| 1770 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 1771 else 1772 path; 1773 } 1774 1775 // if the focus target at focus_id is an `a:` link that should navigate (a 1776 // different parent page, or a different files directory within the repo page), 1777 // return its route; otherwise null. lets a host turn a click / enter on such a 1778 // link into a navigation rather than a focus change. 1779 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1780 const current = data.current_page; 1781 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 1782 // a link to a different parent page always navigates; within a page, a 1783 // files-directory / commits-page / list-window change navigates while tab 1784 // links stay in-page. 1785 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 1786 return null; 1787 } 1788 1789 // the route carried by an `ai:` focus target, or null for any other target 1790 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 1791 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 1792 } 1793 1794 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 1795 const child = root_focus.children.get(focus_id) orelse return null; 1796 const custom = switch (child.focus.kind) { 1797 .custom => |c| c, 1798 else => return null, 1799 }; 1800 if (!std.mem.startsWith(u8, custom, prefix)) return null; 1801 const path = custom[prefix.len..]; 1802 // local sessions build (and parse) links with the repo identity elided 1803 return if (data.is_local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 1804 } 1805 1806 // a display author, resolved against the admin db at read time 1807 pub const Author = union(enum) { 1808 unknown, 1809 email: []const u8, // an email no user matches 1810 user_name: []const u8, 1811 1812 // the commit author line's email, resolved like initFromEmail 1813 pub fn init( 1814 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1815 arena: *std.heap.ArenaAllocator, 1816 author_line: []const u8, 1817 ) !Author { 1818 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 1819 } 1820 1821 // from a record's stored author email: it resolves to the user currently 1822 // holding it, else stays the email. the result is allocated in `arena`, 1823 // so the email may borrow transient memory. 1824 pub fn initFromEmail( 1825 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 1826 arena: *std.heap.ArenaAllocator, 1827 author_email: ?[]const u8, 1828 ) !Author { 1829 const email = author_email orelse return .unknown; 1830 if (haxy_moment) |moment| { 1831 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 1832 return .{ .user_name = user.event.name }; 1833 } 1834 } 1835 return .{ .email = try arena.allocator().dupe(u8, email) }; 1836 } 1837 }; 1838 1839 // the "a:" link to user `name`'s page. 1840 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 1841 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 1842 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 1843 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 1844 } 1845 1846 // a focusable " author " box showing `author`, linking to their user page 1847 // when it is a known user 1848 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox(Widget) { 1849 const text = switch (author) { 1850 .unknown => "", 1851 .email, .user_name => |t| t, 1852 }; 1853 var tb = try wgt.TextBox(Widget).init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 1854 errdefer tb.deinit(allocator); 1855 tb.getFocus().focusable = true; 1856 switch (author) { 1857 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 1858 else => {}, 1859 } 1860 return tb; 1861 } 1862 1863 // native-TUI navigation 1864 pub const Nav = struct { 1865 root: Widget, 1866 route: RoutablePage, 1867 // backs the current page (its Page data and the page-scoped strings its 1868 // widgets build). owned here, swapped on each navigation. session.page_arena 1869 // tracks whichever of these is current so widgets allocate into it. 1870 arena: *std.heap.ArenaAllocator, 1871 history: std.ArrayList(Entry), 1872 1873 // each retained page keeps its own arena, freed when the page leaves the 1874 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 1875 // long-lived session from accumulating every page it ever visited. 1876 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 1877 1878 // cap on retained back-history so the chain can't grow memory without bound 1879 const max_history: usize = 16; 1880 1881 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 1882 const arena = try allocator.create(std.heap.ArenaAllocator); 1883 arena.* = std.heap.ArenaAllocator.init(allocator); 1884 errdefer { 1885 arena.deinit(); 1886 allocator.destroy(arena); 1887 } 1888 1889 session.page_arena = arena; 1890 const route = session.data.current_page; 1891 1892 const page = try arena.allocator().create(Page); 1893 page.* = try Page.init(arena, session, route); 1894 return .{ 1895 .root = try initRoot(allocator, page, session), 1896 .route = route, 1897 .arena = arena, 1898 .history = .empty, 1899 }; 1900 } 1901 1902 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 1903 self.root.deinit(allocator); 1904 freeArena(allocator, self.arena); 1905 for (self.history.items) |*entry| { 1906 entry.root.deinit(allocator); 1907 freeArena(allocator, entry.arena); 1908 } 1909 self.history.deinit(allocator); 1910 } 1911 1912 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 1913 arena.deinit(); 1914 allocator.destroy(arena); 1915 } 1916 1917 // reconcile the displayed root with the session's navigation state. a 1918 // forward nav (current_page moved to a different parent page) pushes the 1919 // current root and builds the new page in a fresh arena; a back request 1920 // frees the current page and restores the previous one. when escape is 1921 // pressed with no history left we switch to the quit tab instead of quitting. 1922 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 1923 // refresh: rebuild the current page in place from a current moment 1924 if (session.refresh_requested) { 1925 session.refresh_requested = false; 1926 if (session.haxy_moment != null or session.local != null) { 1927 const arena = try allocator.create(std.heap.ArenaAllocator); 1928 arena.* = std.heap.ArenaAllocator.init(allocator); 1929 errdefer freeArena(allocator, arena); 1930 1931 session.page_arena = arena; 1932 1933 const route = session.data.current_page; 1934 const page = try arena.allocator().create(Page); 1935 page.* = try Page.init(arena, session, route); 1936 const new_root = try initRoot(allocator, page, session); 1937 1938 self.root.deinit(allocator); 1939 freeArena(allocator, self.arena); 1940 self.root = new_root; 1941 self.route = route; 1942 self.arena = arena; 1943 } 1944 return; 1945 } 1946 1947 if (session.nav_back) { 1948 session.nav_back = false; 1949 if (self.history.pop()) |entry| { 1950 self.root.deinit(allocator); 1951 freeArena(allocator, self.arena); 1952 self.root = entry.root; 1953 self.route = entry.route; 1954 self.arena = entry.arena; 1955 session.page_arena = entry.arena; 1956 session.data.current_page = entry.route; 1957 chooseAnsiArtForNavigation(session); 1958 return; 1959 } 1960 // nothing to go back to; switch to the quit confirmation 1961 const root_focus = self.root.getFocus(); 1962 var iter = root_focus.children.iterator(); 1963 while (iter.next()) |entry| { 1964 switch (entry.value_ptr.focus.kind) { 1965 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 1966 root_focus.setFocus(entry.key_ptr.*); 1967 // a Stack only builds its selected child, so the quit 1968 // button isn't in the focus tree until a build runs with 1969 // the quit tab selected. build once, then send a 1970 // synthetic arrow_down to drop focus from the tab onto 1971 // the button so it's ready to confirm. 1972 try self.root.build(allocator, .{ 1973 .min_size = .{ .width = null, .height = 40 }, 1974 .max_size = .{ .width = 80, .height = null }, 1975 }, root_focus); 1976 try self.root.input(allocator, .arrow_down, root_focus); 1977 break; 1978 }, 1979 else => {}, 1980 } 1981 } 1982 return; 1983 } 1984 1985 // forward navigation: navigate() set next_page to the page to move to (a 1986 // cross-page link or tab change crossing pages) 1987 if (session.next_page) |route| { 1988 session.next_page = null; 1989 if (session.haxy_moment == null and session.local == null) return; 1990 // the page we navigated to becomes the current page 1991 session.data.current_page = route; 1992 1993 const arena = try allocator.create(std.heap.ArenaAllocator); 1994 arena.* = std.heap.ArenaAllocator.init(allocator); 1995 errdefer freeArena(allocator, arena); 1996 1997 session.page_arena = arena; 1998 1999 const page = try arena.allocator().create(Page); 2000 page.* = try Page.init(arena, session, route); 2001 const new_root = try initRoot(allocator, page, session); 2002 2003 try self.history.append(allocator, .{ .root = self.root, .route = self.route, .arena = self.arena }); 2004 // drop the oldest entry (freeing its widget tree and arena) once over cap 2005 if (self.history.items.len > max_history) { 2006 var oldest = self.history.orderedRemove(0); 2007 oldest.root.deinit(allocator); 2008 freeArena(allocator, oldest.arena); 2009 } 2010 self.root = new_root; 2011 self.route = route; 2012 self.arena = arena; 2013 } 2014 } 2015 }; 2016 2017 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2018 const page_widget: Widget = switch (page.*) { 2019 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2020 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2021 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2022 }; 2023 2024 chooseAnsiArtForNavigation(session); 2025 2026 // on the TUI/SSH, the page sits above a one-row footer showing the url 2027 var root = if (session.is_terminal) blk: { 2028 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2029 errdefer box.deinit(allocator); 2030 const bg_id = bg_blk: { 2031 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2032 errdefer background.deinit(allocator); 2033 const id = background.getFocus().id; 2034 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2035 break :bg_blk id; 2036 }; 2037 { 2038 var footer = try widget.Footer.init(allocator, session); 2039 errdefer footer.deinit(allocator); 2040 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2041 } 2042 box.getFocus().child_id = bg_id; 2043 break :blk Widget{ .box = box }; 2044 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2045 errdefer root.deinit(allocator); 2046 2047 // input-owning views build their TextInputs in init, so reset their 2048 // registrations here 2049 session.text_inputs.clearRetainingCapacity(); 2050 session.input_values.clearRetainingCapacity(); 2051 2052 try root.build(allocator, .{ 2053 .min_size = .{ .width = null, .height = 40 }, 2054 .max_size = .{ .width = 100, .height = null }, 2055 }, root.getFocus()); 2056 2057 return root; 2058 } 2059 2060 // native and server-side sessions choose from the embedded collection. WASM 2061 // keeps the art serialized by the server instead. 2062 fn chooseAnsiArtForNavigation(session: *Session) void { 2063 if (comptime ansi_arts.len == 0) return; 2064 const io = session.io orelse return; 2065 var random: usize = undefined; 2066 io.random(std.mem.asBytes(&random)); 2067 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2068 } 2069