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