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