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