╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
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