╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1793 // input instead of mutating state or touching the DB themselves; the host 1794 // drains them each frame (see applyAndWritePending / applyPending). this keeps DB 1795 // side effects out of the widget tree and gives every render path one place to 1796 // turn a UI action into a state change + event. 1797 pub const Action = union(enum) { 1798 toggle_ansi, 1799 }; 1800 1801 pub fn init( 1802 arena: *std.heap.ArenaAllocator, 1803 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1804 data: Data, 1805 ) !Self { 1806 var session = Self{ 1807 .data = data, 1808 .arena = arena, 1809 // until a host swaps in a page-scoped arena (Nav does this), page 1810 // allocations land in the session arena. on the web path that's the 1811 // intended behavior, since the whole arena is per-request. 1812 .page_arena = arena, 1813 .admin_repo = repo, 1814 .haxy_moment = try evt.currentMoment(evt.admin_repo_opts, repo), 1815 }; 1816 try session.loadUser(); 1817 return session; 1818 } 1819 1820 // load the logged-in user's name and persisted preferences from the db 1821 pub fn loadUser(self: *Self) !void { 1822 const user_id = self.data.user_id orelse return; 1823 const moment = self.haxy_moment orelse return; 1824 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.arena, user_id)) |user| { 1825 self.data.user_name = user.event.name; 1826 self.data.enable_ansi = user.event.enable_ansi; 1827 } 1828 } 1829 1830 pub fn userId(self: *const Self) ?[evt.event_id_size]u8 { 1831 const bytes = self.data.user_id orelse return null; 1832 if (bytes.len != evt.event_id_size) return null; 1833 var id: [evt.event_id_size]u8 = undefined; 1834 @memcpy(&id, bytes); 1835 return id; 1836 } 1837 1838 pub fn formFeedback(self: *const Self, comptime tag: std.meta.Tag(FormFeedback)) ?@FieldType(FormFeedback, @tagName(tag)) { 1839 const feedback = self.data.form_feedback orelse return null; 1840 if (std.meta.activeTag(feedback) != tag) return null; 1841 return @field(feedback, @tagName(tag)); 1842 } 1843 1844 pub fn clearFormFeedback(self: *Self) void { 1845 self.data.form_feedback = null; 1846 } 1847 1848 // the commit author for an event this session creates, or null when it may 1849 // not create one 1850 pub fn eventAuthor(self: *Self) !?evt.CommitAuthor { 1851 if (self.data.host_kind == .local) { 1852 const src = self.local orelse return local_author_fallback; 1853 const io = self.io orelse return local_author_fallback; 1854 return try localAuthor(src, io, self.page_arena.child_allocator, self.page_arena); 1855 } 1856 const user_id = self.data.user_id orelse return null; 1857 const moment = self.haxy_moment orelse return null; 1858 const user = (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, self.page_arena, user_id)) orelse return null; 1859 return .{ .name = user.event.name, .email = user.event.email }; 1860 } 1861 1862 // queue an action for the host to drain this frame. 1863 pub fn push(self: *Self, action: Action) !void { 1864 try self.pending.append(self.arena.allocator(), action); 1865 } 1866 1867 // apply a single action's in-memory effect only (no persistence). 1868 fn apply(self: *Self, action: Action) void { 1869 switch (action) { 1870 .toggle_ansi => self.data.enable_ansi = !self.data.enable_ansi, 1871 } 1872 } 1873 1874 // drain the session's queued actions by applying them to the session. 1875 // used on the wasm path, which has no repo to persist to. 1876 pub fn applyPending(self: *Self) void { 1877 for (self.pending.items) |action| self.apply(action); 1878 self.pending.clearRetainingCapacity(); 1879 } 1880 1881 // drain the session's queued actions by applying them to the session and 1882 // writing them to the db 1883 pub fn applyAndWritePending( 1884 self: *Self, 1885 io: std.Io, 1886 allocator: std.mem.Allocator, 1887 repo: *rp.Repo(.xit, evt.admin_repo_opts), 1888 ) !void { 1889 defer self.pending.clearRetainingCapacity(); 1890 for (self.pending.items) |action| { 1891 self.apply(action); 1892 switch (action) { 1893 .toggle_ansi => if (self.data.user_id) |user_id| { 1894 try evt.User.toggleAnsi(evt.admin_repo_opts, io, allocator, repo, user_id); 1895 }, 1896 } 1897 } 1898 } 1899 1900 // reload the moment from the admin repo and re-read user preferences 1901 pub fn reloadMoment(self: *Self, allocator: std.mem.Allocator, repo: *rp.Repo(.xit, evt.admin_repo_opts)) !void { 1902 const moment = try evt.currentMoment(evt.admin_repo_opts, repo); 1903 self.haxy_moment = moment; 1904 1905 const user_id = self.data.user_id orelse return; 1906 var arena = std.heap.ArenaAllocator.init(allocator); 1907 defer arena.deinit(); 1908 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, &arena, user_id)) |user| { 1909 self.data.enable_ansi = user.event.enable_ansi; 1910 } 1911 } 1912 1913 // request a forward navigation to `route`; the host consumes next_page 1914 // (Nav.sync on the terminal, the wasm tick on the web). 1915 pub fn navigate(self: *Session, route: RoutablePage) !void { 1916 self.clearFormFeedback(); 1917 self.next_page = route; 1918 } 1919 }; 1920 1921 pub fn run(io: std.Io, allocator: std.mem.Allocator, session: *Session, repo_maybe: ?*rp.Repo(.xit, evt.admin_repo_opts)) !void { 1922 var nav = try Nav.init(allocator, session); 1923 defer nav.deinit(allocator); 1924 1925 var terminal = try term.Terminal.init(io, allocator); 1926 var terminal_live = true; 1927 defer if (terminal_live) terminal.deinit(io); 1928 1929 // set term as active so it will be properly cooked 1930 // when a panic/segfault happens 1931 term.setActive(&terminal); 1932 defer term.setActive(null); 1933 1934 while (!terminal.shouldQuit()) { 1935 const grid_changed = try terminal.render(&nav.root); 1936 1937 // process any inputs. 1938 // 1939 // if the grid didn't change, then first do a blocking 1940 // read, so the thread will sleep until further input. 1941 // after that, all remaining reads are non-blocking so 1942 // we can process the rest of the queued inputs. 1943 // 1944 // if the grid *did* change, then only do non-blocking 1945 // reads. we do not want to sleep the thread because 1946 // there may be an animation that requires more looping. 1947 var blocking = !grid_changed; 1948 while (try terminal.readKey(io, blocking)) |key| { 1949 blocking = false; 1950 try inputKey(allocator, &nav.root, key, session); 1951 } 1952 1953 if (session.host_request) |request| { 1954 session.host_request = null; 1955 switch (request) { 1956 .show_copyable_text => |copyable_text| { 1957 term.setActive(null); 1958 terminal.deinit(io); 1959 terminal_live = false; 1960 showCopyableText(io, copyable_text) catch |show_err| { 1961 terminal = try term.Terminal.init(io, allocator); 1962 terminal_live = true; 1963 term.setActive(&terminal); 1964 return show_err; 1965 }; 1966 terminal = try term.Terminal.init(io, allocator); 1967 terminal_live = true; 1968 term.setActive(&terminal); 1969 }, 1970 .sync_events => { 1971 try nav.root.build(allocator, .{ 1972 .min_size = .{ .width = null, .height = null }, 1973 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 1974 }, nav.root.getFocus()); 1975 _ = try terminal.render(&nav.root); 1976 try Repo.Events.performSync(allocator, session); 1977 }, 1978 } 1979 } 1980 1981 // persist queued actions when there's a repo; local mode has none, 1982 // so just apply in-memory 1983 if (repo_maybe) |repo| { 1984 try session.applyAndWritePending(io, allocator, repo); 1985 // reload so the next navigation builds its page from the current moment 1986 try session.reloadMoment(allocator, repo); 1987 } else { 1988 session.applyPending(); 1989 } 1990 1991 // reconcile navigation: forward to a new page, or back on escape. 1992 try nav.sync(allocator, session); 1993 1994 // the quit button (on the quit tab) asks the host to tear down. 1995 if (session.quit_requested) terminal.requestQuit(); 1996 1997 try nav.root.build(allocator, .{ 1998 .min_size = .{ .width = null, .height = null }, 1999 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2000 }, nav.root.getFocus()); 2001 } 2002 } 2003 2004 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2005 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2006 null 2007 else 2008 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2009 defer if (tty) |file| file.close(io); 2010 2011 const input = if (tty) |file| file else std.Io.File.stdin(); 2012 const output = if (tty) |file| file else std.Io.File.stdout(); 2013 2014 var write_buf: [1024]u8 = undefined; 2015 var writer = output.writer(io, &write_buf); 2016 try writer.interface.print("\x1b[2J\x1b[Hcopy the following text and then press enter to go back:\r\n\r\n{s}\r\n", .{copyable_text}); 2017 try writer.interface.flush(); 2018 2019 var read_buf: [1]u8 = undefined; 2020 var reader = input.reader(io, &read_buf); 2021 while (true) { 2022 const byte = try reader.interface.takeByte(); 2023 if (byte == '\r' or byte == '\n') return; 2024 } 2025 } 2026 2027 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2028 const root_focus = root.getFocus(); 2029 const focused_before = root_focus.grandchild_id; 2030 var activate_in_page_link = false; 2031 switch (key) { 2032 // request a navigation pop; the host's Nav.sync goes back a page, or 2033 // quits when there's no history left. 2034 .escape => { 2035 session.back = .requested; 2036 return; 2037 }, 2038 .ctrl => |letter| switch (letter) { 2039 'r' => { 2040 session.refresh_requested = true; 2041 return; 2042 }, 2043 else => try root.input(allocator, key, root_focus), 2044 }, 2045 .enter => { 2046 if (root_focus.grandchild_id) |gid| { 2047 // follow a cross-page link 2048 if (crossPageLink(root_focus, gid, session.data)) |route| { 2049 return session.navigate(route); 2050 } 2051 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2052 } 2053 activate_in_page_link = true; 2054 try root.input(allocator, key, root_focus); 2055 }, 2056 .mouse => |mouse| { 2057 if (mouse.action == .press and mouse.action.press == .left) { 2058 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2059 const focus_id = hit.id; 2060 if (hit.kind == .background) { 2061 root_focus.setFocus(focus_id); 2062 return; 2063 } 2064 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2065 if (session.back == .available) session.back = .requested; 2066 return; 2067 } 2068 // follow a cross-page link 2069 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2070 return session.navigate(route); 2071 } 2072 if (rawLink(root_focus, focus_id)) |url| { 2073 if (!mouse.ctrl) try requestRawLink(session, url); 2074 return; 2075 } 2076 root_focus.setFocus(focus_id); 2077 activate_in_page_link = true; 2078 } 2079 // forward the press into the widget tree so buttons (and any 2080 // future click-aware widgets) can react. widgets that don't 2081 // care about presses ignore it. 2082 try root.input(allocator, key, root_focus); 2083 } else { 2084 try root.input(allocator, key, root_focus); 2085 } 2086 }, 2087 else => try root.input(allocator, key, root_focus), 2088 } 2089 2090 const focused_after = root_focus.grandchild_id; 2091 if (!activate_in_page_link and focused_before == focused_after) return; 2092 const focus_id = focused_after orelse return; 2093 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2094 } 2095 2096 // a link to bytes the server serves directly rather than to a page route, so 2097 // it never resolves to a RoutablePage 2098 pub const raw_link_prefix = "ax:"; 2099 2100 // a route already loaded into the current page; focusing it changes the url 2101 // without rebuilding the page 2102 pub const in_page_link_prefix = "ai:"; 2103 2104 // an in-page tab's base route, or the exact current route when it is selected 2105 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2106 const target = if (selected) session.data.current_page else route; 2107 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2108 } 2109 2110 // a button the web renderer covers with a file picker that posts the chosen 2111 // file to the url after the prefix 2112 pub const file_input_prefix = "file:"; 2113 2114 // a submit button that overrides its enclosing form's action 2115 pub const submit_action_prefix = "submit:"; 2116 2117 // the url an `ax:` link points at, or null for anything else. a host that 2118 // suppresses the anchor's own navigation needs this to follow the link. 2119 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2120 const child = root_focus.children.get(focus_id) orelse return null; 2121 const custom = switch (child.focus.kind) { 2122 .custom => |c| c, 2123 else => return null, 2124 }; 2125 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2126 return custom[raw_link_prefix.len..]; 2127 } 2128 2129 fn requestRawLink(session: *Session, url: []const u8) !void { 2130 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2131 } 2132 2133 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2134 return if (session.web_port) |port| 2135 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2136 else 2137 path; 2138 } 2139 2140 // if the focus target at focus_id is an `a:` link that should navigate (a 2141 // different parent page, or a different files directory within the repo page), 2142 // return its route; otherwise null. lets a host turn a click / enter on such a 2143 // link into a navigation rather than a focus change. 2144 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2145 const current = data.current_page; 2146 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2147 // a link to a different parent page always navigates; within a page, a 2148 // files-directory / commits-page / list-window change navigates while tab 2149 // links stay in-page. 2150 if (route.parent() != current.parent() or RoutablePage.repoPageChanged(route, current) or RoutablePage.forkPageChanged(route, current) or RoutablePage.homePageChanged(route, current) or RoutablePage.userPageChanged(route, current)) return route; 2151 return null; 2152 } 2153 2154 // the route carried by an `ai:` focus target, or null for any other target 2155 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2156 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2157 } 2158 2159 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2160 const child = root_focus.children.get(focus_id) orelse return null; 2161 const custom = switch (child.focus.kind) { 2162 .custom => |c| c, 2163 else => return null, 2164 }; 2165 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2166 const path = custom[prefix.len..]; 2167 // local sessions build (and parse) links with the repo identity elided 2168 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2169 } 2170 2171 // a display author, resolved against the admin db at read time 2172 pub const Author = union(enum) { 2173 unknown, 2174 email: []const u8, // an email no user matches 2175 user_name: []const u8, 2176 2177 // the commit author line's email, resolved like initFromEmail 2178 pub fn init( 2179 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2180 arena: *std.heap.ArenaAllocator, 2181 author_line: []const u8, 2182 ) !Author { 2183 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2184 } 2185 2186 // from a record's stored author email: it resolves to the user currently 2187 // holding it, else stays the email. the result is allocated in `arena`, 2188 // so the email may borrow transient memory. 2189 pub fn initFromEmail( 2190 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2191 arena: *std.heap.ArenaAllocator, 2192 author_email: ?[]const u8, 2193 ) !Author { 2194 const email = author_email orelse return .unknown; 2195 if (haxy_moment) |moment| { 2196 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2197 return .{ .user_name = user.event.name }; 2198 } 2199 } 2200 return .{ .email = try arena.allocator().dupe(u8, email) }; 2201 } 2202 }; 2203 2204 // the "a:" link to user `name`'s page. 2205 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2206 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2207 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2208 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2209 } 2210 2211 // a focusable " author " box showing `author`, linking to their user page 2212 // when it is a known user 2213 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2214 const text = switch (author) { 2215 .unknown => "", 2216 .email, .user_name => |t| t, 2217 }; 2218 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2219 errdefer tb.deinit(allocator); 2220 tb.getFocus().mode = .all; 2221 switch (author) { 2222 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2223 else => {}, 2224 } 2225 return tb; 2226 } 2227 2228 // native-TUI navigation 2229 pub const Nav = struct { 2230 root: Widget, 2231 // backs the current page (its Page data and the page-scoped strings its 2232 // widgets build). owned here, swapped on each navigation. session.page_arena 2233 // tracks whichever of these is current so widgets allocate into it. 2234 arena: *std.heap.ArenaAllocator, 2235 history: std.ArrayList(Entry), 2236 2237 // each retained page keeps its own arena, freed when the page leaves the 2238 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2239 // long-lived session from accumulating every page it ever visited. 2240 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2241 2242 // cap on retained back-history so the chain can't grow memory without bound 2243 const max_history: usize = 16; 2244 2245 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2246 session.back = .unavailable; 2247 const arena = try allocator.create(std.heap.ArenaAllocator); 2248 arena.* = std.heap.ArenaAllocator.init(allocator); 2249 errdefer { 2250 arena.deinit(); 2251 allocator.destroy(arena); 2252 } 2253 2254 session.page_arena = arena; 2255 const route = session.data.current_page; 2256 2257 const page = try arena.allocator().create(Page); 2258 page.* = try Page.init(arena, session, route); 2259 return .{ 2260 .root = try initRoot(allocator, page, session), 2261 .arena = arena, 2262 .history = .empty, 2263 }; 2264 } 2265 2266 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2267 self.root.deinit(allocator); 2268 freeArena(allocator, self.arena); 2269 for (self.history.items) |*entry| { 2270 entry.root.deinit(allocator); 2271 freeArena(allocator, entry.arena); 2272 } 2273 self.history.deinit(allocator); 2274 } 2275 2276 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2277 arena.deinit(); 2278 allocator.destroy(arena); 2279 } 2280 2281 // reconcile the displayed root with the session's navigation state. a 2282 // forward nav (current_page moved to a different parent page) pushes the 2283 // current root and builds the new page in a fresh arena; a back request 2284 // frees the current page and restores the previous one. when escape is 2285 // pressed with no history left we switch to the quit tab instead of quitting. 2286 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2287 // refresh: rebuild the current page in place from a current moment 2288 if (session.refresh_requested) { 2289 session.refresh_requested = false; 2290 if (session.haxy_moment != null or session.local != null) { 2291 const arena = try allocator.create(std.heap.ArenaAllocator); 2292 arena.* = std.heap.ArenaAllocator.init(allocator); 2293 errdefer freeArena(allocator, arena); 2294 2295 session.page_arena = arena; 2296 2297 const route = session.data.current_page; 2298 const page = try arena.allocator().create(Page); 2299 page.* = try Page.init(arena, session, route); 2300 const new_root = try initRoot(allocator, page, session); 2301 2302 self.root.deinit(allocator); 2303 freeArena(allocator, self.arena); 2304 self.root = new_root; 2305 self.arena = arena; 2306 } 2307 return; 2308 } 2309 2310 if (session.back == .requested) { 2311 if (self.history.pop()) |entry| { 2312 session.back = if (self.history.items.len == 0) .unavailable else .available; 2313 self.root.deinit(allocator); 2314 freeArena(allocator, self.arena); 2315 self.root = entry.root; 2316 self.arena = entry.arena; 2317 session.page_arena = entry.arena; 2318 session.data.current_page = entry.route; 2319 chooseAnsiArtForNavigation(session); 2320 return; 2321 } 2322 session.back = .unavailable; 2323 // nothing to go back to; switch to the quit confirmation 2324 const root_focus = self.root.getFocus(); 2325 var iter = root_focus.children.iterator(); 2326 while (iter.next()) |entry| { 2327 switch (entry.value_ptr.focus.kind) { 2328 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2329 root_focus.setFocus(entry.key_ptr.*); 2330 // a Stack only builds its selected child, so the quit 2331 // button isn't in the focus tree until a build runs with 2332 // the quit tab selected. build once, then send a 2333 // synthetic arrow_down to drop focus from the tab onto 2334 // the button so it's ready to confirm. 2335 try self.root.build(allocator, .{ 2336 .min_size = .{ .width = null, .height = 40 }, 2337 .max_size = .{ .width = 80, .height = null }, 2338 }, root_focus); 2339 try self.root.input(allocator, .arrow_down, root_focus); 2340 break; 2341 }, 2342 else => {}, 2343 } 2344 } 2345 return; 2346 } 2347 2348 // forward navigation: navigate() set next_page to the page to move to (a 2349 // cross-page link or tab change crossing pages) 2350 if (session.next_page) |route| { 2351 session.next_page = null; 2352 if (session.haxy_moment == null and session.local == null) return; 2353 const previous_route = session.data.current_page; 2354 // the page we navigated to becomes the current page 2355 session.data.current_page = route; 2356 session.back = .available; 2357 2358 const arena = try allocator.create(std.heap.ArenaAllocator); 2359 arena.* = std.heap.ArenaAllocator.init(allocator); 2360 errdefer freeArena(allocator, arena); 2361 2362 session.page_arena = arena; 2363 2364 const page = try arena.allocator().create(Page); 2365 page.* = try Page.init(arena, session, route); 2366 const new_root = try initRoot(allocator, page, session); 2367 2368 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2369 // drop the oldest entry (freeing its widget tree and arena) once over cap 2370 if (self.history.items.len > max_history) { 2371 var oldest = self.history.orderedRemove(0); 2372 oldest.root.deinit(allocator); 2373 freeArena(allocator, oldest.arena); 2374 } 2375 self.root = new_root; 2376 self.arena = arena; 2377 } 2378 } 2379 }; 2380 2381 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2382 const page_widget: Widget = switch (page.*) { 2383 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2384 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2385 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2386 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2387 }; 2388 2389 chooseAnsiArtForNavigation(session); 2390 2391 // on the TUI/SSH, the page sits above a one-row footer showing the url 2392 var root = if (session.is_terminal) blk: { 2393 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2394 errdefer box.deinit(allocator); 2395 const bg_id = bg_blk: { 2396 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2397 errdefer background.deinit(allocator); 2398 const id = background.getFocus().id; 2399 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2400 break :bg_blk id; 2401 }; 2402 { 2403 var footer = try widget.Footer.init(allocator, session); 2404 errdefer footer.deinit(allocator); 2405 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2406 } 2407 box.getFocus().child_id = bg_id; 2408 break :blk Widget{ .box = box }; 2409 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2410 errdefer root.deinit(allocator); 2411 2412 // input-owning views build their TextInputs in init, so reset their 2413 // registrations here 2414 session.text_inputs.clearRetainingCapacity(); 2415 2416 try root.build(allocator, .{ 2417 .min_size = .{ .width = null, .height = 40 }, 2418 .max_size = .{ .width = 100, .height = null }, 2419 }, root.getFocus()); 2420 2421 return root; 2422 } 2423 2424 // native and server-side sessions choose from the embedded collection. WASM 2425 // keeps the art serialized by the server instead. 2426 fn chooseAnsiArtForNavigation(session: *Session) void { 2427 if (comptime ansi_arts.len == 0) return; 2428 const io = session.io orelse return; 2429 var random: usize = undefined; 2430 io.random(std.mem.asBytes(&random)); 2431 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2432 } 2433