╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Events.zig ───────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
312 try box.children.put(allocator, link.getFocus().id, .{ .widget = .{ .text_box = link }, .rect = null, .min_size = null }); 313 } 314 315 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 316 self.box.deinit(allocator); 317 } 318 319 fn header(self: *View) *Header.View { 320 return &self.box.children.values()[header_index].widget.repo_events_header; 321 } 322 323 fn content(self: *View) *wgt.Box(ui.Widget) { 324 return &self.box.children.values()[content_index].widget.box; 325 } 326 327 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 328 return &self.content().children.values()[list_index].widget.scroll; 329 } 330 331 fn listBox(self: *View) *wgt.Box(ui.Widget) { 332 return &self.listScroll().child.box; 333 } 334 335 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 336 return &self.content().children.values()[detail_index].widget.scroll; 337 } 338 339 fn detailBox(self: *View) *wgt.Box(ui.Widget) { 340 return &self.detailScroll().child.box; 341 } 342 343 fn detailActive(self: *View) bool { 344 return self.content().getFocus().child_id == self.detailScroll().getFocus().id; 345 } 346 347 fn headerActive(self: *View) bool { 348 return self.box.getFocus().child_id == self.header().getFocus().id; 349 } 350 351 fn selectedEventIndex(self: *View) ?usize { 352 const id = self.listBox().getFocus().child_id orelse return null; 353 const index = self.listBox().children.getIndex(id) orelse return null; 354 return if (index < self.data.events.len) index else null; 355 } 356 357 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 358 self.clearGrid(); 359 try self.refreshDetail(allocator); 360 361 if (root_focus.grandchild_id) |id| if (self.box.getFocus().children.contains(id)) { 362 if (self.selectedEventIndex()) |index| { 363 const event = self.data.events[index]; 364 if (ui.RoutablePage.repoEventsRoute(self.data.identity, event.kind, event.id)) |route| 365 self.session.data.current_page = route; 366 } 367 }; 368 369 const list = self.listBox(); 370 for (list.children.keys(), list.children.values()) |id, *child| switch (child.widget) { 371 .text_box => |*text_box| text_box.options.border_style = if (list.getFocus().child_id == id) .single else .hidden, 372 else => {}, 373 }; 374 375 const both_fit = if (constraint.max_size.width) |width| width >= list_max_width + detail_min_width else true; 376 self.content().children.values()[list_index].max_size = if (both_fit) .{ .width = list_max_width, .height = null } else null; 377 const detail_width = if (constraint.max_size.width) |width| if (both_fit) width - list_max_width else width else detail_min_width; 378 self.content().children.values()[detail_index].min_size = .{ .width = detail_width, .height = null }; 379 for (self.detailBox().children.values()) |*child| child.max_size = .{ .width = detail_width -| 2, .height = null }; 380 381 try self.box.build(allocator, constraint, root_focus); 382 } 383 384 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 385 const index = self.selectedEventIndex() orelse return; 386 if (self.detailed_index) |old| if (old == index) return; 387 const box = self.detailBox(); 388 for (box.children.values()) |*child| child.widget.deinit(allocator); 389 box.children.clearAndFree(allocator); 390 box.getFocus().child_id = null; 391 392 const item = self.data.events[index]; 393 try self.addViewEvent(allocator, box, item); 394 try addField(allocator, box, @tagName(item.kind), " kind "); 395 try self.addAuthor(allocator, box, item.author); 396 if (item.deleted) try addField(allocator, box, "(deleted)", " data "); 397 398 if (box.children.count() > 0) box.getFocus().child_id = box.children.keys()[0]; 399 self.detailScroll().x = 0; 400 self.detailScroll().y = 0; 401 self.detailScroll().getFocus().version +%= 1; 402 self.detailed_index = index; 403 } 404 405 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 406 if (self.headerActive()) { 407 if (key == .arrow_down) root_focus.setFocus(self.content().getFocus().id) else try self.header().input(allocator, key, root_focus); 408 return; 409 } 410 if (key == .arrow_up and self.contentAtTop() and self.header().focusButton(root_focus)) return; 411 if (self.detailActive()) { 412 if (key == .arrow_left) { 413 root_focus.setFocus(self.listScroll().getFocus().id); 414 } else if (inp.rowDelta(key, @intCast(self.detailBox().children.count()))) |delta| { 415 ui.moveRowFocus(self.detailBox(), self.detailScroll(), root_focus, delta); 416 } 417 } else if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 418 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 419 } else switch (key) { 420 .arrow_right => if (self.detailBox().children.count() > 0) root_focus.setFocus(self.detailScroll().getFocus().id), 421 .enter => if (self.selectedEventIndex() != null) 422 root_focus.setFocus(self.detailScroll().getFocus().id) 423 else if (self.data.next) |next| if (ui.RoutablePage.repoEventsRoute(self.data.identity, next.kind, next.id)) |route| try self.session.navigate(route), 424 else => {}, 425 } 426 } 427 428 fn contentAtTop(self: *View) bool { 429 const box = if (self.detailActive()) self.detailBox() else self.listBox(); 430 const scroll = if (self.detailActive()) self.detailScroll() else self.listScroll(); 431 const id = box.getFocus().child_id orelse return true; 432 return box.children.getIndex(id) == 0 and scroll.y == 0; 433 } 434 435 pub fn getSelectedIndex(self: *View) ?usize { 436 if (self.headerActive()) return 0; 437 return if (self.header().isFocusable() or !self.contentAtTop()) 1 else 0; 438 } 439 440 pub fn clearGrid(self: *View) void { 441 self.box.clearGrid(); 442 } 443 444 pub fn getGrid(self: View) ?Grid { 445 return self.box.getGrid(); 446 } 447 448 pub fn getFocus(self: *View) *Focus { 449 return self.box.getFocus(); 450 } 451 }; 452 453 pub const Header = struct { 454 status: []const u8, 455 is_local: bool, 456 457 const nothing = "nothing to sync"; 458 const unsynced = "sync needed!"; 459 460 const Progress = struct { 461 message: *std.Io.Writer.Allocating, 462 463 pub fn run(self: @This(), _: std.Io, event: rp.ProgressEvent) !void { 464 switch (event) { 465 .text => |value| { 466 self.message.clearRetainingCapacity(); 467 try self.message.writer.writeAll(value); 468 }, 469 else => {}, 470 } 471 } 472 }; 473 474 pub fn local( 475 comptime repo_kind: rp.RepoKind, 476 comptime repo_opts: rp.RepoOpts(repo_kind), 477 aa: std.mem.Allocator, 478 repo: *rp.Repo(repo_kind, repo_opts), 479 io: std.Io, 480 failure: ?[]const u8, 481 ) !Header { 482 if (failure != null) return Header.empty(aa, failure); 483 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, aa)) orelse return .{ .status = nothing, .is_local = true }; 484 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 485 const local_oid = (try repo.readRef(io, evt.events_ref)) orelse return .{ .status = nothing, .is_local = true }; 486 const remote_oid = (try repo.readRef(io, remote_ref)) orelse return .{ .status = nothing, .is_local = true }; 487 return .{ .status = if (std.mem.eql(u8, &local_oid, &remote_oid)) nothing else unsynced, .is_local = true }; 488 } 489 490 pub fn empty(aa: std.mem.Allocator, failure: ?[]const u8) !Header { 491 return .{ .status = if (failure) |name| try std.fmt.allocPrint(aa, "error: {s}", .{name}) else nothing, .is_local = true }; 492 } 493 494 pub fn remote(aa: std.mem.Allocator, count: usize) !Header { 495 return .{ .status = try std.fmt.allocPrint(aa, "{d} events found", .{count}), .is_local = false }; 496 } 497 498 fn withCount(self: Header, aa: std.mem.Allocator, count: usize) !Header { 499 return .{ .status = try std.fmt.allocPrint(aa, "{d} events found - {s}", .{ count, self.status }), .is_local = self.is_local }; 500 } 501 502 pub const View = struct { 503 box: wgt.Box(ui.Widget), 504 session: *ui.Session, 505 button_id: ?usize, 506 507 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 508 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 509 errdefer box.deinit(allocator); 510 var button_id: ?usize = null; 511 if (data.is_local) { 512 box.getFocus().kind = .{ .custom = "form:/sync" }; 513 var button = try wgt.TextBox(ui.Widget).init(allocator, "sync", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 514 errdefer button.deinit(allocator); 515 button.getFocus().focusable = true; 516 button.getFocus().kind = .{ .custom = "submit" }; 517 button_id = button.getFocus().id; 518 box.getFocus().child_id = button_id; 519 try box.children.put(allocator, button.getFocus().id, .{ .widget = .{ .text_box = button }, .rect = null, .min_size = null }); 520 } 521 522 var status = try wgt.TextBox(ui.Widget).init(allocator, data.status, .{ .border_style = .hidden, .wrap_kind = .none }); 523 errdefer status.deinit(allocator); 524 try box.children.put(allocator, status.getFocus().id, .{ .widget = .{ .text_box = status }, .rect = null, .min_size = null, .shrink = true }); 525 return .{ .box = box, .session = session, .button_id = button_id }; 526 } 527 528 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 529 self.box.deinit(allocator); 530 } 531 532 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 533 self.clearGrid(); 534 try self.box.build(allocator, constraint, root_focus); 535 } 536 537 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 538 const button_id = self.button_id orelse return; 539 if (!self.session.is_terminal) return; 540 if (self.session.host_request != null) return; 541 switch (key) { 542 .enter => self.requestSync(), 543 .mouse => |mouse| if (inp.leftClickOn(root_focus, button_id, mouse)) self.requestSync(), 544 else => {}, 545 } 546 _ = allocator; 547 } 548 549 fn requestSync(self: *Header.View) void { 550 self.session.host_request = .sync_events; 551 const button_id = self.button_id orelse return; 552 const child = self.box.children.getPtr(button_id) orelse return; 553 const button = switch (child.widget) { 554 .text_box => |*button| button, 555 else => return, 556 }; 557 button.content = "syncing..."; 558 for (button.box.children.values()) |*line| switch (line.widget) { 559 .text => |*text| text.content = "syncing...", 560 else => {}, 561 }; 562 } 563 564 pub fn focusButton(self: *Header.View, root_focus: *Focus) bool { 565 const button_id = self.button_id orelse return false; 566 root_focus.setFocus(button_id); 567 return true; 568 } 569 570 pub fn isFocusable(self: *Header.View) bool { 571 return self.button_id != null; 572 } 573 574 pub fn clearGrid(self: *Header.View) void { 575 self.box.clearGrid(); 576 } 577 578 pub fn getGrid(self: Header.View) ?Grid { 579 return self.box.getGrid(); 580 } 581 582 pub fn getFocus(self: *Header.View) *Focus { 583 return self.box.getFocus(); 584 } 585 }; 586 }; 587 588 pub fn performSync(allocator: std.mem.Allocator, session: *ui.Session) !void { 589 if (comptime builtin.target.cpu.arch == .wasm32) return; 590 const io = session.io orelse return; 591 const source = session.local orelse return; 592 defer session.refresh_requested = true; 593 const failure = sync(io, allocator, source) catch |err| { 594 session.data.sync_failure = @errorName(err); 595 return; 596 }; 597 if (failure) |message| { 598 defer allocator.free(message); 599 session.data.sync_failure = try session.arena.allocator().dupe(u8, message); 600 return; 601 } 602 session.data.sync_failure = null; 603 switch (session.data.current_page) { 604 .repo_events => |route| { 605 if (ui.RoutablePage.repoEventsRoute(route.name.slice(), null, "")) |first| 606 session.data.current_page = first; 607 }, 608 else => {}, 609 } 610 } 611 612 fn remoteName( 613 comptime repo_kind: rp.RepoKind, 614 comptime repo_opts: rp.RepoOpts(repo_kind), 615 repo: *rp.Repo(repo_kind, repo_opts), 616 io: std.Io, 617 allocator: std.mem.Allocator, 618 ) !?[]u8 { 619 var remotes = try repo.listRemotes(io, allocator); 620 defer remotes.deinit(); 621 const names = remotes.sections.keys(); 622 if (names.len == 0) return null; 623 for (names) |name| { 624 const remote_ref: rf.Ref = .{ .kind = .{ .remote = name }, .name = evt.events_ref.name }; 625 if (try repo.readRef(io, remote_ref) != null) return try allocator.dupe(u8, name); 626 } 627 return try allocator.dupe(u8, names[0]); 628 } 629 630 fn isLoopbackSshUrl(url: []const u8) bool { 631 const uri = std.Uri.parse(url) catch return false; 632 const host = switch (uri.host orelse return false) { 633 .raw => |value| value, 634 .percent_encoded => |value| value, 635 }; 636 return std.mem.eql(u8, host, "localhost") or 637 std.mem.eql(u8, host, "127.0.0.1") or 638 std.mem.eql(u8, host, "::1") or 639 std.mem.eql(u8, host, "[::1]"); 640 } 641 642 pub fn sync(io: std.Io, allocator: std.mem.Allocator, source: ui.RepoSource) !?[]u8 { 643 var message: std.Io.Writer.Allocating = .init(allocator); 644 defer message.deinit(); 645 646 switch (source.repo_kind) { 647 inline else => |repo_kind| { 648 var any_repo = try rp.AnyRepo(repo_kind, .{ .ProgressCtx = Header.Progress }).open(io, allocator, .{ .path = source.path }); 649 defer any_repo.deinit(io, allocator); 650 switch (any_repo) { 651 inline else => |*repo| { 652 const repo_opts = repo.self_repo_opts; 653 const remote_name = (try remoteName(repo_kind, repo_opts, repo, io, allocator)) orelse return error.RemoteNotFound; 654 defer allocator.free(remote_name); 655 656 var config = try repo.listConfig(io, allocator); 657 defer config.deinit(); 658 const configured_ssh = if (config.sections.get("core")) |core| core.get("sshcommand") else null; 659 const ssh = if (configured_ssh) |command| if (command.len > 0) command else "ssh" else "ssh"; 660 const remote_section = try std.fmt.allocPrint(allocator, "remote.{s}", .{remote_name}); 661 defer allocator.free(remote_section); 662 const remote_config = config.sections.get(remote_section) orelse return error.RemoteNotFound; 663 const fetch_url = remote_config.get("url") orelse return error.UrlNotFound; 664 const push_url = remote_config.get("pushurl") orelse fetch_url; 665 const loopback_opts = " -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null"; 666 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 "" }); 667 defer allocator.free(fetch_ssh_command); 668 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 "" }); 669 defer allocator.free(push_ssh_command); 670 const fetch_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = fetch_ssh_command } } }; 671 const push_transport_opts: xit.net.Opts(repo_opts.ProgressCtx) = .{ .progress_ctx = .{ .message = &message }, .wire = .{ .ssh = .{ .command = push_ssh_command } } }; 672 673 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 }); 674 defer allocator.free(fetch_refspec); 675 var fetch_opts = fetch_transport_opts; 676 fetch_opts.refspecs = &.{fetch_refspec}; 677 message.clearRetainingCapacity(); 678 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 679 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 680 return err; 681 }; 682 683 const remote_ref: rf.Ref = .{ .kind = .{ .remote = remote_name }, .name = evt.events_ref.name }; 684 const local_oid = try repo.readRef(io, evt.events_ref); 685 const remote_oid = try repo.readRef(io, remote_ref); 686 if (local_oid == null and remote_oid == null) { 687 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 688 return null; 689 } 690 if (remote_oid != null) try evt.mergeEvents(repo_kind, repo_opts, io, allocator, repo, remote_ref); 691 try evt.consume(repo_kind, repo_opts, io, allocator, repo, evt.events_ref, &.{}); 692 693 const push_refspec = try std.fmt.allocPrint(allocator, "refs/heads/{s}:refs/heads/{s}", .{ evt.events_ref.name, evt.events_ref.name }); 694 defer allocator.free(push_refspec); 695 message.clearRetainingCapacity(); 696 repo.push(io, allocator, remote_name, push_refspec, false, push_transport_opts) catch |err| { 697 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 698 return err; 699 }; 700 message.clearRetainingCapacity(); 701 repo.fetch(io, allocator, remote_name, fetch_opts) catch |err| { 702 if (err == error.ServerReportedError and message.written().len != 0) return try allocator.dupe(u8, message.written()); 703 return err; 704 }; 705 }, 706 } 707 }, 708 } 709 return null; 710 } 711