╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
438 // selected file's content window. both are repopulated per selection by 439 // populateDetail. 440 { 441 var detail_outer = blk: { 442 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 443 errdefer frame.deinit(allocator); 444 445 { 446 var nav_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 447 errdefer nav_box.deinit(allocator); 448 try frame.children.put(allocator, nav_box.getFocus().id, .{ .widget = .{ .box = nav_box }, .rect = null, .min_size = null }); 449 } 450 { 451 // the content scroll wrapped in a bordered box; build sets the 452 // border single normally and double when the content is focused 453 // (the content box itself is borderless). 454 var scroll_frame = blk2: { 455 var detail_scroll = blk3: { 456 var detail_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 457 errdefer detail_inner.deinit(allocator); 458 break :blk3 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = detail_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 459 }; 460 errdefer detail_scroll.deinit(allocator); 461 var sf = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .single, .round_corners = true, .direction = .vert }); 462 errdefer sf.deinit(allocator); 463 // the wrapper's selected child is its scroll, so the focus 464 // chain reaches the content box (populateDetail points the 465 // scroll's inner box at it). 466 sf.getFocus().child_id = detail_scroll.getFocus().id; 467 try sf.children.put(allocator, detail_scroll.getFocus().id, .{ .widget = .{ .scroll = detail_scroll }, .rect = null, .min_size = null }); 468 break :blk2 sf; 469 }; 470 errdefer scroll_frame.deinit(allocator); 471 // entering the detail pane lands on the content scroll (focus 472 // recovery descends the frame's selected child). the nav links 473 // above are reached by scrolling to the top, then up. 474 frame.getFocus().child_id = scroll_frame.getFocus().id; 475 // shrink, so the fill scroll yields the nav box's row 476 // instead of consuming the frame's whole height. 477 try frame.children.put(allocator, scroll_frame.getFocus().id, .{ .widget = .{ .box = scroll_frame }, .rect = null, .min_size = null, .flex = .shrink }); 478 } 479 break :blk frame; 480 }; 481 errdefer detail_outer.deinit(allocator); 482 detail_outer.getFocus().mode = .mouse; 483 try box.children.put(allocator, detail_outer.getFocus().id, .{ .widget = .{ .box = detail_outer }, .rect = null, .min_size = .{ .width = detail_min_width, .height = null } }); 484 } 485 486 box.getFocus().child_id = box.children.keys()[list_index]; 487 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 488 489 // focus lives in the split, except that search results start in the 490 // search box so the term can be refined right away. 491 outer.getFocus().child_id = outer.children.keys()[if (data.find != null) header_index else outer.children.count() - 1]; 492 return .{ .box = outer, .data = data, .session = session, .shown_index = null }; 493 } 494 495 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, bottom_label: []const u8, link: []const u8) !void { 496 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .round_corners = true, .wrap_kind = .none, .bottom_label = bottom_label }); 497 errdefer row.deinit(allocator); 498 row.getFocus().mode = .all; 499 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 500 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 501 } 502 503 // the selected file's contents as a single focusable multi-line text box. 504 // it's borderless — the border lives on the surrounding scroll frame, which 505 // doubles when this content is focused. a non-empty `link` makes activating 506 // the box follow it, like addRow. 507 fn addContentBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 508 _ = self; 509 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = null, .wrap_kind = .none }); 510 errdefer tb.deinit(allocator); 511 tb.getFocus().mode = .all; 512 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 513 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 514 } 515 516 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 517 self.box.deinit(allocator); 518 } 519 520 // the split is last, after the sub-header where there is one 521 fn contentIndex(self: *View) usize { 522 return self.box.children.count() - 1; 523 } 524 525 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 526 return &self.box.children.values()[self.contentIndex()].widget.box; 527 } 528 529 fn header(self: *View) ?*ui.widget.SearchHeader { 530 switch (self.box.children.values()[header_index].widget) { 531 .search_header => |*header_view| return header_view, 532 else => return null, 533 } 534 } 535 536 fn headerActive(self: *View) bool { 537 const header_view = self.header() orelse return false; 538 return self.box.getFocus().child_id == header_view.getFocus().id; 539 } 540 541 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 542 return &self.contentBox().children.values()[list_index].widget.scroll; 543 } 544 545 fn listBox(self: *View) *wgt.Box(ui.Widget) { 546 return &self.listScroll().child.box; 547 } 548 549 fn detailOuter(self: *View) *wgt.Box(ui.Widget) { 550 return &self.contentBox().children.values()[detail_index].widget.box; 551 } 552 553 fn navBox(self: *View) *wgt.Box(ui.Widget) { 554 return &self.detailOuter().children.values()[detail_nav_index].widget.box; 555 } 556 557 // the bordered box wrapping the content scroll (border toggles with focus). 558 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 559 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 560 } 561 562 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 563 return &self.detailScrollFrame().children.values()[0].widget.scroll; 564 } 565 566 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 567 return &self.detailScroll().child.box; 568 } 569 570 fn detailActive(self: *View) bool { 571 const content = self.contentBox(); 572 const cid = content.getFocus().child_id orelse return false; 573 return content.children.getIndex(cid) == detail_index; 574 } 575 576 // the selected list row's index, or null when nothing is focused. 577 fn selectedRowIndex(self: *View) ?usize { 578 const lb = self.listBox(); 579 const cid = lb.getFocus().child_id orelse return null; 580 return lb.children.getIndex(cid); 581 } 582 583 // the entry the selected row points at, or null for the ".." row (which 584 // isn't an entry). directory rows return their entry too, though they carry 585 // no contents. 586 fn selectedEntry(self: *View) ?Entry { 587 const idx = self.selectedRowIndex() orelse return null; 588 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 589 if (idx < base) return null; 590 const e = idx - base; 591 if (e >= self.data.entries.len) return null; 592 return self.data.entries[e]; 593 } 594 595 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 596 self.clearGrid(); 597 if (self.data.no_commits) return self.box.build(allocator, constraint, root_focus); 598 599 // swap the detail pane to the selected entry when the selection changes. 600 try self.refreshDetail(allocator); 601 602 // the selected list row shows a border (the focused TextBox upgrades it 603 // to a double border itself); the rest stay borderless. 604 const lb = self.listBox(); 605 for (lb.children.keys(), lb.children.values()) |id, *child| { 606 switch (child.widget) { 607 .text_box => |*tb| ui.widget.markSelected(tb, lb.getFocus().child_id == id), 608 else => {}, 609 } 610 } 611 612 // the content scroll's border is single normally and double when the 613 // content is focused (the content box itself is borderless). 614 const has_content = self.detailInner().children.count() > 0; 615 self.detailScrollFrame().options.border_style = if (!has_content) null else if (self.focusOnContent(root_focus)) .double else .single; 616 617 // cap the list at list_max_width only while the detail pane fits beside 618 // it. the box drops the detail pane when the width can't hold both 619 // minimums, so when it's that narrow we lift the cap and let the list 620 // fill the whole width. 621 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 622 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 623 624 // stretch the detail pane and its bordered scroll frame across the rest 625 // of the width so the pane fills the area rather than shrinking to its 626 // content, like the refs view's columns. the content scroll inside keeps 627 // its natural width: forcing it to the full inner width would make it one 628 // column too wide whenever the vertical scrollbar appears, leaving a 629 // phantom horizontal scroll. (height fills via the scroll clipping to the 630 // viewport.) 631 if (constraint.max_size.width) |w| { 632 const detail_w = if (both_panes_fit) w - list_max_width else w; 633 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 634 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 635 } else { 636 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 637 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 638 } 639 640 // crossing panes only re-selects the content box's child (focusList / 641 // focusDetail); when the window is too narrow to show both, the pane that 642 // was holding focus is dropped here. the framework recovers focus after 643 // the top-level build by re-deriving it down the selected-child chain, so 644 // there's nothing to fix up afterward. 645 try self.box.build(allocator, constraint, root_focus); 646 647 // clamp the list's initial offset (a selected row near the end of the 648 // list) against this build's grids, for the next frame. initRoot's 649 // pre-sizing build is unbounded — its full-content "viewport" would 650 // clamp the offset to zero, so only a bounded build counts. 651 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 652 } 653 654 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 655 const cur = self.selectedRowIndex(); 656 if (std.meta.eql(cur, self.shown_index)) return; 657 try self.populateDetail(allocator); 658 self.shown_index = cur; 659 } 660 661 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 662 const nav_box = self.navBox(); 663 const inner = self.detailInner(); 664 665 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 666 nav_box.children.clearAndFree(allocator); 667 nav_box.getFocus().child_id = null; 668 for (inner.children.values()) |*child| child.widget.deinit(allocator); 669 inner.children.clearAndFree(allocator); 670 inner.getFocus().child_id = null; 671 672 // only files have contents to show; directories and the ".." row leave 673 // the pane empty. 674 var markdown = false; 675 if (self.selectedEntry()) |entry| { 676 if (!entry.is_dir) { 677 // the window links sit above the scroll, so they stay put 678 // while the content scrolls. 679 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 680 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 681 if (!entry.loaded) { 682 // the placeholder carries the file's "a:" link, so enter 683 // (or a click) here loads the file like on its list row. 684 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 685 const link = try fileLink(self.session.page_arena, self.data, path, false, 0); 686 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 687 } else if (entry.is_binary) { 688 try self.addContentBox(allocator, inner, "(binary file)", ""); 689 } else if (isMarkdown(entry.name) and entry.window_start == 0 and !entry.has_more and entry.lines.len <= max_markdown_lines) { 690 const page_arena = self.session.page_arena; 691 const doc = try md.parse(page_arena.allocator(), entry.lines); 692 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 693 var view = try Markdown.View.init(allocator, doc, self.data, path, page_arena); 694 errdefer view.deinit(allocator); 695 try inner.children.put(allocator, view.getFocus().id, .{ .widget = .{ .markdown = view }, .rect = null, .min_size = null }); 696 markdown = true; 697 } else { 698 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 699 try self.addContentBox(allocator, inner, text, ""); 700 } 701 } 702 } 703 // point each box at its first child so focus can descend into it. 704 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 705 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 706 707 // reserve the nav row's height (the shrink scroll frame yields it). 708 self.detailOuter().children.values()[detail_nav_index].min_size = 709 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 710 711 // rendered markdown wraps to the pane's width, so it only scrolls down 712 const sc = self.detailScroll(); 713 sc.options.direction = if (markdown) .vert else .both; 714 715 // reset the scroll to the top for the newly-shown file: directly on the 716 // terminal (the wasm offset), and via a version bump on the web (so the 717 // renderer's scroll id changes and JS drops the preserved position). 718 sc.x = 0; 719 sc.y = 0; 720 sc.getFocus().version +%= 1; 721 } 722 723 // a focusable "next" link above the content. it's an `a:` link to the 724 // selected file at `target_start`, so activating it (the host follows the 725 // link) reloads the page on the next window. 726 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 727 const page_arena = self.session.page_arena; 728 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 729 const route = self.data.filesRoute(path, target_start) orelse return error.RouteTooLong; 730 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 731 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 732 errdefer tb.deinit(allocator); 733 tb.getFocus().mode = .all; 734 tb.getFocus().kind = .{ .custom = link }; 735 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 736 } 737 738 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 739 if (self.data.no_commits) return self.box.input(allocator, key, root_focus); 740 // scrolling crosses the sub header boundary the same way arrows do 741 const direction = inp.vertDirection(key); 742 if (self.headerActive()) { 743 // active means there is one 744 const header_view = self.header() orelse unreachable; 745 if (direction == .down) { 746 root_focus.setFocus(self.contentBox().getFocus().id); 747 return; 748 } 749 if (key == .enter) if (try header_view.submittedText(allocator)) |term| { 750 defer allocator.free(term); 751 return self.submit(term); 752 }; 753 return header_view.input(allocator, key, root_focus); 754 } 755 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 756 if (self.detailActive()) { 757 try self.detailInput(allocator, key, root_focus); 758 } else { 759 try self.listInput(key, root_focus); 760 } 761 } 762 763 // navigate to the typed term's results, pinned to the listing's ref. an 764 // empty term leaves the results for the plain listing. 765 fn submit(self: *View, term: []const u8) !void { 766 if (term.len == 0 and self.data.find == null) return; 767 const route = self.data.filesRoute("", 0) orelse return; 768 try self.session.navigate(route.withFind(term) orelse return); 769 } 770 771 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 772 // up/down (and the scroll wheel) move the selection; page up/down jump a 773 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 774 // directory (or "..") row are turned into navigation by the host 775 // (crossPageLink) before reaching here. 776 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 777 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 778 return; 779 } 780 switch (key) { 781 .enter, .arrow_right => try self.focusDetail(root_focus), 782 else => {}, 783 } 784 } 785 786 // whether focus is on the content box (vs. a "previous"/"next" nav link). 787 fn focusOnContent(self: *View, root_focus: *Focus) bool { 788 const g = root_focus.grandchild_id orelse return false; 789 if (self.markdownView()) |view| return view.owns(g); 790 return self.detailInner().children.contains(g); 791 } 792 793 // the rendered markdown in the detail pane, if that's what it shows 794 fn markdownView(self: *View) ?*Markdown.View { 795 const inner = self.detailInner(); 796 if (inner.children.count() == 0) return null; 797 return switch (inner.children.values()[0].widget) { 798 .markdown => |*view| view, 799 else => null, 800 }; 801 } 802 803 fn detailInput(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 804 const sc = self.detailScroll(); 805 const on_content = self.focusOnContent(root_focus); 806 // the "next" link sits above the content scroll. on the link, left returns 807 // to the file list and down drops into the content. in the content, the 808 // terminal scrolls by offset and reaching the top then pressing up crosses 809 // back up to the link; on the web vertical scrolling is the browser's job 810 // so up just crosses to the link. 811 switch (key) { 812 .arrow_left => { 813 if (on_content) { 814 if (sc.x > 0) { 815 sc.x -= 1; 816 self.detailScroll().clampToContent(); 817 } else try self.focusList(root_focus); 818 } else if (!self.moveNav(root_focus, -1)) { 819 try self.focusList(root_focus); 820 } 821 }, 822 .arrow_right => { 823 if (on_content) { 824 sc.x += 1; 825 self.detailScroll().clampToContent(); 826 } else _ = self.moveNav(root_focus, 1); 827 }, 828 .arrow_up => { 829 if (on_content) { 830 if (self.session.is_terminal and sc.y > 0) { 831 sc.y -= 1; 832 self.detailScroll().clampToContent(); 833 } else self.focusNav(root_focus); // cross up to the links 834 } 835 }, 836 .arrow_down => { 837 if (on_content) { 838 if (self.session.is_terminal) { 839 sc.y += 1; 840 self.detailScroll().clampToContent(); 841 } 842 } else self.focusContent(root_focus); // links -> content 843 }, 844 .page_up => if (on_content and self.session.is_terminal) { 845 sc.y -= 10; 846 self.detailScroll().clampToContent(); 847 }, 848 .page_down => if (on_content and self.session.is_terminal) { 849 sc.y += 10; 850 self.detailScroll().clampToContent(); 851 }, 852 .home => if (on_content and self.session.is_terminal) { 853 sc.y = 0; 854 self.detailScroll().clampToContent(); 855 }, 856 .end => if (on_content and self.session.is_terminal) { 857 sc.y = std.math.maxInt(isize); 858 self.detailScroll().clampToContent(); 859 }, 860 .mouse => |mouse| switch (mouse.action) { 861 .scroll => |dir| if (on_content and self.session.is_terminal) { 862 sc.y += if (dir == .up) -5 else 5; 863 self.detailScroll().clampToContent(); 864 }, 865 else => {}, 866 }, 867 // step between the rendered markdown's links; the web leaves tab to the browser 868 .tab, .back_tab => if (on_content and self.session.is_terminal) if (self.markdownView()) |view| { 869 try view.stepLink(allocator, root_focus, sc, key == .tab); 870 }, 871 else => {}, 872 } 873 } 874 875 // focus the first (left-most) nav link; a no-op when there are none, leaving 876 // focus on the content. 877 fn focusNav(self: *View, root_focus: *Focus) void { 878 const nav_box = self.navBox(); 879 if (nav_box.children.count() == 0) return; 880 root_focus.setFocus(nav_box.children.keys()[0]); 881 } 882 883 fn focusContent(self: *View, root_focus: *Focus) void { 884 if (self.markdownView()) |view| return root_focus.setFocus(view.body.id); 885 const inner = self.detailInner(); 886 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 887 root_focus.setFocus(id); 888 } 889 890 // move focus `delta` rows within the nav box; returns false (without moving) 891 // when that would step off either end. 892 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 893 const nav_box = self.navBox(); 894 const keys = nav_box.children.keys(); 895 const cur_id = nav_box.getFocus().child_id orelse return false; 896 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 897 const target = cur + delta; 898 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 899 root_focus.setFocus(keys[@intCast(target)]); 900 return true; 901 } 902 903 // enter the detail pane, landing on the "next" link when there is one (so 904 // right-arrow reaches it), else on the content. a directory or ".." row has 905 // neither, so there's nothing to enter. selecting the frame's child (then 906 // focusing the frame) also handles the too-narrow case where the pane isn't 907 // laid out yet — focus lands inside it after the next build. 908 fn focusDetail(self: *View, root_focus: *Focus) !void { 909 const frame = self.detailOuter(); 910 if (self.navBox().children.count() > 0) { 911 frame.getFocus().child_id = self.navBox().getFocus().id; 912 } else if (self.detailInner().children.count() > 0) { 913 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 914 } else return; 915 root_focus.setFocus(frame.getFocus().id); 916 } 917 918 // return to the list. 919 fn focusList(self: *View, root_focus: *Focus) !void { 920 root_focus.setFocus(self.listScroll().getFocus().id); 921 } 922 923 pub fn clearGrid(self: *View) void { 924 self.box.clearGrid(); 925 } 926 927 pub fn getGrid(self: View) ?Grid { 928 return self.box.getGrid(); 929 } 930 931 pub fn getFocus(self: *View) *Focus { 932 return self.box.getFocus(); 933 } 934 935 fn contentAtTop(self: *View) bool { 936 if (self.detailActive()) { 937 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 938 return self.navBox().children.count() == 0 and 939 (!self.session.is_terminal or self.detailScroll().y == 0); 940 } 941 const lb = self.listBox(); 942 const cid = lb.getFocus().child_id orelse return true; 943 return lb.children.getIndex(cid) == 0; 944 } 945 946 // only the header's own row sits directly below the repository header. 947 pub fn atTop(self: *View) bool { 948 if (self.data.no_commits) return true; 949 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 950 return self.headerActive() or (!focusable and self.contentAtTop()); 951 } 952 953 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 954 const header_view = self.header() orelse return false; 955 return header_view.focusHeader(root_focus); 956 } 957 }; 958 959 // join `lines` into one string, each prefixed with its 1-based file line number 960 // (right-aligned in a column wide enough for the last number, then a space), 961 // like the diffs view. `start` is the window's 0-based first line, so numbers 962 // continue across paginated windows. 963 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 964 if (lines.len == 0) return ""; 965 const width = std.fmt.count("{d}", .{start + lines.len}); 966 const indent = try arena.alloc(u8, width); 967 @memset(indent, ' '); 968 969 var out: std.ArrayList(u8) = .empty; 970 for (lines, 0..) |line, i| { 971 if (i != 0) try out.append(arena, '\n'); 972 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 973 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 974 try out.appendSlice(arena, num_str); 975 try out.append(arena, ' '); 976 try out.appendSlice(arena, line); 977 } 978 return out.toOwnedSlice(arena); 979 } 980 981 // the index of the "README"/"README.md" file entry (case-insensitive), or null 982 // if none. used to pick the default selection at the repo root. 983 fn readmeIndex(entries: []const Entry) ?usize { 984 for (entries, 0..) |entry, i| { 985 if (!entry.is_dir and isReadme(entry.name)) return i; 986 } 987 return null; 988 } 989 990 fn isMarkdown(name: []const u8) bool { 991 const extension = std.fs.path.extension(name); 992 return std.ascii.eqlIgnoreCase(extension, ".md") or std.ascii.eqlIgnoreCase(extension, ".markdown"); 993 } 994 995 fn isReadme(name: []const u8) bool { 996 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 997 } 998 999 // the index of the entry the route named (data.selected_file), or null if none. 1000 fn selectedFileIndex(data: *const Self) ?usize { 1001 const name = data.selected_file orelse return null; 1002 for (data.entries, 0..) |entry, i| { 1003 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 1004 } 1005 return null; 1006 } 1007 1008 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 1009 // following it navigates to that directory's listing. 1010 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 1011 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 1012 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 1013 } 1014 1015 // a link to the file's route. a preloaded file gets an "ai:" link 1016 // (crossPageLink ignores it, so a click selects the row in place and shows its 1017 // contents); an unloaded file gets an "a:" link, so activating it reloads the 1018 // page with that file selected and its content read. 1019 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 1020 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 1021 const prefix = if (loaded) "ai:" else "a:"; 1022 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 1023 } 1024 1025 // the url `line` value for a content window: its first line's 1-based number, 1026 // except the first window, which serializes as unset (0). 1027 fn lineNumber(window_start: usize) usize { 1028 return if (window_start == 0) 0 else window_start + 1; 1029 } 1030 1031 // `dir`/`name`, or just `name` at the root. 1032 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 1033 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 1034 } 1035 1036 // `dir` with its last segment removed ("" when `dir` has a single segment). 1037 fn parentDir(dir: []const u8) []const u8 { 1038 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 1039 return dir[0..slash]; 1040 } 1041 1042 // a search result's bottom label: the directory it sits in, empty at the root. 1043 // a directory too wide for the list column is clipped from the left. 1044 fn resultDirLabel(aa: std.mem.Allocator, path: []const u8) ![]const u8 { 1045 const slash = std.mem.lastIndexOfScalar(u8, path, '/') orelse return ""; 1046 const dir = path[0..slash]; 1047 // the row's border and the label's padding take two columns each 1048 const max_width = View.list_max_width - 4; 1049 var width = try xitui.width.displayWidth(dir); 1050 if (width <= max_width) return std.fmt.allocPrint(aa, " {s} ", .{dir}); 1051 1052 const ellipsis = ".."; 1053 var iter = (try std.unicode.Utf8View.init(dir)).iterator(); 1054 var start: usize = 0; 1055 while (width > max_width - ellipsis.len) { 1056 const slice = iter.nextCodepointSlice() orelse break; 1057 width -= xitui.width.cellWidth(try std.unicode.utf8Decode(slice)); 1058 start += slice.len; 1059 } 1060 return std.fmt.allocPrint(aa, " " ++ ellipsis ++ "{s} ", .{dir[start..]}); 1061 } 1062