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