╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
176 created_order: u64, 177 removed: bool, 178 }; 179 180 fn readMeta( 181 comptime hash_kind: hash.HashKind, 182 arena: *std.heap.ArenaAllocator, 183 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 184 kind: evt.EventKind, 185 id: *const [evt.event_id_size]u8, 186 ) !EventMeta { 187 return switch (kind) { 188 inline else => |event_kind| blk: { 189 const T = switch (event_kind) { 190 .user => evt.User, 191 .repo => evt.Repo, 192 .issue => evt.Issue, 193 .discuss => evt.Discussion, 194 .comment => evt.Comment, 195 .attach => evt.Attachment, 196 }; 197 const record = (try readRecord(T, hash_kind, arena, haxy_moment, id)) orelse return error.NotFound; 198 break :blk .{ .created_order = record.created_order, .removed = record.removed }; 199 }, 200 }; 201 } 202 203 fn readItem( 204 comptime hash_kind: hash.HashKind, 205 arena: *std.heap.ArenaAllocator, 206 haxy_moment: evt.EventDB(hash_kind).HashMap(.read_only), 207 kind_map: evt.EventDB(hash_kind).HashMap(.read_only), 208 admin_moment: ?evt.AdminDB.HashMap(.read_only), 209 identity: []const u8, 210 include_view_url: bool, 211 kind: evt.EventKind, 212 id: *const [evt.event_id_size]u8, 213 ) !?Item { 214 var item: Item = .{ 215 .id = try formatId(arena.allocator(), id), 216 .kind = kind, 217 .author = .unknown, 218 }; 219 switch (kind) { 220 .user => { 221 const record = (try readRecord(evt.User, hash_kind, arena, haxy_moment, id)) orelse return null; 222 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.event.email); 223 }, 224 .repo => { 225 const record = (try readRecord(evt.Repo, hash_kind, arena, haxy_moment, id)) orelse return null; 226 if (admin_moment) |moment| { 227 if (try evt.User.readById(evt.AdminDB, evt.admin_repo_opts.hash, moment, arena, record.event.user_id)) |user| 228 item.author = .{ .user_name = user.event.name }; 229 } 230 }, 231 .issue => { 232 const record = (try readRecord(evt.Issue, hash_kind, arena, haxy_moment, id)) orelse return null; 233 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 234 if (!include_view_url) return item; 235 const route = ui.RoutablePage.repoThreadCommentsRoute(.issue, identity, item.id, 0) orelse return error.RouteTooLong; 236 item.view_url = try route.toUrl(arena); 237 }, 238 .discuss => { 239 const record = (try readRecord(evt.Discussion, hash_kind, arena, haxy_moment, id)) orelse return null; 240 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 241 if (!include_view_url) return item; 242 const route = ui.RoutablePage.repoThreadCommentsRoute(.discuss, identity, item.id, 0) orelse return error.RouteTooLong; 243 item.view_url = try route.toUrl(arena); 244 }, 245 .comment => { 246 const record = (try readRecord(evt.Comment, hash_kind, arena, haxy_moment, id)) orelse return null; 247 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 248 if (!include_view_url) return item; 249 var thread_id: [evt.event_id_size]u8 = undefined; 250 _ = std.fmt.hexToBytes(&thread_id, &record.event.thread_id) catch return error.InvalidEventId; 251 const thread_kind = (try evt.readEventKind(hash_kind, kind_map, &thread_id)) orelse return item; 252 const route = ui.RoutablePage.repoThreadCommentRoute(thread_kind, identity, &record.event.thread_id, item.id, 0) orelse return item; 253 item.view_url = try route.toUrl(arena); 254 }, 255 .attach => { 256 const record = (try readRecord(evt.Attachment, hash_kind, arena, haxy_moment, id)) orelse return null; 257 item.author = try ui.Author.initFromEmail(admin_moment, arena, record.author_email); 258 if (!include_view_url) return item; 259 var parent_id: [evt.event_id_size]u8 = undefined; 260 _ = std.fmt.hexToBytes(&parent_id, &record.event.parent_id) catch return error.InvalidEventId; 261 const parent_kind = (try evt.readEventKind(hash_kind, kind_map, &parent_id)) orelse return item; 262 const route = ui.RoutablePage.repoThreadCommentsRoute(parent_kind, identity, &record.event.parent_id, 0) orelse return item; 263 item.view_url = try route.toUrl(arena); 264 }, 265 } 266 return item; 267 } 268 269 pub const View = struct { 270 box: wgt.Box(ui.Widget), 271 data: *const Self, 272 session: *ui.Session, 273 detailed_index: [2]?usize, 274 275 const header_index = 0; 276 const content_index = 1; 277 const list_index = 0; 278 const detail_index = 1; 279 const list_max_width = 20; 280 const detail_min_width = 40; 281 282 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 283 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 284 errdefer outer.deinit(allocator); 285 286 var header_view = try Header.View.init(allocator, data, session); 287 errdefer header_view.deinit(allocator); 288 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_events_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 289 290 var content_stack = try wgt.Stack(ui.Widget).init(allocator); 291 errdefer content_stack.deinit(allocator); 292 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }) |view| { 293 var content_box = try initContent(allocator, data, session, view); 294 errdefer content_box.deinit(allocator); 295 try content_stack.children.put(allocator, content_box.getFocus().id, .{ .box = content_box }); 296 } 297 content_stack.getFocus().child_id = content_stack.children.keys()[@intFromEnum(data.view)]; 298 try outer.children.put(allocator, content_stack.getFocus().id, .{ .widget = .{ .stack = content_stack }, .rect = null, .min_size = null }); 299 outer.getFocus().child_id = outer.children.keys()[header_index]; 300 return .{ .box = outer, .data = data, .session = session, .detailed_index = .{ null, null } }; 301 } 302 303 fn initContent(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session, view: ui.RoutablePage.EventsView) !wgt.Box(ui.Widget) { 304 var content_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 305 errdefer content_box.deinit(allocator); 306 307 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 308 errdefer list_box.deinit(allocator); 309 const event_window = data.window(view); 310 for (event_window.events) |event| { 311 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, event.kind, event.id) orelse return error.RouteTooLong; 312 const link = try std.fmt.allocPrint(session.page_arena.allocator(), "ai:{s}", .{try route.toUrl(session.page_arena)}); 313 try addRow(allocator, &list_box, @tagName(event.kind), link); 314 } 315 if (event_window.next) |next| { 316 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, next.kind, next.id) orelse return error.RouteTooLong; 317 const link = try std.fmt.allocPrint(session.page_arena.allocator(), "a:{s}", .{try route.toUrl(session.page_arena)}); 318 try addRow(allocator, &list_box, "next →", link); 319 } 320 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 321 var list_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 322 errdefer list_scroll.deinit(allocator); 323 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 } }); 324 325 var detail_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 326 errdefer detail_box.deinit(allocator); 327 var detail_scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 328 errdefer detail_scroll.deinit(allocator); 329 try content_box.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 330 331 content_box.getFocus().child_id = content_box.children.keys()[list_index]; 332 return content_box; 333 } 334 335 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 336 var row = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 337 errdefer row.deinit(allocator); 338 row.getFocus().focusable = true; 339 row.getFocus().kind = .{ .custom = link }; 340 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 341 } 342 343 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 344 self.box.deinit(allocator); 345 } 346 347 fn header(self: *View) *Header.View { 348 return &self.box.children.values()[header_index].widget.repo_events_header; 349 } 350 351 fn contentStack(self: *View) *wgt.Stack(ui.Widget) { 352 return &self.box.children.values()[content_index].widget.stack; 353 } 354 355 fn selectedView(self: *View) ui.RoutablePage.EventsView { 356 const stack = self.contentStack(); 357 const id = stack.getFocus().child_id orelse return .active; 358 return @enumFromInt(stack.children.getIndex(id) orelse 0); 359 } 360 361 fn eventWindow(self: *View) *const Window { 362 return self.data.window(self.selectedView()); 363 } 364 365 fn content(self: *View) *wgt.Box(ui.Widget) { 366 const stack = self.contentStack(); 367 return &stack.children.values()[@intFromEnum(self.selectedView())].box; 368 } 369 370 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 371 return &self.content().children.values()[list_index].widget.scroll; 372 } 373 374 fn listBox(self: *View) *wgt.Box(ui.Widget) { 375 return &self.listScroll().child.box; 376 } 377 378 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 379 return &self.content().children.values()[detail_index].widget.scroll; 380 } 381 382 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 383 return &self.detailScroll().child.box; 384 } 385 386 fn detailActive(self: *View) bool { 387 return self.content().getFocus().child_id == self.detailScroll().getFocus().id; 388 } 389 390 fn headerActive(self: *View) bool { 391 return self.box.getFocus().child_id == self.header().getFocus().id; 392 } 393 394 fn selectedEventIndex(self: *View) ?usize { 395 const id = self.listBox().getFocus().child_id orelse return null; 396 const index = self.listBox().children.getIndex(id) orelse return null; 397 return if (index < self.eventWindow().events.len) index else null; 398 } 399 400 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 401 self.clearGrid(); 402 if (self.header().getSelectedIndex()) |index| 403 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 404 const view = self.selectedView(); 405 var selected_kind: ?evt.EventKind = null; 406 var selected_id: []const u8 = ""; 407 if (view == self.data.view) { 408 if (self.data.selected) |selected| { 409 selected_kind = selected.kind; 410 selected_id = selected.id; 411 } 412 } 413 if (ui.RoutablePage.repoEventsRoute(self.data.identity, view, selected_kind, selected_id)) |route| 414 self.session.data.current_page = route; 415 try self.refreshDetail(allocator); 416 417 if (root_focus.grandchild_id) |id| if (self.box.getFocus().children.contains(id)) { 418 if (self.selectedEventIndex()) |index| { 419 const event = self.eventWindow().events[index]; 420 if (ui.RoutablePage.repoEventsRoute(self.data.identity, view, event.kind, event.id)) |route| 421 self.session.data.current_page = route; 422 } 423 }; 424 425 const list = self.listBox(); 426 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 427 .text_box => |*text_box| text_box.options.border_style = if (list.getFocus().child_id == id) .single else .hidden, 428 else => {}, 429 }; 430 431 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 432 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 433 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 434 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 435 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 436 437 try self.box.build(allocator, constraint, root_focus); 438 } 439 440 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 441 const index = self.selectedEventIndex() orelse return; 442 const detailed_index = &self.detailed_index[@intFromEnum(self.selectedView())]; 443 if (detailed_index.*) |old| if (old == index) return; 444 const box = self.detailBox(); 445 for (box.children.values()) |*child| child.widget.deinit(allocator); 446 box.children.clearAndFree(allocator); 447 box.getFocus().child_id = null; 448 449 const item = self.eventWindow().events[index]; 450 if (item.view_url) |url| { 451 var link = try wgt.TextBox(ui.Widget).init(allocator, "view event", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 452 errdefer link.deinit(allocator); 453 link.getFocus().focusable = true; 454 link.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{url}) }; 455 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 456 } 457 { 458 var kind = try wgt.TextBox(ui.Widget).init(allocator, @tagName(item.kind), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " kind " }); 459 errdefer kind.deinit(allocator); 460 kind.getFocus().focusable = true; 461 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 462 } 463 { 464 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 465 errdefer author.deinit(allocator); 466 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 467 } 468 469 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 470 self.detailScroll().x = 0; 471 self.detailScroll().y = 0; 472 self.detailScroll().getFocus().version +%= 1; 473 detailed_index.* = index; 474 } 475 476 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 477 if (self.headerActive()) { 478 if (key == .arrow_down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 479 return; 480 } 481 if (key == .arrow_up and self.contentAtTop()) { 482 self.header().focusCurrent(root_focus); 483 return; 484 } 485 if (self.detailActive()) { 486 if (key == .arrow_left) { 487 root_focus.setFocus(self.listScroll().getFocus().id); 488 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 489 ui.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 490 } 491 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 492 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 493 } else switch (key) { 494 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 495 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 496 else => {}, 497 } 498 } 499 500 fn contentAtTop(self: *View) bool { 501 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 502 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 503 const id = box.getFocus().child_id orelse return true; 504 return box.children.getIndex(id) == 0 and scroll.y == 0; 505 } 506 507 pub fn getSelectedIndex(self: *View) ?usize { 508 return if (self.headerActive()) 0 else 1; 509 } 510 511 pub fn clearGrid(self: *View) void { 512 self.box.clearGrid(); 513 } 514 515 pub fn getGrid(self: View) ?Grid { 516 return self.box.getGrid(); 517 } 518 519 pub fn getFocus(self: *View) *Focus { 520 return self.box.getFocus(); 521 } 522 }; 523 524 pub const Header = struct { 525 sync_status: ?[]const u8, 526 527 const nothing = "nothing to sync"; 528 const unsynced = "sync needed!"; 529 530 const Progress = struct { 531 message: *std.Io.Writer.Allocating, 532 533 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 534 switch (event) { 535 .text => |value| { 536 self.message.clearRetainingCapacity(); 537 try self.message.writer.writeAll(value); 538 }, 539 else => {}, 540 } 541 } 542 }; 543 544 pub fn local( 545 comptime repo_kind: rp.RepoKind, 546 comptime repo_opts: rp.RepoOpts(repo_kind), 547 aa: std.mem.Allocator, 548 repo: *rp.Repo(repo_kind, repo_opts), 549 io: std.Io, 550 failure: ?[]const u8, 551 ) !Header { 552 if (failure != null) return Header.empty(aa, failure); 553 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 554 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 555 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 556 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 557 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 558 } 559 560 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 561 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 562 } 563 564 pub fn remote() Header { 565 return .{ .sync_status = null }; 566 } 567 568 pub const View = struct { 569 box: wgt.Box(ui.Widget), 570 session: *ui.Session, 571 tab_ids: [2]usize, 572 button_id: ?usize, 573 574 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 575 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 576 errdefer box.deinit(allocator); 577 var tab_ids: [2]usize = undefined; 578 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, 0..) |view, i| { 579 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "") orelse return error.RouteTooLong; 580 const count = data.window(view).count; 581 const label = try std.fmt.allocPrint(session.page_arena.allocator(), "{s} ({d})", .{ @tagName(view), count }); 582 var tab = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 583 errdefer tab.deinit(allocator); 584 tab.getFocus().focusable = true; 585 tab.getFocus().kind = .{ .custom = try std.fmt.allocPrint(session.page_arena.allocator(), "ai:{s}", .{try route.toUrl(session.page_arena)}) }; 586 tab_ids[i] = tab.getFocus().id; 587 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 588 } 589 590 var button_id: ?usize = null; 591 if (data.header.sync_status != null) { 592 box.getFocus().kind = .{ .custom = "form:/sync" }; 593 var button = try wgt.TextBox(ui.Widget).init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 594 errdefer button.deinit(allocator); 595 button.getFocus().focusable = true; 596 button.getFocus().kind = .{ .custom = "submit" }; 597 button_id = button.getFocus().id; 598 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 599 } 600 601 if (data.header.sync_status) |status_text| { 602 var status = try wgt.TextBox(ui.Widget).init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 603 errdefer status.deinit(allocator); 604 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .shrink = true }); 605 } 606 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 607 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 608 } 609 610 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 611 self.box.deinit(allocator); 612 } 613 614 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 615 self.clearGrid(); 616 for (self.tab_ids) |id| { 617 const child = self.box.children.getPtr(id) orelse continue; 618 const tab = switch (child.widget) { 619 .text_box => |*text_box| text_box, 620 else => continue, 621 }; 622 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 623 } 624 try self.box.build(allocator, constraint, root_focus); 625 } 626 627 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 628 const selected = self.box.getFocus().child_id orelse return; 629 const current = self.box.children.getIndex(selected) orelse return; 630 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 631 if (inp.moveTab(key, current, count)) |next| { 632 root_focus.setFocus(self.box.children.keys()[next]); 633 return; 634 } 635 switch (key) { 636 .enter => if (self.syncFocused()) self.requestSync(), 637 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) self.requestSync(), 638 else => {}, 639 } 640 _ = allocator; 641 } 642 643 fn requestSync(self: *Header.View) void { 644 if (!self.session.is_terminal) return; 645 if (self.session.host_request != null) return; 646 self.session.host_request = .sync_events; 647 const button_id = self.button_id orelse return; 648 const child = self.box.children.getPtr(button_id) orelse return; 649 const button = switch (child.widget) { 650 .text_box => |*button| button, 651 else => return, 652 }; 653 button.content = "syncing..."; 654 for (button.box.children.values()) |*line| switch (line.widget) { 655 .text => |*text| text.content = "syncing...", 656 else => {}, 657 }; 658 } 659 660 fn syncFocused(self: *Header.View) bool { 661 const button_id = self.button_id orelse return false; 662 return self.box.getFocus().child_id == button_id; 663 } 664 665 pub fn getSelectedIndex(self: *Header.View) ?usize { 666 const id = self.box.getFocus().child_id orelse return null; 667 return std.mem.indexOfScalar(usize, &self.tab_ids, id); 668 } 669 670 pub fn focusCurrent(self: *Header.View, root_focus: *Focus) void { 671 root_focus.setFocus(self.box.getFocus().child_id orelse self.tab_ids[0]); 672 } 673 674 pub fn clearGrid(self: *Header.View) void { 675 self.box.clearGrid(); 676 } 677 678 pub fn getGrid(self: Header.View) ?Grid { 679 return self.box.getGrid(); 680 } 681 682 pub fn getFocus(self: *Header.View) *Focus { 683 return self.box.getFocus(); 684 } 685 }; 686 }; 687 688 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 689 if (comptime builtin.target.cpu.arch == .wasm32) return; 690 const io = session.io orelse return; 691 const source = session.local orelse return; 692 defer session.refresh_requested = true; 693 const failure = sync(io, allocator, source) catch |err| { 694 session.data.sync_failure = @errorName(err); 695 return; 696 }; 697 if (failure) |message| { 698 defer allocator.free(message); 699 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 700 return; 701 } 702 session.data.sync_failure = null; 703 switch (session.data.current_page) { 704 .repo_events => |route| { 705 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), route.view, null, "")) |first| 706 session.data.current_page = first; 707 }, 708 else => {}, 709 } 710 } 711 712 fn remoteName( 713 comptime repo_kind: rp.RepoKind, 714 comptime repo_opts: rp.RepoOpts(repo_kind), 715 repo: *rp.Repo(repo_kind, repo_opts), 716 io: std.Io, 717 allocator: std.mem.Allocator, 718 ) !?[]u8 { 719 var remotes = try repo.listRemotes(io, allocator); 720 defer remotes.deinit(); 721 const names = remotes.sections.keys(); 722 if (names.len == 0) return null; 723 for (names) |name| { 724 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 725 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 726 } 727 return try allocator.dupe(u8, names[0]); 728 } 729 730 fn isLoopbackSshUrl(url: []const u8) bool { 731 const uri = std.Uri.parse(url) catch return false; 732 const host = switch (uri.host orelse return false) { 733 .raw => |value| value, 734 .percent_encoded => |value| value, 735 }; 736 return std.mem.eql(u8, host, "localhost") or 737 std.mem.eql(u8, host, "127.0.0.1") or 738 std.mem.eql(u8, host, "::1") or 739 std.mem.eql(u8, host, "[::1]"); 740 } 741 742 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 743 var message: std.Io.Writer.Allocating = .init(allocator); 744 defer message.deinit(); 745 746 switch (source.repo_kind) { 747 inline else => |repo_kind| { 748 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, source.localInitOpts()); 749 defer any_repo.deinit(io, allocator); 750 switch (any_repo) { 751 inline else => |*repo| { 752 const repo_opts = repo.self_repo_opts; 753 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 754 defer allocator.free(remote_name); 755 756 var config = try repo.listConfig(io, allocator); 757 defer config.deinit(); 758 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 759 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 760 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 761 defer allocator.free(remote_section); 762 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 763 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 764 const push_url = remote_config.get("pushurl") orelse fetch_url; 765 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 766 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 "" }); 767 defer allocator.free(fetch_ssh_command); 768 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 "" }); 769 defer allocator.free(push_ssh_command); 770 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 771 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 772 773 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 }); 774 defer allocator.free(fetch_refspec); 775 var fetch_opts = fetch_transport_opts; 776 fetch_opts.refspecs = &.{fetch_refspec}; 777 message.clearRetainingCapacity(); 778 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 779 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 780 return err; 781 }; 782 783 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 784 const local_oid = try repo.readRef(io, evt.events_ref); 785 const remote_oid = try repo.readRef(io, remote_ref); 786 if (local_oid == null and remote_oid == null) { 787 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 788 return null; 789 } 790 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 791 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 792 793 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 794 defer allocator.free(push_refspec); 795 message.clearRetainingCapacity(); 796 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 797 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 798 return err; 799 }; 800 message.clearRetainingCapacity(); 801 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 802 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 803 return err; 804 }; 805 }, 806 } 807 }, 808 } 809 return null; 810 } 811