╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const inp = @import("../input.zig"); 6 const xit = @import("xit"); 7 const rp = xit.repo; 8 const rf = xit.ref; 9 const hash = xit.hash; 10 const xitui = xit.xitui; 11 const wgt = xitui.widget; 12 const layout = xitui.layout; 13 const Key = xitui.input.Key; 14 const Grid = xitui.grid.Grid; 15 const Focus = xitui.focus.Focus; 16 17 const active_tab_label = "active"; 18 const removed_tab_label = "removed"; 19 20 const page_size = 20; 21 22 pub const Item = struct { 23 id: []const u8, 24 kind: evt.EventKind, 25 author: ui.Author, 26 view_url: ?[]const u8 = null, 27 }; 28 29 pub const EventRef = struct { 30 id: []const u8, 31 kind: evt.EventKind, 32 }; 33 34 pub const Window = struct { 35 count: usize = 0, 36 events: []const Item = &.{}, 37 next: ?EventRef = null, 38 39 // a window without events, which shows the empty message 40 fn isEmpty(self: *const Window) bool { 41 return self.events.len == 0; 42 } 43 }; 44 45 identity: []const u8, 46 view: ui.RoutablePage.EventsView, 47 selected: ?EventRef, 48 active: Window, 49 removed: Window, 50 header: Header, 51 // set when the list is limited to the events one transaction wrote 52 moment: ?u64 = null, 53 54 const Self = @This(); 55 56 pub fn init( 57 comptime repo_kind: rp.RepoKind, 58 comptime repo_opts: rp.RepoOpts(repo_kind), 59 arena: *std.heap.ArenaAllocator, 60 repo: *rp.Repo(repo_kind, repo_opts), 61 io: std.Io, 62 admin_moment: ?evt.AdminDB.HashMap(.read_only), 63 identity: []const u8, 64 repo_event: evt.Repo, 65 requested_view: ui.RoutablePage.EventsView, 66 selected_kind: ?evt.EventKind, 67 selected: []const u8, 68 moment_index: ?u64, 69 local: bool, 70 failure: ?[]const u8, 71 ) !Self { 72 const aa = arena.allocator(); 73 var result = try empty(aa, identity, requested_view, local, failure); 74 result.moment = moment_index; 75 if (local) result.header = try Header.local(repo_kind, repo_opts, aa, repo, io, failure); 76 const require_selected_event = selected_kind != null or selected.len != 0; 77 if ((selected_kind == null) != (selected.len == 0)) return error.NotFound; 78 79 const DB = evt.EventDB(repo_opts.hash); 80 const gpa = arena.child_allocator; 81 var event_db_maybe: ?evt.LocalEventDB(repo_opts.hash) = if (repo_kind == .git) try evt.LocalEventDB(repo_opts.hash).openReadOnly(io, gpa, repo.core.repo_dir) else null; 82 defer if (event_db_maybe) |*event_db| event_db.deinit(io, gpa); 83 const haxy_moment = (if (moment_index) |index| 84 transactionMoment(repo_kind, repo_opts, repo, index) 85 else if (event_db_maybe) |*event_db| 86 evt.currentMomentFromDb(repo_opts.hash, event_db.db) 87 else if (repo_kind == .git) 88 return result 89 else 90 evt.currentMoment(repo_opts, repo)) catch { 91 if (require_selected_event) return error.NotFound; 92 return result; 93 }; 94 95 const kind_map_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.event_index_key)) orelse { 96 if (require_selected_event) return error.NotFound; 97 return result; 98 }; 99 const kind_map = try DB.HashMap(.read_only).init(kind_map_cursor); 100 101 const active_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.active_event_id_set_key)); 102 const removed_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.removed_event_id_set_key)); 103 104 var root_key: ?[@sizeOf(u64) + evt.event_id_size]u8 = null; 105 if (selected.len != 0) { 106 var id: [evt.event_id_size]u8 = undefined; 107 _ = std.fmt.hexToBytes(&id, selected) catch return error.NotFound; 108 if (!std.mem.eql(u8, selected, &std.fmt.bytesToHex(id, .lower))) return error.NotFound; 109 const kind = selected_kind orelse return error.NotFound; 110 const stored_kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse return error.NotFound; 111 if (stored_kind != kind) return error.NotFound; 112 result.selected = .{ .id = try aa.dupe(u8, selected), .kind = kind }; 113 const meta = try readMeta(repo_opts.hash, arena, haxy_moment, kind, &id); 114 result.view = if (meta.removed) .removed else .active; 115 root_key = evt.orderKeyDesc(meta.updated_order, &id); 116 } 117 118 if (moment_index != null) { 119 try readMomentEvents(repo_kind, repo_opts, arena, &result, haxy_moment, kind_map, admin_moment, identity, repo_event, selected); 120 return result; 121 } 122 123 for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 124 const id_set_cursor = switch (view) { 125 .active => active_cursor, 126 .removed => removed_cursor, 127 }; 128 const event_window = switch (view) { 129 .active => &result.active, 130 .removed => &result.removed, 131 }; 132 const id_set = try DB.SortedSet(.read_only).init(id_set_cursor orelse { 133 if (require_selected_event and result.view == view) return error.NotFound; 134 continue; 135 }); 136 event_window.count = @intCast(try id_set.count()); 137 const selected_key = if (result.view == view) root_key else null; 138 if (selected_key) |*key| if (!try id_set.contains(key)) return error.NotFound; 139 140 var iter = if (selected_key) |*key| try id_set.iteratorFrom(key) else try id_set.iteratorFromIndex(0); 141 var events: std.ArrayList(Item) = .empty; 142 while (try iter.next()) |cursor_value| { 143 const id = try evt.readOrderKeyId(DB, cursor_value); 144 const kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse continue; 145 if (events.items.len == page_size) { 146 event_window.next = .{ .id = try formatId(aa, &id), .kind = kind }; 147 break; 148 } 149 const item = (try readItem(repo_opts.hash, arena, haxy_moment, kind_map, admin_moment, identity, repo_event, view == .active, kind, &id)) orelse continue; 150 try events.append(aa, item); 151 } 152 event_window.events = events.items; 153 } 154 return result; 155 } 156 157 // the haxy state as of one transaction in the repo history. a moment page 158 // reads from it, so undoing or rebasing later transactions cannot change what 159 // it shows, and the events read as they were when the transaction ran 160 fn transactionMoment( 161 comptime repo_kind: rp.RepoKind, 162 comptime repo_opts: rp.RepoOpts(repo_kind), 163 repo: *rp.Repo(repo_kind, repo_opts), 164 index: u64, 165 ) !evt.EventDB(repo_opts.hash).HashMap(.read_only) { 166 if (repo_kind == .git) return error.NotFound; 167 return try evt.currentMomentFromRepoMoment(repo_opts.hash, try repo.core.momentAt(index)); 168 } 169 170 // the moment's set is keyed on the event order, not the update order the 171 // active and removed sets use, so it gets its own walk. one transaction's 172 // events are few, so the whole set is scanned to count both views. 173 fn readMomentEvents( 174 comptime repo_kind: rp.RepoKind, 175 comptime repo_opts: rp.RepoOpts(repo_kind), 176 arena: *std.heap.ArenaAllocator, 177 result: *Self, 178 haxy_moment: evt.EventDB(repo_opts.hash).HashMap(.read_only), 179 kind_map: evt.EventDB(repo_opts.hash).HashMap(.read_only), 180 admin_moment: ?evt.AdminDB.HashMap(.read_only), 181 identity: []const u8, 182 repo_event: evt.Repo, 183 selected: []const u8, 184 ) !void { 185 const DB = evt.EventDB(repo_opts.hash); 186 const aa = arena.allocator(); 187 // the moment names its own position, so the page needs no index from the 188 // url. a transaction that consumed nothing has no set of its own, which 189 // leaves the list empty rather than failing the page: the terminal hosts 190 // build their pages inside the run loop, where an error ends the session 191 const index_cursor = try haxy_moment.getCursor(hash.hashInt(repo_opts.hash, evt.moment_index_key)) orelse return; 192 const ids = try evt.momentEventIds(DB, repo_opts.hash, haxy_moment, try index_cursor.readUint()) orelse return; 193 194 var events: [2]std.ArrayList(Item) = .{ .empty, .empty }; 195 // the selected event's window starts at it, not at the top 196 var awaiting_selected = selected.len != 0; 197 198 var iter = try ids.iterator(); 199 while (try iter.next()) |cursor_value| { 200 const id = try evt.readOrderKeyId(DB, cursor_value); 201 const kind = (try evt.readEventKind(repo_opts.hash, kind_map, &id)) orelse continue; 202 const meta = try readMeta(repo_opts.hash, arena, haxy_moment, kind, &id); 203 const view: ui.RoutablePage.EventsView = if (meta.removed) .removed else .active; 204 const event_window = switch (view) { 205 .active => &result.active, 206 .removed => &result.removed, 207 }; 208 event_window.count += 1; 209 if (awaiting_selected and view == result.view) { 210 if (!std.mem.eql(u8, selected, &std.fmt.bytesToHex(id, .lower))) continue; 211 awaiting_selected = false; 212 } 213 const window_events = &events[@backingInt(view)]; 214 if (window_events.items.len == page_size) { 215 if (event_window.next == null) event_window.next = .{ .id = try formatId(aa, &id), .kind = kind }; 216 continue; 217 } 218 const item = (try readItem(repo_opts.hash, arena, haxy_moment, kind_map, admin_moment, identity, repo_event, view == .active, kind, &id)) orelse continue; 219 try window_events.append(aa, item); 220 } 221 // the window start can name an event this batch no longer holds 222 if (awaiting_selected) return; 223 result.active.events = events[@backingInt(ui.RoutablePage.EventsView.active)].items; 224 result.removed.events = events[@backingInt(ui.RoutablePage.EventsView.removed)].items; 225 } 226 227 pub fn empty(aa: std.mem.Allocator, identity: []const u8, view: ui.RoutablePage.EventsView, local: bool, failure: ?[]const u8) !Self { 228 return .{ 229 .identity = try aa.dupe(u8, identity), 230 .view = view, 231 .selected = null, 232 .active = .{}, 233 .removed = .{}, 234 .header = if (local) try Header.empty(aa, failure) else Header.remote(), 235 }; 236 } 237 238 fn window(self: *const Self, view: ui.RoutablePage.EventsView) *const Window { 239 return switch (view) { 240 .active => &self.active, 241 .removed => &self.removed, 242 }; 243 } 244 245 fn formatId(aa: std.mem.Allocator, id: *const [evt.event_id_size]u8) ![]const u8 { 246 const hex = std.fmt.bytesToHex(id.*, .lower); 247 return try aa.dupe(u8, &hex); 248 } 249 250 const EventMeta = struct { 251 updated_order: u64, 252 removed: bool, 253 }; 254 255 fn readMeta( 256 comptime hash_kind: hash.HashKind, 257 arena: *std.heap.ArenaAllocator, 258 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 259 kind: evt.EventKind, 260 id: *const [evt.event_id_size]u8, 261 ) !EventMeta { 262 return switch (kind) { 263 inline else => |event_kind| blk: { 264 const T = switch (event_kind) { 265 .user => evt.User, 266 .repo => evt.Repo, 267 .fork => evt.Fork, 268 .grant => evt.Grant, 269 .notify => evt.Notify, 270 .discuss => evt.Discussion, 271 .issue => evt.Issue, 272 .comment => evt.Comment, 273 .attach => evt.Attachment, 274 .assign => evt.Assignment, 275 .patchrev => evt.PatchRev, 276 .patch => evt.Patch, 277 }; 278 break :blk (try evt.readRecordSubset(T, EventMeta, evt.EventDB(hash_kind), hash_kind, haxy_moment, arena, id)) orelse return error.NotFound; 279 }, 280 }; 281 } 282 283 fn readItem( 284 comptime hash_kind: hash.HashKind, 285 arena: *std.heap.ArenaAllocator, 286 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 287 kind_map: evt.EventDB(hash_kind).HashMap(.read_only), 288 admin_moment: ?evt.AdminDB.HashMap(.read_only), 289 identity: []const u8, 290 repo_event: evt.Repo, 291 include_view_url: bool, 292 kind: evt.EventKind, 293 id: *const [evt.event_id_size]u8, 294 ) !?Item { 295 const DB = evt.EventDB(hash_kind); 296 // the parts of a record a row shows 297 const Authored = struct { author_email: ?[]const u8 }; 298 const Owned = struct { event: struct { user_id: []const u8 } }; 299 var item: Item = .{ 300 .id = try formatId(arena.allocator(), id), 301 .kind = kind, 302 .author = .unknown, 303 }; 304 switch (kind) { 305 .user => { 306 const record = (try evt.readRecordSubset(evt.User, struct { event: struct { email: []const u8 } }, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 307 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.event.email); 308 }, 309 .repo => { 310 const record = (try evt.readRecordSubset(evt.Repo, Owned, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 311 if (admin_moment) |moment| { 312 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, record.event.user_id)) |user| 313 item.author = .{ .user_name = user.event.name }; 314 } 315 }, 316 .fork => _ = (try evt.readRecordSubset(evt.Fork, struct { removed: bool }, DB, hash_kind, haxy_moment, arena, id)) orelse return null, 317 .grant => _ = (try evt.readRecordSubset(evt.Grant, struct { removed: bool }, DB, hash_kind, haxy_moment, arena, id)) orelse return null, 318 .notify => _ = (try evt.readRecordSubset(evt.Notify, struct { removed: bool }, DB, hash_kind, haxy_moment, arena, id)) orelse return null, 319 .discuss => { 320 const record = (try evt.readRecordSubset(evt.Discussion, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 321 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 322 if (!include_view_url or repo_event.threadRole(.discuss) == null) return item; 323 const route = ui.RoutablePage.repoThreadCommentsRoute(.discuss, identity, item.id, 0) orelse return error.RouteTooLong; 324 item.view_url = try route.toUrl(arena); 325 }, 326 .issue => { 327 const record = (try evt.readRecordSubset(evt.Issue, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 328 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 329 if (!include_view_url or repo_event.threadRole(.issue) == null) return item; 330 const route = ui.RoutablePage.repoThreadCommentsRoute(.issue, identity, item.id, 0) orelse return error.RouteTooLong; 331 item.view_url = try route.toUrl(arena); 332 }, 333 .comment => { 334 const record = (try evt.readRecordSubset(evt.Comment, struct { author_email: ?[]const u8, event: struct { thread_id: [evt.event_id_size * 2]u8 } }, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 335 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 336 if (!include_view_url) return item; 337 var thread_id: [evt.event_id_size]u8 = undefined; 338 _ = std.fmt.hexToBytes(&thread_id, &record.event.thread_id) catch return error.InvalidEventId; 339 const thread_kind = (try evt.readEventKind(hash_kind, kind_map, &thread_id)) orelse return item; 340 if (repo_event.threadRole(thread_kind) == null) return item; 341 const route = ui.RoutablePage.repoThreadCommentRoute(thread_kind, identity, &record.event.thread_id, item.id, 0) orelse return item; 342 item.view_url = try route.toUrl(arena); 343 }, 344 .attach => { 345 const record = (try evt.readRecordSubset(evt.Attachment, struct { author_email: ?[]const u8, event: struct { parent_id: [evt.event_id_size * 2]u8 } }, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 346 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 347 if (!include_view_url) return item; 348 var parent_id: [evt.event_id_size]u8 = undefined; 349 _ = std.fmt.hexToBytes(&parent_id, &record.event.parent_id) catch return error.InvalidEventId; 350 const parent_kind = (try evt.readEventKind(hash_kind, kind_map, &parent_id)) orelse return item; 351 if (repo_event.threadRole(parent_kind) == null) return item; 352 const route = ui.RoutablePage.repoThreadCommentsRoute(parent_kind, identity, &record.event.parent_id, 0) orelse return item; 353 item.view_url = try route.toUrl(arena); 354 }, 355 .assign => { 356 const record = (try evt.readRecordSubset(evt.Assignment, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 357 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 358 }, 359 .patch => { 360 const record = (try evt.readRecordSubset(evt.Patch, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 361 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 362 if (!include_view_url or repo_event.threadRole(.patch) == null) return item; 363 const route = ui.RoutablePage.repoThreadCommentsRoute(.patch, identity, item.id, 0) orelse return error.RouteTooLong; 364 item.view_url = try route.toUrl(arena); 365 }, 366 .patchrev => { 367 const record = (try evt.readRecordSubset(evt.PatchRev, Authored, DB, hash_kind, haxy_moment, arena, id)) orelse return null; 368 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 369 }, 370 } 371 return item; 372 } 373 374 pub const View = struct { 375 box: wgt.Box(ui.Widget), 376 data: *const Self, 377 session: *ui.Session, 378 detailed_index: [2]?usize, 379 380 const header_index = 0; 381 const content_index = 1; 382 const list_index = 0; 383 const detail_index = 1; 384 const list_max_width = 20; 385 const detail_min_width = 40; 386 387 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 388 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 389 errdefer outer.deinit(allocator); 390 391 var header_view = try Header.View.init(allocator, data, session); 392 errdefer header_view.deinit(allocator); 393 // a row taller than the header, leaving a blank line beneath it 394 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_events_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 395 396 var content_stack = try wgt.Stack(ui.Widget).init(allocator); 397 errdefer content_stack.deinit(allocator); 398 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 399 var view_content = try initContent(allocator, data, session, view); 400 errdefer view_content.deinit(allocator); 401 try content_stack.children.put(allocator, view_content.getFocus().id, view_content); 402 } 403 content_stack.getFocus().child_id = content_stack.children.keys()[@backingInt(data.view)]; 404 try outer.children.put(allocator, content_stack.getFocus().id, .{ .widget = .{ .stack = content_stack }, .rect = null, .min_size = null }); 405 outer.getFocus().child_id = outer.children.keys()[content_index]; 406 return .{ .box = outer, .data = data, .session = session, .detailed_index = .{ null, null } }; 407 } 408 409 // an empty window shows the empty message in place of the list and detail 410 fn initContent(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, view: ui.RoutablePage.EventsView) !ui.Widget { 411 const event_window = data.window(view); 412 if (event_window.isEmpty()) return .{ .center = try ui.widget.initEmpty(allocator) }; 413 414 var content_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 415 errdefer content_box.deinit(allocator); 416 417 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert, .stretch = true }); 418 errdefer list_box.deinit(allocator); 419 for (event_window.events) |event| { 420 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, event.kind, event.id, data.moment) orelse return error.RouteTooLong; 421 const link = try session.page_arena.allocator().print("ai:{s}", .{try route.toUrl(session.page_arena)}); 422 try addRow(allocator, &list_box, @tagName(event.kind), link); 423 } 424 if (event_window.next) |next| { 425 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, next.kind, next.id, data.moment) orelse return error.RouteTooLong; 426 const link = try session.page_arena.allocator().print("a:{s}", .{try route.toUrl(session.page_arena)}); 427 try addRow(allocator, &list_box, "next →", link); 428 } 429 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 430 var list_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 431 errdefer list_scroll.deinit(allocator); 432 try content_box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 433 434 // the detail scroll, below the way back to a hidden list 435 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 436 errdefer frame.deinit(allocator); 437 try ui.widget.addListBack(allocator, &frame, "← back to event list"); 438 { 439 var detail_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 440 errdefer detail_box.deinit(allocator); 441 var detail_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 442 errdefer detail_scroll.deinit(allocator); 443 detail_scroll.getFocus().mode = .mouse; 444 frame.getFocus().child_id = detail_scroll.getFocus().id; 445 try frame.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 446 } 447 try content_box.children.put(allocator, frame.getFocus().id, .{ .widget = .{ .box = frame }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 448 449 content_box.getFocus().child_id = content_box.children.keys()[detail_index]; 450 return .{ .box = content_box }; 451 } 452 453 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 454 var row = try wgt.TextBox.init(allocator, text, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 455 errdefer row.deinit(allocator); 456 row.getFocus().mode = .all; 457 row.getFocus().kind = .{ .custom = link }; 458 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 459 } 460 461 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 462 self.box.deinit(allocator); 463 } 464 465 fn header(self: *View) *Header.View { 466 return &self.box.children.values()[header_index].widget.repo_events_header; 467 } 468 469 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 470 return &self.box.children.values()[content_index].widget.stack; 471 } 472 473 fn selectedView(self: *View) ui.RoutablePage.EventsView { 474 const stack = self.contentStack(); 475 const id = stack.getFocus().child_id orelse return .active; 476 return @fromBackingInt(@intCast(stack.children.getIndex(id) orelse 0)); 477 } 478 479 fn eventWindow(self: *View) *const Window { 480 return self.data.window(self.selectedView()); 481 } 482 483 fn content(self: *View) *wgt.Box(ui.Widget) { 484 const stack = self.contentStack(); 485 return &stack.children.values()[@backingInt(self.selectedView())].box; 486 } 487 488 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 489 return &self.content().children.values()[list_index].widget.scroll; 490 } 491 492 fn listBox(self: *View) *wgt.Box(ui.Widget) { 493 return &self.listScroll().child.box; 494 } 495 496 fn detailFrame(self: *View) *wgt.Box(ui.Widget) { 497 return &self.content().children.values()[detail_index].widget.box; 498 } 499 500 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 501 return &self.detailFrame().children.values()[ui.widget.list_pane_index].widget.scroll; 502 } 503 504 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 505 return &self.detailScroll().child.box; 506 } 507 508 fn detailActive(self: *View) bool { 509 return self.content().getFocus().child_id == self.detailFrame().getFocus().id; 510 } 511 512 fn headerActive(self: *View) bool { 513 return self.box.getFocus().child_id == self.header().getFocus().id; 514 } 515 516 fn selectedEventIndex(self: *View) ?usize { 517 const id = self.listBox().getFocus().child_id orelse return null; 518 const index = self.listBox().children.getIndex(id) orelse return null; 519 return if (index < self.eventWindow().events.len) index else null; 520 } 521 522 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 523 self.clearGrid(); 524 if (self.header().getSelectedIndex()) |index| 525 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 526 if (self.eventWindow().isEmpty()) return self.box.build(allocator, constraint, root_focus); 527 try self.refreshDetail(allocator); 528 529 const list = self.listBox(); 530 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 531 .text_box => |*text_box| ui.widget.markSelected(text_box, list.getFocus().child_id == id), 532 else => {}, 533 }; 534 535 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 536 ui.widget.showListBack(self.detailFrame(), !both_fit); 537 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 538 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 539 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 540 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 541 542 try self.box.build(allocator, constraint, root_focus); 543 } 544 545 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 546 const index = self.selectedEventIndex() orelse return; 547 const detailed_index = &self.detailed_index[@backingInt(self.selectedView())]; 548 if (detailed_index.*) |old| if (old == index) return; 549 const box = self.detailBox(); 550 for (box.children.values()) |*child| child.widget.deinit(allocator); 551 box.children.clearAndFree(allocator); 552 box.getFocus().child_id = null; 553 554 const item = self.eventWindow().events[index]; 555 if (item.view_url) |url| { 556 var link = try wgt.TextBox.init(allocator, "view event", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 557 errdefer link.deinit(allocator); 558 link.getFocus().mode = .all; 559 link.getFocus().kind = .{ .custom = try self.session.page_arena.allocator().print("a:{s}", .{url}) }; 560 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 561 } 562 { 563 var kind = try wgt.TextBox.init(allocator, @tagName(item.kind), .{ .border = .single, .round_corners = true, .wrap_kind = .word, .top_label = .{ .text = " kind " } }); 564 errdefer kind.deinit(allocator); 565 kind.getFocus().mode = .all; 566 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 567 } 568 { 569 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 570 errdefer author.deinit(allocator); 571 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 572 } 573 574 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 575 self.detailScroll().x = 0; 576 self.detailScroll().y = 0; 577 self.detailScroll().getFocus().version +%= 1; 578 ui.widget.setListBackLink(self.detailFrame(), self.listBox().children.values()[index].widget.text_box.getFocus().kind.custom); 579 detailed_index.* = index; 580 } 581 582 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 583 // scrolling crosses the sub header boundary the same way arrows do 584 const direction = inp.vertDirection(key); 585 if (self.headerActive()) { 586 if (direction == .down) { 587 // the empty message has nothing to focus 588 if (!self.eventWindow().isEmpty()) root_focus.setFocus(self.content().getFocus().id); 589 } else try self.header().input(allocator, key, root_focus); 590 return; 591 } 592 // the way back to the list, by enter or a click 593 if (self.detailActive()) if (ui.widget.listBackId(self.detailFrame())) |id| { 594 if (inp.activated(root_focus, id, key)) return self.focusList(root_focus); 595 if (root_focus.grandchild_id == id) return switch (direction) { 596 .up => self.header().focusCurrent(root_focus), 597 .down => root_focus.setFocus(self.detailScroll().getFocus().id), 598 .none => if (key == .arrow_left) self.focusList(root_focus), 599 }; 600 if (direction == .up and self.contentAtTop()) return root_focus.setFocus(id); 601 }; 602 if (direction == .up and self.contentAtTop()) { 603 self.header().focusCurrent(root_focus); 604 return; 605 } 606 if (self.detailActive()) { 607 if (key == .arrow_left) { 608 self.focusList(root_focus); 609 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 610 ui.widget.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 611 } 612 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 613 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 614 } else switch (key) { 615 .arrow_right => if (self.detailBox().children.count() > 0) self.focusDetail(root_focus), 616 .enter => if (self.selectedEventIndex() != null) self.focusDetail(root_focus), 617 // when the window is too narrow to lay out the detail pane beside 618 // the list, a click on a row opens it like enter. the row was just 619 // selected, so the detail is swapped to it ahead of the build. 620 .mouse => |mouse| if (self.selectedEventIndex() != null and self.content().children.values()[detail_index].rect == null and ui.widget.clickOnSelectedRow(self.listBox(), root_focus, mouse)) { 621 try self.refreshDetail(allocator); 622 self.focusDetail(root_focus); 623 }, 624 else => {}, 625 } 626 } 627 628 // enter the detail pane, landing on the way back to the list when it shows 629 fn focusDetail(self: *View, root_focus: *Focus) void { 630 if (ui.widget.listBackId(self.detailFrame()) != null) return ui.widget.focusListBack(self.detailFrame(), root_focus); 631 root_focus.setFocus(self.detailScroll().getFocus().id); 632 } 633 634 fn focusList(self: *View, root_focus: *Focus) void { 635 root_focus.setFocus(self.listScroll().getFocus().id); 636 } 637 638 fn contentAtTop(self: *View) bool { 639 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 640 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 641 const id = box.getFocus().child_id orelse return true; 642 return box.children.getIndex(id) == 0 and scroll.y == 0; 643 } 644 645 pub fn atTop(self: *View) bool { 646 return self.headerActive(); 647 } 648 649 // arriving from the page's tabs lands on the sub header, not the content 650 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 651 self.header().focusCurrent(root_focus); 652 return true; 653 } 654 655 pub fn clearGrid(self: *View) void { 656 self.box.clearGrid(); 657 } 658 659 pub fn getGrid(self: View) ?Grid { 660 return self.box.getGrid(); 661 } 662 663 pub fn getFocus(self: *View) *Focus { 664 return self.box.getFocus(); 665 } 666 }; 667 668 pub const Header = struct { 669 sync_status: ?[]const u8, 670 671 const nothing = "nothing to sync"; 672 const unsynced = "sync needed!"; 673 674 const Progress = struct { 675 message: *std.Io.Writer.Allocating, 676 677 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 678 switch (event) { 679 .text => |value| { 680 self.message.clearRetainingCapacity(); 681 try self.message.writer.writeAll(value); 682 }, 683 else => {}, 684 } 685 } 686 }; 687 688 pub fn local( 689 comptime repo_kind: rp.RepoKind, 690 comptime repo_opts: rp.RepoOpts(repo_kind), 691 aa: std.mem.Allocator, 692 repo: *rp.Repo(repo_kind, repo_opts), 693 io: std.Io, 694 failure: ?[]const u8, 695 ) !Header { 696 if (failure != null) return Header.empty(aa, failure); 697 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 698 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 699 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 700 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 701 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 702 } 703 704 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 705 return .{ .sync_status = if (failure) |name| try aa.print("error: {s}", .{name}) else nothing }; 706 } 707 708 pub fn remote() Header { 709 return .{ .sync_status = null }; 710 } 711 712 pub const View = struct { 713 box: wgt.Box(ui.Widget), 714 session: *ui.Session, 715 tab_ids: [2]usize, 716 // the tab stays selected while the sync button has focus 717 selected_tab: usize, 718 button_id: ?usize, 719 720 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 721 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 722 errdefer box.deinit(allocator); 723 var tab_ids: [2]usize = undefined; 724 const view_labels = [_][]const u8{ active_tab_label, removed_tab_label }; 725 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, view_labels, 0..) |view, view_label, i| { 726 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "", data.moment) orelse return error.RouteTooLong; 727 const count = data.window(view).count; 728 var label_buf: [64]u8 = undefined; 729 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ view_label, count }); 730 var tab = try wgt.TextBox.init(allocator, label, .{ .border = .hidden, .round_corners = true, .wrap_kind = .none }); 731 errdefer tab.deinit(allocator); 732 tab.getFocus().mode = .all; 733 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 734 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 735 tab_ids[i] = tab.getFocus().id; 736 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 737 } 738 739 // pushes the sync button and status to the right 740 { 741 var spacer = try ui.widget.Spacer.init(allocator); 742 errdefer spacer.deinit(allocator); 743 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null, .flex = .grow }); 744 } 745 746 var button_id: ?usize = null; 747 if (data.header.sync_status != null) { 748 box.getFocus().kind = .{ .custom = "form:/sync" }; 749 var button = try wgt.TextBox.init(allocator, "sync", .{ .border = .single, .round_corners = true, .wrap_kind = .none }); 750 errdefer button.deinit(allocator); 751 button.getFocus().mode = .all; 752 button.getFocus().kind = .{ .custom = "submit" }; 753 button_id = button.getFocus().id; 754 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 755 } 756 757 if (data.header.sync_status) |status_text| { 758 var status = try wgt.TextBox.init(allocator, status_text, .{ .border = .hidden, .wrap_kind = .none }); 759 errdefer status.deinit(allocator); 760 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 761 } 762 box.getFocus().child_id = tab_ids[@backingInt(data.view)]; 763 return .{ .box = box, .session = session, .tab_ids = tab_ids, .selected_tab = @backingInt(data.view), .button_id = button_id }; 764 } 765 766 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 767 self.box.deinit(allocator); 768 } 769 770 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 771 self.clearGrid(); 772 if (self.getSelectedIndex()) |index| self.selected_tab = index; 773 for (self.tab_ids, 0..) |id, index| { 774 const child = self.box.children.getPtr(id) orelse continue; 775 const tab = switch (child.widget) { 776 .text_box => |*text_box| text_box, 777 else => continue, 778 }; 779 ui.widget.markSelected(tab, index == self.selected_tab); 780 } 781 try self.box.build(allocator, constraint, root_focus); 782 } 783 784 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 785 const selected = self.box.getFocus().child_id orelse return; 786 const stops = [_]?usize{ self.tab_ids[0], self.tab_ids[1], self.button_id }; 787 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 788 const current = std.mem.indexOfScalar(?usize, stops[0..count], selected) orelse return; 789 if (inp.moveTab(key, current, count)) |next| { 790 root_focus.setFocus(stops[next] orelse unreachable); 791 return; 792 } 793 switch (key) { 794 .enter => if (self.syncFocused()) try self.requestSync(allocator), 795 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 796 else => {}, 797 } 798 } 799 800 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 801 if (!self.session.is_terminal) return; 802 if (self.session.host_request != null) return; 803 const button_id = self.button_id orelse return; 804 const child = self.box.children.getPtr(button_id) orelse return; 805 const button = switch (child.widget) { 806 .text_box => |*button| button, 807 else => return, 808 }; 809 try button.setContent(allocator, "syncing..."); 810 self.session.host_request = .sync_events; 811 } 812 813 fn syncFocused(self: *Header.View) bool { 814 const button_id = self.button_id orelse return false; 815 return self.box.getFocus().child_id == button_id; 816 } 817 818 pub fn getSelectedIndex(self: *Header.View) ?usize { 819 const id = self.box.getFocus().child_id orelse return null; 820 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 821 } 822 823 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 824 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 825 } 826 827 pub fn clearGrid(self: *Header.View) void { 828 self.box.clearGrid(); 829 } 830 831 pub fn getGrid(self: Header.View) ?Grid { 832 return self.box.getGrid(); 833 } 834 835 pub fn getFocus(self: *Header.View) *Focus { 836 return self.box.getFocus(); 837 } 838 }; 839 }; 840 841 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 842 if (comptime builtin.target.cpu.arch == .wasm32) return; 843 const io = session.io orelse return; 844 const source = session.local orelse return; 845 defer session.refresh_requested = true; 846 const failure = sync(io, allocator, source) catch |err| { 847 session.data.sync_failure = @errorName(err); 848 return; 849 }; 850 if (failure) |message| { 851 defer allocator.free(message); 852 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 853 return; 854 } 855 session.data.sync_failure = null; 856 switch (session.data.current_page) { 857 .repo_events => |route| { 858 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "", route.moment)) |first| 859 session.data.current_page = first; 860 }, 861 else => {}, 862 } 863 } 864 865 fn remoteName( 866 comptime repo_kind: rp.RepoKind, 867 comptime repo_opts: rp.RepoOpts(repo_kind), 868 repo: *rp.Repo(repo_kind, repo_opts), 869 io: std.Io, 870 allocator: std.mem.Allocator, 871 ) !?[]u8 { 872 var remotes = try repo.listRemotes(io, allocator); 873 defer remotes.deinit(); 874 const names = remotes.sections.keys(); 875 if (names.len == 0) return null; 876 for (names) |name| { 877 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 878 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 879 } 880 return try allocator.dupe(u8, names[0]); 881 } 882 883 fn isLoopbackSshUrl(url: []const u8) bool { 884 const uri = std.Uri.parse(url) catch return false; 885 const host = switch (uri.host orelse return false) { 886 .raw => |value| value, 887 .percent_encoded => |value| value, 888 }; 889 return std.mem.eql(u8, host, "localhost") or 890 std.mem.eql(u8, host, "127.0.0.1") or 891 std.mem.eql(u8, host, "::1") or 892 std.mem.eql(u8, host, "[::1]"); 893 } 894 895 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 896 var message: std.Io.Writer.Allocating = .init(allocator); 897 defer message.deinit(); 898 899 switch (source.repo_kind) { 900 inline else => |repo_kind| { 901 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 902 defer any_repo.deinit(io, allocator); 903 switch (any_repo) { 904 inline else => |*repo| { 905 const repo_opts = repo.self_repo_opts; 906 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 907 defer allocator.free(remote_name); 908 909 var config = try repo.listConfig(io, allocator); 910 defer config.deinit(); 911 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 912 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 913 const remote_section = try allocator.print("remote.{s}", .{remote_name}); 914 defer allocator.free(remote_section); 915 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 916 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 917 const push_url = remote_config.get("pushurl") orelse fetch_url; 918 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 919 const fetch_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(fetch_url)) loopback_opts else "" }); 920 defer allocator.free(fetch_ssh_command); 921 const push_ssh_command = try allocator.print("{s}{s} -o BatchMode=yes -o LogLevel=QUIET", .{ ssh, if (isLoopbackSshUrl(push_url)) loopback_opts else "" }); 922 defer allocator.free(push_ssh_command); 923 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 924 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 925 926 const fetch_refspec = try allocator.print("+refs/heads/{s}:refs/remotes/{s}/{s}", .{ evt.events_ref.name, remote_name, evt.events_ref.name }); 927 defer allocator.free(fetch_refspec); 928 var fetch_opts = fetch_transport_opts; 929 fetch_opts.refspecs = &.{fetch_refspec}; 930 message.clearRetainingCapacity(); 931 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 932 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 933 return err; 934 }; 935 936 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 937 const local_oid = try repo.readRef(io, evt.events_ref); 938 const remote_oid = try repo.readRef(io, remote_ref); 939 if (local_oid == null and remote_oid == null) { 940 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 941 return null; 942 } 943 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 944 try evt.consume(.local, .repo, repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 945 946 const push_refspec = try allocator.print("refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 947 defer allocator.free(push_refspec); 948 message.clearRetainingCapacity(); 949 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 950 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 951 return err; 952 }; 953 message.clearRetainingCapacity(); 954 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 955 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 956 return err; 957 }; 958 }, 959 } 960 }, 961 } 962 return null; 963 } 964