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