╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
483 // stays at the directory with no offset. the path is joined in a stack 484 // buffer since build runs every frame on the web. 485 if (root_focus.grandchild_id) |g| { 486 if (self.box.getFocus().children.contains(g)) { 487 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 488 var sel_path = self.data.dir; 489 var sel_after: usize = 0; 490 if (self.selectedEntry()) |entry| { 491 if (!entry.is_dir) { 492 sel_path = if (self.data.dir.len == 0) 493 entry.name 494 else 495 std.fmt.bufPrint(&buf, "{s}/{s}", .{ self.data.dir, entry.name }) catch self.data.dir; 496 sel_after = entry.window_start; 497 } 498 } 499 if (ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, sel_path, sel_after)) |route| 500 self.session.data.current_page = route; 501 } 502 } 503 504 // the selected list row shows a border (the focused TextBox upgrades it 505 // to a double border itself); the rest stay borderless. 506 const lb = self.listBox(); 507 for (lb.children.keys(), lb.children.values()) |id, *child| { 508 switch (child.widget) { 509 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 510 else => {}, 511 } 512 } 513 514 // the content scroll's border is single normally and double when the 515 // content is focused (the content box itself is borderless). 516 const has_content = self.detailInner().children.count() > 0; 517 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 518 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 519 520 // same for the "next" link above the content. 521 const nav_box = self.navBox(); 522 for (nav_box.children.keys(), nav_box.children.values()) |id, *child| { 523 switch (child.widget) { 524 .text_box => |*tb| tb.options.border_style = if (nav_box.getFocus().child_id == id) .single else .hidden, 525 else => {}, 526 } 527 } 528 529 // cap the list at list_max_width only while the detail pane fits beside 530 // it. the box drops the detail pane when the width can't hold both 531 // minimums, so when it's that narrow we lift the cap and let the list 532 // fill the whole width. 533 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 534 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 535 536 // stretch the detail pane and its bordered scroll frame across the rest 537 // of the width so the pane fills the area rather than shrinking to its 538 // content, like the refs view's columns. the content scroll inside keeps 539 // its natural width: forcing it to the full inner width would make it one 540 // column too wide whenever the vertical scrollbar appears, leaving a 541 // phantom horizontal scroll. (height fills via the scroll clipping to the 542 // viewport.) 543 if (constraint.max_size.width) |w| { 544 const detail_w = if (both_panes_fit) w - list_max_width else w; 545 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 546 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 547 } else { 548 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 549 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 550 } 551 552 // crossing panes only re-selects the content box's child (focusList / 553 // focusDetail); when the window is too narrow to show both, the pane that 554 // was holding focus is dropped here. the framework recovers focus after 555 // the top-level build by re-deriving it down the selected-child chain, so 556 // there's nothing to fix up afterward. 557 try self.box.build(allocator, constraint, root_focus); 558 } 559 560 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 561 const cur = self.selectedRowIndex(); 562 if (std.meta.eql(cur, self.shown_index)) return; 563 try self.populateDetail(allocator); 564 self.shown_index = cur; 565 } 566 567 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 568 const nav_box = self.navBox(); 569 const inner = self.detailInner(); 570 571 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 572 nav_box.children.clearAndFree(allocator); 573 nav_box.getFocus().child_id = null; 574 for (inner.children.values()) |*child| child.widget.deinit(allocator); 575 inner.children.clearAndFree(allocator); 576 inner.getFocus().child_id = null; 577 578 // only files have contents to show; directories and the ".." row leave 579 // the pane empty. 580 if (self.selectedEntry()) |entry| { 581 if (!entry.is_dir) { 582 // the "next" window link sits above the scroll, so it stays put 583 // while the content scrolls. 584 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, entry.window_start + file_page); 585 if (entry.is_binary) { 586 try self.addContentBox(allocator, inner, "(binary file)"); 587 } else { 588 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 589 try self.addContentBox(allocator, inner, text); 590 } 591 } 592 } 593 // point each box at its first child so focus can descend into it. 594 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 595 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 596 597 // reset the scroll to the top for the newly-shown file: directly on the 598 // terminal (the wasm offset), and via a version bump on the web (so the 599 // renderer's scroll id changes and JS drops the preserved position). 600 const sc = self.detailScroll(); 601 sc.x = 0; 602 sc.y = 0; 603 sc.getFocus().version +%= 1; 604 } 605 606 // a focusable "next" link above the content. it's an `a:` link to the 607 // selected file at `target_after`, so activating it (the host follows the 608 // link) reloads the page on the next window. 609 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_after: usize) !void { 610 const page_arena = self.session.page_arena; 611 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 612 const route = ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, path, target_after) orelse return error.RouteTooLong; 613 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 614 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 615 errdefer tb.deinit(allocator); 616 tb.getFocus().focusable = true; 617 tb.getFocus().kind = .{ .custom = link }; 618 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 619 } 620 621 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 622 _ = allocator; 623 if (self.detailActive()) { 624 try self.detailInput(key, root_focus); 625 } else { 626 try self.listInput(key, root_focus); 627 } 628 } 629 630 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 631 // up/down (and the scroll wheel) move the selection; page up/down jump a 632 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 633 // directory (or "..") row are turned into navigation by the host 634 // (crossPageLink) before reaching here. 635 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 636 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 637 return; 638 } 639 switch (key) { 640 .enter, .arrow_right => try self.focusDetail(root_focus), 641 else => {}, 642 } 643 } 644 645 // whether focus is on the content box (vs. a "previous"/"next" nav link). 646 fn focusOnContent(self: *View, root_focus: *Focus) bool { 647 const g = root_focus.grandchild_id orelse return false; 648 return self.detailInner().children.contains(g); 649 } 650 651 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 652 const sc = self.detailScroll(); 653 const on_content = self.focusOnContent(root_focus); 654 // the "next" link sits above the content scroll. on the link, left returns 655 // to the file list and down drops into the content. in the content, the 656 // terminal scrolls by offset and reaching the top then pressing up crosses 657 // back up to the link; on the web vertical scrolling is the browser's job 658 // so up just crosses to the link. 659 switch (key) { 660 .arrow_left => { 661 if (on_content) { 662 if (sc.x > 0) { 663 sc.x -= 1; 664 ui.clampScroll(self.detailScroll()); 665 } else try self.focusList(root_focus); 666 } else if (!self.moveNav(root_focus, -1)) { 667 try self.focusList(root_focus); 668 } 669 }, 670 .arrow_right => { 671 if (on_content) { 672 sc.x += 1; 673 ui.clampScroll(self.detailScroll()); 674 } else _ = self.moveNav(root_focus, 1); 675 }, 676 .arrow_up => { 677 if (on_content) { 678 if (self.session.is_terminal and sc.y > 0) { 679 sc.y -= 1; 680 ui.clampScroll(self.detailScroll()); 681 } else self.focusNav(root_focus); // cross up to the links 682 } 683 }, 684 .arrow_down => { 685 if (on_content) { 686 if (self.session.is_terminal) { 687 sc.y += 1; 688 ui.clampScroll(self.detailScroll()); 689 } 690 } else self.focusContent(root_focus); // links -> content 691 }, 692 .page_up => if (on_content and self.session.is_terminal) { 693 sc.y -= 10; 694 ui.clampScroll(self.detailScroll()); 695 }, 696 .page_down => if (on_content and self.session.is_terminal) { 697 sc.y += 10; 698 ui.clampScroll(self.detailScroll()); 699 }, 700 .home => if (on_content and self.session.is_terminal) { 701 sc.y = 0; 702 ui.clampScroll(self.detailScroll()); 703 }, 704 .end => if (on_content and self.session.is_terminal) { 705 sc.y = std.math.maxInt(isize); 706 ui.clampScroll(self.detailScroll()); 707 }, 708 .mouse => |mouse| switch (mouse.action) { 709 .scroll => |dir| if (on_content and self.session.is_terminal) { 710 sc.y += if (dir == .up) -5 else 5; 711 ui.clampScroll(self.detailScroll()); 712 }, 713 else => {}, 714 }, 715 else => {}, 716 } 717 } 718 719 // focus the first (left-most) nav link; a no-op when there are none, leaving 720 // focus on the content. 721 fn focusNav(self: *View, root_focus: *Focus) void { 722 const nav_box = self.navBox(); 723 if (nav_box.children.count() == 0) return; 724 root_focus.setFocus(nav_box.children.keys()[0]); 725 } 726 727 fn focusContent(self: *View, root_focus: *Focus) void { 728 const inner = self.detailInner(); 729 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 730 root_focus.setFocus(id); 731 } 732 733 // move focus `delta` rows within the nav box; returns false (without moving) 734 // when that would step off either end. 735 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 736 const nav_box = self.navBox(); 737 const keys = nav_box.children.keys(); 738 const cur_id = nav_box.getFocus().child_id orelse return false; 739 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 740 const target = cur + delta; 741 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 742 root_focus.setFocus(keys[@intCast(target)]); 743 return true; 744 } 745 746 // enter the detail pane, landing on the "next" link when there is one (so 747 // right-arrow reaches it), else on the content. a directory or ".." row has 748 // neither, so there's nothing to enter. selecting the frame's child (then 749 // focusing the frame) also handles the too-narrow case where the pane isn't 750 // laid out yet — focus lands inside it after the next build. 751 fn focusDetail(self: *View, root_focus: *Focus) !void { 752 const frame = self.detailOuter(); 753 if (self.navBox().children.count() > 0) { 754 frame.getFocus().child_id = self.navBox().getFocus().id; 755 } else if (self.detailInner().children.count() > 0) { 756 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 757 } else return; 758 root_focus.setFocus(frame.getFocus().id); 759 } 760 761 // return to the list. 762 fn focusList(self: *View, root_focus: *Focus) !void { 763 root_focus.setFocus(self.listScroll().getFocus().id); 764 } 765 766 pub fn clearGrid(self: *View) void { 767 self.box.clearGrid(); 768 } 769 770 pub fn getGrid(self: View) ?Grid { 771 return self.box.getGrid(); 772 } 773 774 pub fn getFocus(self: *View) *Focus { 775 return self.box.getFocus(); 776 } 777 778 // for the parent's "scroll up at the top jumps to the header" check. inside 779 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 780 // focus, report its selection directly so up-arrow at the top row ascends. 781 pub fn getSelectedIndex(self: *View) ?usize { 782 if (self.detailActive()) return 1; 783 const lb = self.listBox(); 784 const cid = lb.getFocus().child_id orelse return null; 785 return lb.children.getIndex(cid); 786 } 787 }; 788 789 // join `lines` into one string, each prefixed with its 1-based file line number 790 // (right-aligned in a column wide enough for the last number, then a space), 791 // like the diffs view. `start` is the window's 0-based first line, so numbers 792 // continue across paginated windows. 793 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 794 if (lines.len == 0) return ""; 795 const width = std.fmt.count("{d}", .{start + lines.len}); 796 const indent = try arena.alloc(u8, width); 797 @memset(indent, ' '); 798 799 var out: std.ArrayList(u8) = .empty; 800 for (lines, 0..) |line, i| { 801 if (i != 0) try out.append(arena, '\n'); 802 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 803 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 804 try out.appendSlice(arena, num_str); 805 try out.append(arena, ' '); 806 try out.appendSlice(arena, line); 807 } 808 return out.toOwnedSlice(arena); 809 } 810 811 // the index of the "README"/"README.md" file entry (case-insensitive), or null 812 // if none. used to pick the default selection at the repo root. 813 fn readmeIndex(entries: []const Entry) ?usize { 814 for (entries, 0..) |entry, i| { 815 if (entry.is_dir) continue; 816 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 817 return i; 818 } 819 return null; 820 } 821 822 // the index of the entry the route named (data.selected_file), or null if none. 823 fn selectedFileIndex(data: *const Self) ?usize { 824 const name = data.selected_file orelse return null; 825 for (data.entries, 0..) |entry, i| { 826 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 827 } 828 return null; 829 } 830 831 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 832 // following it navigates to that directory's listing. 833 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 834 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 835 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 836 } 837 838 // an "ai:" link to the file's route: crossPageLink ignores it, so a wasm click 839 // selects the row in place and shows its contents in the detail pane. 840 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 841 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 842 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.urlAlloc(page_arena)}); 843 } 844 845 // `dir`/`name`, or just `name` at the root. 846 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 847 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 848 } 849 850 // `dir` with its last segment removed ("" when `dir` has a single segment). 851 fn parentDir(dir: []const u8) []const u8 { 852 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 853 return dir[0..slash]; 854 } 855