╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
603 // (crossPageLink) before reaching here. 604 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 605 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 606 return; 607 } 608 switch (key) { 609 .enter, .arrow_right => try self.focusDetail(root_focus), 610 else => {}, 611 } 612 } 613 614 // whether focus is on the content box (vs. a "previous"/"next" nav link). 615 fn focusOnContent(self: *View, root_focus: *Focus) bool { 616 const g = root_focus.grandchild_id orelse return false; 617 return self.detailInner().children.contains(g); 618 } 619 620 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 621 const sc = self.detailScroll(); 622 const on_content = self.focusOnContent(root_focus); 623 // the "next" link sits above the content scroll. on the link, left returns 624 // to the file list and down drops into the content. in the content, the 625 // terminal scrolls by offset and reaching the top then pressing up crosses 626 // back up to the link; on the web vertical scrolling is the browser's job 627 // so up just crosses to the link. 628 switch (key) { 629 .arrow_left => { 630 if (on_content) { 631 if (sc.x > 0) { 632 sc.x -= 1; 633 self.detailScroll().clampToContent(); 634 } else try self.focusList(root_focus); 635 } else if (!self.moveNav(root_focus, -1)) { 636 try self.focusList(root_focus); 637 } 638 }, 639 .arrow_right => { 640 if (on_content) { 641 sc.x += 1; 642 self.detailScroll().clampToContent(); 643 } else _ = self.moveNav(root_focus, 1); 644 }, 645 .arrow_up => { 646 if (on_content) { 647 if (self.session.is_terminal and sc.y > 0) { 648 sc.y -= 1; 649 self.detailScroll().clampToContent(); 650 } else self.focusNav(root_focus); // cross up to the links 651 } 652 }, 653 .arrow_down => { 654 if (on_content) { 655 if (self.session.is_terminal) { 656 sc.y += 1; 657 self.detailScroll().clampToContent(); 658 } 659 } else self.focusContent(root_focus); // links -> content 660 }, 661 .page_up => if (on_content and self.session.is_terminal) { 662 sc.y -= 10; 663 self.detailScroll().clampToContent(); 664 }, 665 .page_down => if (on_content and self.session.is_terminal) { 666 sc.y += 10; 667 self.detailScroll().clampToContent(); 668 }, 669 .home => if (on_content and self.session.is_terminal) { 670 sc.y = 0; 671 self.detailScroll().clampToContent(); 672 }, 673 .end => if (on_content and self.session.is_terminal) { 674 sc.y = std.math.maxInt(isize); 675 self.detailScroll().clampToContent(); 676 }, 677 .mouse => |mouse| switch (mouse.action) { 678 .scroll => |dir| if (on_content and self.session.is_terminal) { 679 sc.y += if (dir == .up) -5 else 5; 680 self.detailScroll().clampToContent(); 681 }, 682 else => {}, 683 }, 684 else => {}, 685 } 686 } 687 688 // focus the first (left-most) nav link; a no-op when there are none, leaving 689 // focus on the content. 690 fn focusNav(self: *View, root_focus: *Focus) void { 691 const nav_box = self.navBox(); 692 if (nav_box.children.count() == 0) return; 693 root_focus.setFocus(nav_box.children.keys()[0]); 694 } 695 696 fn focusContent(self: *View, root_focus: *Focus) void { 697 const inner = self.detailInner(); 698 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 699 root_focus.setFocus(id); 700 } 701 702 // move focus `delta` rows within the nav box; returns false (without moving) 703 // when that would step off either end. 704 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 705 const nav_box = self.navBox(); 706 const keys = nav_box.children.keys(); 707 const cur_id = nav_box.getFocus().child_id orelse return false; 708 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 709 const target = cur + delta; 710 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 711 root_focus.setFocus(keys[@intCast(target)]); 712 return true; 713 } 714 715 // enter the detail pane, landing on the "next" link when there is one (so 716 // right-arrow reaches it), else on the content. a directory or ".." row has 717 // neither, so there's nothing to enter. selecting the frame's child (then 718 // focusing the frame) also handles the too-narrow case where the pane isn't 719 // laid out yet — focus lands inside it after the next build. 720 fn focusDetail(self: *View, root_focus: *Focus) !void { 721 const frame = self.detailOuter(); 722 if (self.navBox().children.count() > 0) { 723 frame.getFocus().child_id = self.navBox().getFocus().id; 724 } else if (self.detailInner().children.count() > 0) { 725 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 726 } else return; 727 root_focus.setFocus(frame.getFocus().id); 728 } 729 730 // return to the list. 731 fn focusList(self: *View, root_focus: *Focus) !void { 732 root_focus.setFocus(self.listScroll().getFocus().id); 733 } 734 735 pub fn clearGrid(self: *View) void { 736 self.box.clearGrid(); 737 } 738 739 pub fn getGrid(self: View) ?Grid { 740 return self.box.getGrid(); 741 } 742 743 pub fn getFocus(self: *View) *Focus { 744 return self.box.getFocus(); 745 } 746 747 // for the parent's "scroll up at the top jumps to the header" check. inside 748 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 749 // focus, report its selection directly so up-arrow at the top row ascends. 750 pub fn getSelectedIndex(self: *View) ?usize { 751 if (self.detailActive()) return 1; 752 const lb = self.listBox(); 753 const cid = lb.getFocus().child_id orelse return null; 754 return lb.children.getIndex(cid); 755 } 756 }; 757 758 // join `lines` into one string, each prefixed with its 1-based file line number 759 // (right-aligned in a column wide enough for the last number, then a space), 760 // like the diffs view. `start` is the window's 0-based first line, so numbers 761 // continue across paginated windows. 762 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 763 if (lines.len == 0) return ""; 764 const width = std.fmt.count("{d}", .{start + lines.len}); 765 const indent = try arena.alloc(u8, width); 766 @memset(indent, ' '); 767 768 var out: std.ArrayList(u8) = .empty; 769 for (lines, 0..) |line, i| { 770 if (i != 0) try out.append(arena, '\n'); 771 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 772 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 773 try out.appendSlice(arena, num_str); 774 try out.append(arena, ' '); 775 try out.appendSlice(arena, line); 776 } 777 return out.toOwnedSlice(arena); 778 } 779 780 // the index of the "README"/"README.md" file entry (case-insensitive), or null 781 // if none. used to pick the default selection at the repo root. 782 fn readmeIndex(entries: []const Entry) ?usize { 783 for (entries, 0..) |entry, i| { 784 if (entry.is_dir) continue; 785 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 786 return i; 787 } 788 return null; 789 } 790 791 // the index of the entry the route named (data.selected_file), or null if none. 792 fn selectedFileIndex(data: *const Self) ?usize { 793 const name = data.selected_file orelse return null; 794 for (data.entries, 0..) |entry, i| { 795 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 796 } 797 return null; 798 } 799 800 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 801 // following it navigates to that directory's listing. 802 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 803 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 804 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.urlAlloc(page_arena)}); 805 } 806 807 // an "ai:" link to the file's route: crossPageLink ignores it, so a wasm click 808 // selects the row in place and shows its contents in the detail pane. 809 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 810 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 811 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.urlAlloc(page_arena)}); 812 } 813 814 // `dir`/`name`, or just `name` at the root. 815 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 816 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 817 } 818 819 // `dir` with its last segment removed ("" when `dir` has a single segment). 820 fn parentDir(dir: []const u8) []const u8 { 821 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 822 return dir[0..slash]; 823 } 824