╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ ui.zig ───────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
2221 2222 // the quit button (on the quit tab) asks the host to tear down. 2223 if (session.quit_requested) terminal.requestQuit(); 2224 2225 try nav.root.build(allocator, .{ 2226 .min_size = .{ .width = null, .height = null }, 2227 .max_size = .{ .width = terminal.size.width, .height = terminal.size.height }, 2228 }, nav.root.getFocus()); 2229 } 2230 } 2231 2232 fn showCopyableText(io: std.Io, copyable_text: []const u8) !void { 2233 const tty: ?std.Io.File = if (builtin.os.tag == .windows) 2234 null 2235 else 2236 try std.Io.Dir.cwd().openFile(io, "/dev/tty", .{ .mode = .read_write }); 2237 defer if (tty) |file| file.close(io); 2238 2239 const input = if (tty) |file| file else std.Io.File.stdin(); 2240 const output = if (tty) |file| file else std.Io.File.stdout(); 2241 2242 var write_buf: [1024]u8 = undefined; 2243 var writer = output.writer(io, &write_buf); 2244 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}); 2245 try writer.interface.flush(); 2246 2247 var read_buf: [1]u8 = undefined; 2248 var reader = input.reader(io, &read_buf); 2249 while (true) { 2250 const byte = try reader.interface.takeByte(); 2251 if (byte == '\r' or byte == '\n') return; 2252 } 2253 } 2254 2255 pub fn inputKey(allocator: std.mem.Allocator, root: *Widget, key: Key, session: *Session) !void { 2256 const root_focus = root.getFocus(); 2257 const focused_before = root_focus.grandchild_id; 2258 var activate_in_page_link = false; 2259 switch (key) { 2260 // request a navigation pop; the host's Nav.sync goes back a page, or 2261 // quits when there's no history left. 2262 .escape => { 2263 session.back = .requested; 2264 return; 2265 }, 2266 .ctrl => |letter| switch (letter) { 2267 'r' => { 2268 session.refresh_requested = true; 2269 return; 2270 }, 2271 else => try root.input(allocator, key, root_focus), 2272 }, 2273 .enter => { 2274 if (root_focus.grandchild_id) |gid| { 2275 // follow a cross-page link 2276 if (crossPageLink(root_focus, gid, session.data)) |route| { 2277 return session.navigate(route); 2278 } 2279 if (rawLink(root_focus, gid)) |url| return requestRawLink(session, url); 2280 } 2281 activate_in_page_link = true; 2282 try root.input(allocator, key, root_focus); 2283 }, 2284 .mouse => |mouse| { 2285 if (mouse.action == .press and mouse.action.press == .left) { 2286 if (root_focus.hitTest(mouse.x, mouse.y)) |hit| { 2287 const focus_id = hit.id; 2288 if (hit.kind == .background) { 2289 root_focus.setFocus(focus_id); 2290 return; 2291 } 2292 if (widget.isBackButton((root_focus.children.get(focus_id) orelse return).focus)) { 2293 if (session.back == .available) session.back = .requested; 2294 return; 2295 } 2296 // follow a cross-page link 2297 if (crossPageLink(root_focus, focus_id, session.data)) |route| { 2298 return session.navigate(route); 2299 } 2300 if (rawLink(root_focus, focus_id)) |url| { 2301 if (!mouse.ctrl) try requestRawLink(session, url); 2302 return; 2303 } 2304 root_focus.setFocus(focus_id); 2305 activate_in_page_link = true; 2306 } 2307 // forward the press into the widget tree so buttons (and any 2308 // future click-aware widgets) can react. widgets that don't 2309 // care about presses ignore it. 2310 try root.input(allocator, key, root_focus); 2311 } else { 2312 try root.input(allocator, key, root_focus); 2313 } 2314 }, 2315 else => try root.input(allocator, key, root_focus), 2316 } 2317 2318 const focused_after = root_focus.grandchild_id; 2319 if (!activate_in_page_link and focused_before == focused_after) return; 2320 const focus_id = focused_after orelse return; 2321 if (inPageLink(root_focus, focus_id, session.data)) |route| session.data.current_page = route; 2322 } 2323 2324 // a link to bytes the server serves directly rather than to a page route, so 2325 // it never resolves to a RoutablePage 2326 pub const raw_link_prefix = "ax:"; 2327 2328 // a route already loaded into the current page; focusing it changes the url 2329 // without rebuilding the page 2330 pub const in_page_link_prefix = "ai:"; 2331 2332 // an in-page tab's base route, or the exact current route when it is selected 2333 pub fn inPageTabLink(session: *Session, route: RoutablePage, selected: bool) ![]const u8 { 2334 const target = if (selected) session.data.current_page else route; 2335 return std.fmt.allocPrint(session.page_arena.allocator(), "{s}{s}", .{ in_page_link_prefix, try target.toUrl(session.page_arena) }); 2336 } 2337 2338 // a button the web renderer covers with a file picker that posts the chosen 2339 // file to the url after the prefix 2340 pub const file_input_prefix = "file:"; 2341 2342 // a submit button that overrides its enclosing form's action 2343 pub const submit_action_prefix = "submit:"; 2344 2345 // the url an `ax:` link points at, or null for anything else. a host that 2346 // suppresses the anchor's own navigation needs this to follow the link. 2347 pub fn rawLink(root_focus: *Focus, focus_id: usize) ?[]const u8 { 2348 const child = root_focus.children.get(focus_id) orelse return null; 2349 const custom = switch (child.focus.kind) { 2350 .custom => |c| c, 2351 else => return null, 2352 }; 2353 if (!std.mem.startsWith(u8, custom, raw_link_prefix)) return null; 2354 return custom[raw_link_prefix.len..]; 2355 } 2356 2357 fn requestRawLink(session: *Session, url: []const u8) !void { 2358 session.host_request = .{ .show_copyable_text = try terminalWebUrl(session.page_arena.allocator(), session, url) }; 2359 } 2360 2361 pub fn terminalWebUrl(allocator: std.mem.Allocator, session: *const Session, path: []const u8) ![]const u8 { 2362 return if (session.web_port) |port| 2363 try std.fmt.allocPrint(allocator, "http://localhost:{d}{s}", .{ port, path }) 2364 else 2365 path; 2366 } 2367 2368 // if the focus target at focus_id is an `a:` link that should navigate (a 2369 // different parent page, or a different files directory within the repo page), 2370 // return its route; otherwise null. lets a host turn a click / enter on such a 2371 // link into a navigation rather than a focus change. 2372 pub fn crossPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2373 const current = data.current_page; 2374 const route = pageLink(root_focus, focus_id, data, "a:") orelse return null; 2375 // a link to a different parent page always navigates; within a page, a 2376 // files-directory / commits-page / list-window change navigates while tab 2377 // links stay in-page. 2378 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; 2379 return null; 2380 } 2381 2382 // the route carried by an `ai:` focus target, or null for any other target 2383 pub fn inPageLink(root_focus: *Focus, focus_id: usize, data: Session.Data) ?RoutablePage { 2384 return pageLink(root_focus, focus_id, data, in_page_link_prefix); 2385 } 2386 2387 fn pageLink(root_focus: *Focus, focus_id: usize, data: Session.Data, prefix: []const u8) ?RoutablePage { 2388 const child = root_focus.children.get(focus_id) orelse return null; 2389 const custom = switch (child.focus.kind) { 2390 .custom => |c| c, 2391 else => return null, 2392 }; 2393 if (!std.mem.startsWith(u8, custom, prefix)) return null; 2394 const path = custom[prefix.len..]; 2395 // local sessions build (and parse) links with the repo identity elided 2396 return if (data.host_kind == .local) RoutablePage.fromUrlLocal(path) else RoutablePage.fromUrl(path); 2397 } 2398 2399 // a display author, resolved against the admin db at read time 2400 pub const Author = union(enum) { 2401 unknown, 2402 email: []const u8, // an email no user matches 2403 user_name: []const u8, 2404 2405 // the commit author line's email, resolved like initFromEmail 2406 pub fn init( 2407 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2408 arena: *std.heap.ArenaAllocator, 2409 author_line: []const u8, 2410 ) !Author { 2411 return initFromEmail(haxy_moment, arena, evt.authorEmail(author_line)); 2412 } 2413 2414 // from a record's stored author email: it resolves to the user currently 2415 // holding it, else stays the email. the result is allocated in `arena`, 2416 // so the email may borrow transient memory. 2417 pub fn initFromEmail( 2418 haxy_moment: ?evt.AdminDB.HashMap(.read_only), 2419 arena: *std.heap.ArenaAllocator, 2420 author_email: ?[]const u8, 2421 ) !Author { 2422 const email = author_email orelse return .unknown; 2423 if (haxy_moment) |moment| { 2424 if (try evt.User.readByEmail(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, email)) |user| { 2425 return .{ .user_name = user.event.name }; 2426 } 2427 } 2428 return .{ .email = try arena.allocator().dupe(u8, email) }; 2429 } 2430 }; 2431 2432 // the "a:" link to user `name`'s page. 2433 pub fn userLink(page_arena: *std.heap.ArenaAllocator, name: []const u8) ![]const u8 { 2434 const parsed = RoutablePage.Array(evt.User.name_max_len).from(name) orelse return error.RouteTooLong; 2435 const url = try (RoutablePage{ .user_repos = .{ .name = parsed } }).toUrl(page_arena); 2436 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 2437 } 2438 2439 // a focusable " author " box showing `author`, linking to their user page 2440 // when it is a known user 2441 pub fn authorBox(allocator: std.mem.Allocator, page_arena: *std.heap.ArenaAllocator, author: Author) !wgt.TextBox { 2442 const text = switch (author) { 2443 .unknown => "", 2444 .email, .user_name => |t| t, 2445 }; 2446 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " author " }); 2447 errdefer tb.deinit(allocator); 2448 tb.getFocus().mode = .all; 2449 switch (author) { 2450 .user_name => |name| tb.getFocus().kind = .{ .custom = try userLink(page_arena, name) }, 2451 else => {}, 2452 } 2453 return tb; 2454 } 2455 2456 // native-TUI navigation 2457 pub const Nav = struct { 2458 root: Widget, 2459 // backs the current page (its Page data and the page-scoped strings its 2460 // widgets build). owned here, swapped on each navigation. session.page_arena 2461 // tracks whichever of these is current so widgets allocate into it. 2462 arena: *std.heap.ArenaAllocator, 2463 history: std.ArrayList(Entry), 2464 2465 // each retained page keeps its own arena, freed when the page leaves the 2466 // stack (popped on back, evicted at cap, or on deinit). this is what keeps a 2467 // long-lived session from accumulating every page it ever visited. 2468 const Entry = struct { root: Widget, route: RoutablePage, arena: *std.heap.ArenaAllocator }; 2469 2470 // cap on retained back-history so the chain can't grow memory without bound 2471 const max_history: usize = 16; 2472 2473 pub fn init(allocator: std.mem.Allocator, session: *Session) !Nav { 2474 session.back = .unavailable; 2475 const arena = try allocator.create(std.heap.ArenaAllocator); 2476 arena.* = std.heap.ArenaAllocator.init(allocator); 2477 errdefer { 2478 arena.deinit(); 2479 allocator.destroy(arena); 2480 } 2481 2482 session.page_arena = arena; 2483 const route = session.data.current_page; 2484 2485 const page = try arena.allocator().create(Page); 2486 page.* = try Page.init(arena, session, route); 2487 return .{ 2488 .root = try initRoot(allocator, page, session), 2489 .arena = arena, 2490 .history = .empty, 2491 }; 2492 } 2493 2494 pub fn deinit(self: *Nav, allocator: std.mem.Allocator) void { 2495 self.root.deinit(allocator); 2496 freeArena(allocator, self.arena); 2497 for (self.history.items) |*entry| { 2498 entry.root.deinit(allocator); 2499 freeArena(allocator, entry.arena); 2500 } 2501 self.history.deinit(allocator); 2502 } 2503 2504 fn freeArena(allocator: std.mem.Allocator, arena: *std.heap.ArenaAllocator) void { 2505 arena.deinit(); 2506 allocator.destroy(arena); 2507 } 2508 2509 // reconcile the displayed root with the session's navigation state. a 2510 // forward nav (current_page moved to a different parent page) pushes the 2511 // current root and builds the new page in a fresh arena; a back request 2512 // frees the current page and restores the previous one. when escape is 2513 // pressed with no history left we switch to the quit tab instead of quitting. 2514 pub fn sync(self: *Nav, allocator: std.mem.Allocator, session: *Session) !void { 2515 // refresh: rebuild the current page in place from a current moment 2516 if (session.refresh_requested) { 2517 session.refresh_requested = false; 2518 if (session.haxy_moment != null or session.local != null) { 2519 const arena = try allocator.create(std.heap.ArenaAllocator); 2520 arena.* = std.heap.ArenaAllocator.init(allocator); 2521 errdefer freeArena(allocator, arena); 2522 2523 session.page_arena = arena; 2524 2525 const route = session.data.current_page; 2526 const page = try arena.allocator().create(Page); 2527 page.* = try Page.init(arena, session, route); 2528 const new_root = try initRoot(allocator, page, session); 2529 2530 self.root.deinit(allocator); 2531 freeArena(allocator, self.arena); 2532 self.root = new_root; 2533 self.arena = arena; 2534 } 2535 return; 2536 } 2537 2538 if (session.back == .requested) { 2539 if (self.history.pop()) |entry| { 2540 session.back = if (self.history.items.len == 0) .unavailable else .available; 2541 self.root.deinit(allocator); 2542 freeArena(allocator, self.arena); 2543 self.root = entry.root; 2544 self.arena = entry.arena; 2545 session.page_arena = entry.arena; 2546 session.data.current_page = entry.route; 2547 chooseAnsiArtForNavigation(session); 2548 return; 2549 } 2550 session.back = .unavailable; 2551 // nothing to go back to; switch to the quit confirmation 2552 const root_focus = self.root.getFocus(); 2553 var iter = root_focus.children.iterator(); 2554 while (iter.next()) |entry| { 2555 switch (entry.value_ptr.focus.kind) { 2556 .custom => |custom| if (std.mem.eql(u8, custom, Quit.tab_kind)) { 2557 root_focus.setFocus(entry.key_ptr.*); 2558 // a Stack only builds its selected child, so the quit 2559 // button isn't in the focus tree until a build runs with 2560 // the quit tab selected. build once, then send a 2561 // synthetic arrow_down to drop focus from the tab onto 2562 // the button so it's ready to confirm. 2563 try self.root.build(allocator, .{ 2564 .min_size = .{ .width = null, .height = 40 }, 2565 .max_size = .{ .width = 80, .height = null }, 2566 }, root_focus); 2567 try self.root.input(allocator, .arrow_down, root_focus); 2568 break; 2569 }, 2570 else => {}, 2571 } 2572 } 2573 return; 2574 } 2575 2576 // forward navigation: navigate() set next_page to the page to move to (a 2577 // cross-page link or tab change crossing pages) 2578 if (session.next_page) |route| { 2579 session.next_page = null; 2580 if (session.haxy_moment == null and session.local == null) return; 2581 const previous_route = session.data.current_page; 2582 // the page we navigated to becomes the current page 2583 session.data.current_page = route; 2584 session.back = .available; 2585 2586 const arena = try allocator.create(std.heap.ArenaAllocator); 2587 arena.* = std.heap.ArenaAllocator.init(allocator); 2588 errdefer freeArena(allocator, arena); 2589 2590 session.page_arena = arena; 2591 2592 const page = try arena.allocator().create(Page); 2593 page.* = try Page.init(arena, session, route); 2594 const new_root = try initRoot(allocator, page, session); 2595 2596 try self.history.append(allocator, .{ .root = self.root, .route = previous_route, .arena = self.arena }); 2597 // drop the oldest entry (freeing its widget tree and arena) once over cap 2598 if (self.history.items.len > max_history) { 2599 var oldest = self.history.orderedRemove(0); 2600 oldest.root.deinit(allocator); 2601 freeArena(allocator, oldest.arena); 2602 } 2603 self.root = new_root; 2604 self.arena = arena; 2605 } 2606 } 2607 }; 2608 2609 pub fn initRoot(allocator: std.mem.Allocator, page: *const Page, session: *Session) !Widget { 2610 const page_widget: Widget = switch (page.*) { 2611 .home => |*p| .{ .home = try .init(allocator, p, session) }, 2612 .user => |*p| .{ .user = try .init(allocator, p, session) }, 2613 .repo => |*p| .{ .repo = try .init(allocator, p, session) }, 2614 .fork => |*p| .{ .fork = try .init(allocator, p, session) }, 2615 }; 2616 2617 chooseAnsiArtForNavigation(session); 2618 2619 // on the TUI/SSH, the page sits above a one-row footer showing the url 2620 var root = if (session.is_terminal) blk: { 2621 var box = try wgt.Box(Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 2622 errdefer box.deinit(allocator); 2623 const bg_id = bg_blk: { 2624 var background = try widget.AnsiBackground.init(allocator, page_widget, session); 2625 errdefer background.deinit(allocator); 2626 const id = background.getFocus().id; 2627 try box.children.put(allocator, id, .{ .widget = .{ .background = background }, .rect = null, .min_size = null }); 2628 break :bg_blk id; 2629 }; 2630 { 2631 var footer = try widget.Footer.init(allocator, session); 2632 errdefer footer.deinit(allocator); 2633 try box.children.put(allocator, footer.getFocus().id, .{ .widget = .{ .footer = footer }, .rect = null, .min_size = .{ .width = null, .height = 1 } }); 2634 } 2635 box.getFocus().child_id = bg_id; 2636 break :blk Widget{ .box = box }; 2637 } else Widget{ .background = try widget.AnsiBackground.init(allocator, page_widget, session) }; 2638 errdefer root.deinit(allocator); 2639 2640 // input-owning views build their TextInputs in init, so reset their 2641 // registrations here 2642 session.text_inputs.clearRetainingCapacity(); 2643 2644 try root.build(allocator, .{ 2645 .min_size = .{ .width = null, .height = 40 }, 2646 .max_size = .{ .width = 100, .height = null }, 2647 }, root.getFocus()); 2648 2649 return root; 2650 } 2651 2652 // native and server-side sessions choose from the embedded collection. WASM 2653 // keeps the art serialized by the server instead. 2654 fn chooseAnsiArtForNavigation(session: *Session) void { 2655 if (comptime ansi_arts.len == 0) return; 2656 const io = session.io orelse return; 2657 var random: usize = undefined; 2658 io.random(std.mem.asBytes(&random)); 2659 session.data.ansi_art = ansi_arts[random % ansi_arts.len]; 2660 } 2661