╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
401 return &self.content().children.values()[detail_index].widget.scroll; 402 } 403 404 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 405 return &self.detailScroll().child.box; 406 } 407 408 fn detailActive(self: *View) bool { 409 return self.content().getFocus().child_id == self.detailScroll().getFocus().id; 410 } 411 412 fn headerActive(self: *View) bool { 413 return self.box.getFocus().child_id == self.header().getFocus().id; 414 } 415 416 fn selectedEventIndex(self: *View) ?usize { 417 const id = self.listBox().getFocus().child_id orelse return null; 418 const index = self.listBox().children.getIndex(id) orelse return null; 419 return if (index < self.eventWindow().events.len) index else null; 420 } 421 422 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 423 self.clearGrid(); 424 if (self.header().getSelectedIndex()) |index| 425 self.contentStack().getFocus().child_id = self.contentStack().children.keys()[index]; 426 try self.refreshDetail(allocator); 427 428 const list = self.listBox(); 429 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 430 .text_box => |*text_box| text_box.options.border_style = if (list.getFocus().child_id == id) .single else .hidden, 431 else => {}, 432 }; 433 434 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 435 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 436 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 437 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 438 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 439 440 try self.box.build(allocator, constraint, root_focus); 441 } 442 443 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 444 const index = self.selectedEventIndex() orelse return; 445 const detailed_index = &self.detailed_index[@intFromEnum(self.selectedView())]; 446 if (detailed_index.*) |old| if (old == index) return; 447 const box = self.detailBox(); 448 for (box.children.values()) |*child| child.widget.deinit(allocator); 449 box.children.clearAndFree(allocator); 450 box.getFocus().child_id = null; 451 452 const item = self.eventWindow().events[index]; 453 if (item.view_url) |url| { 454 var link = try wgt.TextBox.init(allocator, "view event", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 455 errdefer link.deinit(allocator); 456 link.getFocus().mode = .all; 457 link.getFocus().kind = .{ .custom = try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{url}) }; 458 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 459 } 460 { 461 var kind = try wgt.TextBox.init(allocator, @tagName(item.kind), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .word, .label = " kind " }); 462 errdefer kind.deinit(allocator); 463 kind.getFocus().mode = .all; 464 try box.children.put(allocator, kind.getFocus().id, .{ .widget = .{ .text_box = kind }, .rect = null, .min_size = null }); 465 } 466 { 467 var author = try ui.authorBox(allocator, self.session.page_arena, item.author); 468 errdefer author.deinit(allocator); 469 try box.children.put(allocator, author.getFocus().id, .{ .widget = .{ .text_box = author }, .rect = null, .min_size = null }); 470 } 471 472 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 473 self.detailScroll().x = 0; 474 self.detailScroll().y = 0; 475 self.detailScroll().getFocus().version +%= 1; 476 detailed_index.* = index; 477 } 478 479 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 480 if (self.headerActive()) { 481 if (key == .arrow_down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 482 return; 483 } 484 if (key == .arrow_up and self.contentAtTop()) { 485 self.header().focusCurrent(root_focus); 486 return; 487 } 488 if (self.detailActive()) { 489 if (key == .arrow_left) { 490 root_focus.setFocus(self.listScroll().getFocus().id); 491 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 492 ui.widget.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 493 } 494 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 495 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 496 } else switch (key) { 497 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 498 .enter => if (self.selectedEventIndex() != null) root_focus.setFocus(self.detailScroll().getFocus().id), 499 else => {}, 500 } 501 } 502 503 fn contentAtTop(self: *View) bool { 504 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 505 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 506 const id = box.getFocus().child_id orelse return true; 507 return box.children.getIndex(id) == 0 and scroll.y == 0; 508 } 509 510 pub fn atTop(self: *View) bool { 511 return self.headerActive(); 512 } 513 514 pub fn clearGrid(self: *View) void { 515 self.box.clearGrid(); 516 } 517 518 pub fn getGrid(self: View) ?Grid { 519 return self.box.getGrid(); 520 } 521 522 pub fn getFocus(self: *View) *Focus { 523 return self.box.getFocus(); 524 } 525 }; 526 527 pub const Header = struct { 528 sync_status: ?[]const u8, 529 530 const nothing = "nothing to sync"; 531 const unsynced = "sync needed!"; 532 533 const Progress = struct { 534 message: *std.Io.Writer.Allocating, 535 536 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 537 switch (event) { 538 .text => |value| { 539 self.message.clearRetainingCapacity(); 540 try self.message.writer.writeAll(value); 541 }, 542 else => {}, 543 } 544 } 545 }; 546 547 pub fn local( 548 comptime repo_kind: rp.RepoKind, 549 comptime repo_opts: rp.RepoOpts(repo_kind), 550 aa: std.mem.Allocator, 551 repo: *rp.Repo(repo_kind, repo_opts), 552 io: std.Io, 553 failure: ?[]const u8, 554 ) !Header { 555 if (failure != null) return Header.empty(aa, failure); 556 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .sync_status = nothing }; 557 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 558 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .sync_status = nothing }; 559 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .sync_status = nothing }; 560 return .{ .sync_status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced }; 561 } 562 563 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 564 return .{ .sync_status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing }; 565 } 566 567 pub fn remote() Header { 568 return .{ .sync_status = null }; 569 } 570 571 pub const View = struct { 572 box: wgt.Box(ui.Widget), 573 session: *ui.Session, 574 tab_ids: [2]usize, 575 button_id: ?usize, 576 577 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !Header.View { 578 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 579 errdefer box.deinit(allocator); 580 var tab_ids: [2]usize = undefined; 581 inline for ([_]ui.RoutablePage.EventsView{ .active, .removed }, 0..) |view, i| { 582 const route = ui.RoutablePage.repoEventsRoute(data.identity, view, null, "") orelse return error.RouteTooLong; 583 const count = data.window(view).count; 584 var label_buf: [64]u8 = undefined; 585 const label = try std.fmt.bufPrint(&label_buf, "{s} ({d})", .{ @tagName(view), count }); 586 var tab = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 587 errdefer tab.deinit(allocator); 588 tab.getFocus().mode = .all; 589 const selected = std.meta.activeTag(session.data.current_page) == .repo_events and data.view == view; 590 tab.getFocus().kind = .{ .custom = try ui.inPageTabLink(session, route, selected) }; 591 tab_ids[i] = tab.getFocus().id; 592 try box.children.put(allocator, tab.getFocus().id, .{ .widget = .{ .text_box = tab }, .rect = null, .min_size = .{ .width = label.len + 2, .height = null } }); 593 } 594 595 var button_id: ?usize = null; 596 if (data.header.sync_status != null) { 597 box.getFocus().kind = .{ .custom = "form:/sync" }; 598 var button = try wgt.TextBox.init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 599 errdefer button.deinit(allocator); 600 button.getFocus().mode = .all; 601 button.getFocus().kind = .{ .custom = "submit" }; 602 button_id = button.getFocus().id; 603 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 604 } 605 606 if (data.header.sync_status) |status_text| { 607 var status = try wgt.TextBox.init(allocator, status_text, .{ .border_style = .hidden, .wrap_kind = .none }); 608 errdefer status.deinit(allocator); 609 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .flex = .shrink }); 610 } 611 box.getFocus().child_id = tab_ids[@intFromEnum(data.view)]; 612 return .{ .box = box, .session = session, .tab_ids = tab_ids, .button_id = button_id }; 613 } 614 615 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 616 self.box.deinit(allocator); 617 } 618 619 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 620 self.clearGrid(); 621 for (self.tab_ids) |id| { 622 const child = self.box.children.getPtr(id) orelse continue; 623 const tab = switch (child.widget) { 624 .text_box => |*text_box| text_box, 625 else => continue, 626 }; 627 tab.options.border_style = if (self.box.getFocus().child_id == id) .single else .hidden; 628 } 629 try self.box.build(allocator, constraint, root_focus); 630 } 631 632 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 633 const selected = self.box.getFocus().child_id orelse return; 634 const current = self.box.children.getIndex(selected) orelse return; 635 const count = 2 + @as(usize, @intFromBool(self.button_id != null)); 636 if (inp.moveTab(key, current, count)) |next| { 637 root_focus.setFocus(self.box.children.keys()[next]); 638 return; 639 } 640 switch (key) { 641 .enter => if (self.syncFocused()) try self.requestSync(allocator), 642 .mouse => |mouse| if (self.button_id) |id| if (inp.leftClickOn(root_focus, id, mouse)) try self.requestSync(allocator), 643 else => {}, 644 } 645 } 646 647 fn requestSync(self: *Header.View, allocator: std.mem.Allocator) !void { 648 if (!self.session.is_terminal) return; 649 if (self.session.host_request != null) return; 650 const button_id = self.button_id orelse return; 651 const child = self.box.children.getPtr(button_id) orelse return; 652 const button = switch (child.widget) { 653 .text_box => |*button| button, 654 else => return, 655 }; 656 try button.setContent(allocator, "syncing..."); 657 self.session.host_request = .sync_events; 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, 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, 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