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