╭ Repos.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const hash = xit.hash; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const inp = xitui.input; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 13 pub const page_size = 20; // how many repos one window shows 14 15 repos: []const evt.Repo, 16 owner_names: []const []const u8, 17 after: usize, // the window start this page was built with, mirrored into the url 18 next_after: ?usize, // the `after` for the "next" row, or null when this is the last window 19 20 const Self = @This(); 21 22 pub fn init( 23 arena: *std.heap.ArenaAllocator, 24 haxy_moment: evt.AdminDB.HashMap(.read_only), 25 after: usize, 26 ) !Self { 27 const DB = evt.AdminDB; 28 const hash_kind = evt.admin_repo_opts.hash; 29 30 var repos: std.ArrayList(evt.Repo) = .empty; 31 var owner_names: std.ArrayList([]const u8) = .empty; 32 33 const empty: Self = .{ .repos = &.{}, .owner_names = &.{}, .after = after, .next_after = null }; 34 35 // the ordered repo-list (oldest first); absent until the first repo exists. 36 const repo_list_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "repo-list")) orelse return empty; 37 const repo_list = try DB.ArrayList(.read_only).init(repo_list_cursor); 38 const count = try repo_list.count(); 39 40 const event_id_to_repo_cursor = try haxy_moment.getCursor(hash.hashInt(hash_kind, "event-id->repo")) orelse return empty; 41 const event_id_to_repo = try DB.HashMap(.read_only).init(event_id_to_repo_cursor); 42 43 // read the window [after, after+page_size) by index — a direct O(log n) 44 // seek per entry, never scanning the whole table. ids whose repo has been 45 // deleted (tombstones) are skipped. 46 const end = @min(after + page_size, count); 47 var i = after; 48 while (i < end) : (i += 1) { 49 const id_cursor = try repo_list.getCursor(@intCast(i)) orelse continue; 50 var event_id: [evt.event_id_size]u8 = undefined; 51 _ = try id_cursor.readBytes(&event_id); 52 const repo_cursor = try event_id_to_repo.getCursor(hash.hashInt(hash_kind, &event_id)) orelse continue; 53 const repo_map = try DB.HashMap(.read_only).init(repo_cursor); 54 const repo_event = try evt.read(evt.Repo, DB, hash_kind, arena, repo_map); 55 try repos.append(arena.allocator(), repo_event); 56 57 const owner = try evt.User.readById(DB, hash_kind, haxy_moment, arena, repo_event.user_id); 58 try owner_names.append(arena.allocator(), if (owner) |o| o.name else ""); 59 } 60 61 return .{ 62 .repos = repos.items, 63 .owner_names = owner_names.items, 64 .after = after, 65 .next_after = if (end < count) end else null, 66 }; 67 } 68 69 pub const View = struct { 70 list: ui.FlowBox.Scroll, 71 data: *const Self, 72 73 pub fn init(allocator: std.mem.Allocator, data: *const Self) !View { 74 var self = blk: { 75 var list = try ui.FlowBox.Scroll.init(allocator, .{}); 76 errdefer list.deinit(allocator); 77 78 break :blk View{ 79 .list = list, 80 .data = data, 81 }; 82 }; 83 errdefer self.deinit(allocator); 84 85 var arena = std.heap.ArenaAllocator.init(allocator); 86 defer arena.deinit(); 87 const aa = arena.allocator(); 88 89 // a leading "previous" row off the first window, one row per repo, then 90 // a trailing "next" row when more remain. each window row navigates to the 91 // adjacent window (full reload on web, Nav rebuild on the TUI). 92 var items: std.ArrayList(ui.FlowBox.Item) = .empty; 93 if (data.after > 0) 94 try items.append(aa, .{ .text = "← previous", .link = try std.fmt.allocPrint(aa, "a:/repos?after={d}", .{data.after -| page_size}) }); 95 for (data.repos, 0..) |repo, i| { 96 // clicking a repo opens its page; the "a:" prefix makes the web 97 // renderer emit an <a href="/repo/alice/foo"> anchor. skip the link 98 // when the owner is unknown so it isn't a dead route. 99 const owner = data.owner_names[i]; 100 try items.append(aa, .{ 101 .text = try std.fmt.allocPrint(aa, "{s} - {s}", .{ repo.name, repo.description }), 102 .link = if (owner.len > 0) try std.fmt.allocPrint(aa, "a:/repo/{s}/{s}", .{ owner, repo.name }) else "", 103 }); 104 } 105 if (data.next_after) |next_after| 106 try items.append(aa, .{ .text = "next →", .link = try std.fmt.allocPrint(aa, "a:/repos?after={d}", .{next_after}) }); 107 try self.list.setItems(allocator, items.items); 108 109 return self; 110 } 111 112 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 113 self.list.deinit(allocator); 114 } 115 116 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 117 try self.list.build(allocator, constraint, root_focus); 118 } 119 120 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 121 try self.list.input(allocator, key, root_focus); 122 } 123 124 pub fn clearGrid(self: *View) void { 125 self.list.clearGrid(); 126 } 127 128 pub fn getGrid(self: View) ?Grid { 129 return self.list.getGrid(); 130 } 131 132 pub fn getFocus(self: *View) *Focus { 133 return self.list.getFocus(); 134 } 135 136 pub fn getSelectedIndex(self: View) ?usize { 137 return self.list.getSelectedIndex(); 138 } 139 }; 140