╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
604 if (self.detailActive()) { 605 try self.detailInput(key, root_focus); 606 } else { 607 try self.listInput(key, root_focus); 608 } 609 } 610 611 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 612 // up/down (and the scroll wheel) move the selection; page up/down jump a 613 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 614 // directory (or "..") row are turned into navigation by the host 615 // (crossPageLink) before reaching here. 616 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 617 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 618 return; 619 } 620 switch (key) { 621 .enter, .arrow_right => try self.focusDetail(root_focus), 622 else => {}, 623 } 624 } 625 626 // whether focus is on the content box (vs. a "previous"/"next" nav link). 627 fn focusOnContent(self: *View, root_focus: *Focus) bool { 628 const g = root_focus.grandchild_id orelse return false; 629 return self.detailInner().children.contains(g); 630 } 631 632 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 633 const sc = self.detailScroll(); 634 const on_content = self.focusOnContent(root_focus); 635 // the "next" link sits above the content scroll. on the link, left returns 636 // to the file list and down drops into the content. in the content, the 637 // terminal scrolls by offset and reaching the top then pressing up crosses 638 // back up to the link; on the web vertical scrolling is the browser's job 639 // so up just crosses to the link. 640 switch (key) { 641 .arrow_left => { 642 if (on_content) { 643 if (sc.x > 0) { 644 sc.x -= 1; 645 self.detailScroll().clampToContent(); 646 } else try self.focusList(root_focus); 647 } else if (!self.moveNav(root_focus, -1)) { 648 try self.focusList(root_focus); 649 } 650 }, 651 .arrow_right => { 652 if (on_content) { 653 sc.x += 1; 654 self.detailScroll().clampToContent(); 655 } else _ = self.moveNav(root_focus, 1); 656 }, 657 .arrow_up => { 658 if (on_content) { 659 if (self.session.is_terminal and sc.y > 0) { 660 sc.y -= 1; 661 self.detailScroll().clampToContent(); 662 } else self.focusNav(root_focus); // cross up to the links 663 } 664 }, 665 .arrow_down => { 666 if (on_content) { 667 if (self.session.is_terminal) { 668 sc.y += 1; 669 self.detailScroll().clampToContent(); 670 } 671 } else self.focusContent(root_focus); // links -> content 672 }, 673 .page_up => if (on_content and self.session.is_terminal) { 674 sc.y -= 10; 675 self.detailScroll().clampToContent(); 676 }, 677 .page_down => if (on_content and self.session.is_terminal) { 678 sc.y += 10; 679 self.detailScroll().clampToContent(); 680 }, 681 .home => if (on_content and self.session.is_terminal) { 682 sc.y = 0; 683 self.detailScroll().clampToContent(); 684 }, 685 .end => if (on_content and self.session.is_terminal) { 686 sc.y = std.math.maxInt(isize); 687 self.detailScroll().clampToContent(); 688 }, 689 .mouse => |mouse| switch (mouse.action) { 690 .scroll => |dir| if (on_content and self.session.is_terminal) { 691 sc.y += if (dir == .up) -5 else 5; 692 self.detailScroll().clampToContent(); 693 }, 694 else => {}, 695 }, 696 else => {}, 697 } 698 } 699 700 // focus the first (left-most) nav link; a no-op when there are none, leaving 701 // focus on the content. 702 fn focusNav(self: *View, root_focus: *Focus) void { 703 const nav_box = self.navBox(); 704 if (nav_box.children.count() == 0) return; 705 root_focus.setFocus(nav_box.children.keys()[0]); 706 } 707 708 fn focusContent(self: *View, root_focus: *Focus) void { 709 const inner = self.detailInner(); 710 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 711 root_focus.setFocus(id); 712 } 713 714 // move focus `delta` rows within the nav box; returns false (without moving) 715 // when that would step off either end. 716 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 717 const nav_box = self.navBox(); 718 const keys = nav_box.children.keys(); 719 const cur_id = nav_box.getFocus().child_id orelse return false; 720 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 721 const target = cur + delta; 722 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 723 root_focus.setFocus(keys[@intCast(target)]); 724 return true; 725 } 726 727 // enter the detail pane, landing on the "next" link when there is one (so 728 // right-arrow reaches it), else on the content. a directory or ".." row has 729 // neither, so there's nothing to enter. selecting the frame's child (then 730 // focusing the frame) also handles the too-narrow case where the pane isn't 731 // laid out yet — focus lands inside it after the next build. 732 fn focusDetail(self: *View, root_focus: *Focus) !void { 733 const frame = self.detailOuter(); 734 if (self.navBox().children.count() > 0) { 735 frame.getFocus().child_id = self.navBox().getFocus().id; 736 } else if (self.detailInner().children.count() > 0) { 737 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 738 } else return; 739 root_focus.setFocus(frame.getFocus().id); 740 } 741 742 // return to the list. 743 fn focusList(self: *View, root_focus: *Focus) !void { 744 root_focus.setFocus(self.listScroll().getFocus().id); 745 } 746 747 pub fn clearGrid(self: *View) void { 748 self.box.clearGrid(); 749 } 750 751 pub fn getGrid(self: View) ?Grid { 752 return self.box.getGrid(); 753 } 754 755 pub fn getFocus(self: *View) *Focus { 756 return self.box.getFocus(); 757 } 758 759 fn contentAtTop(self: *View) bool { 760 if (self.detailActive()) { 761 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 762 return self.navBox().children.count() == 0 and 763 (!self.session.is_terminal or self.detailScroll().y == 0); 764 } 765 const lb = self.listBox(); 766 const cid = lb.getFocus().child_id orelse return true; 767 return lb.children.getIndex(cid) == 0; 768 } 769 770 // only the clone-url row sits directly below the repository header. 771 pub fn getSelectedIndex(self: *View) ?usize { 772 if (self.headerActive()) return 0; 773 return if (self.header().hasCloneUrl() or !self.contentAtTop()) 1 else 0; 774 } 775 776 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 777 return self.header().focusCloneUrl(root_focus); 778 } 779 }; 780 781 // join `lines` into one string, each prefixed with its 1-based file line number 782 // (right-aligned in a column wide enough for the last number, then a space), 783 // like the diffs view. `start` is the window's 0-based first line, so numbers 784 // continue across paginated windows. 785 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 786 if (lines.len == 0) return ""; 787 const width = std.fmt.count("{d}", .{start + lines.len}); 788 const indent = try arena.alloc(u8, width); 789 @memset(indent, ' '); 790 791 var out: std.ArrayList(u8) = .empty; 792 for (lines, 0..) |line, i| { 793 if (i != 0) try out.append(arena, '\n'); 794 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 795 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 796 try out.appendSlice(arena, num_str); 797 try out.append(arena, ' '); 798 try out.appendSlice(arena, line); 799 } 800 return out.toOwnedSlice(arena); 801 } 802 803 // the index of the "README"/"README.md" file entry (case-insensitive), or null 804 // if none. used to pick the default selection at the repo root. 805 fn readmeIndex(entries: []const Entry) ?usize { 806 for (entries, 0..) |entry, i| { 807 if (!entry.is_dir and isReadme(entry.name)) return i; 808 } 809 return null; 810 } 811 812 fn isReadme(name: []const u8) bool { 813 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 814 } 815 816 // the index of the entry the route named (data.selected_file), or null if none. 817 fn selectedFileIndex(data: *const Self) ?usize { 818 const name = data.selected_file orelse return null; 819 for (data.entries, 0..) |entry, i| { 820 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 821 } 822 return null; 823 } 824 825 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 826 // following it navigates to that directory's listing. 827 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 828 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 829 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 830 } 831 832 // a link to the file's route. a preloaded file gets an "ai:" link 833 // (crossPageLink ignores it, so a click selects the row in place and shows its 834 // contents); an unloaded file gets an "a:" link, so activating it reloads the 835 // page with that file selected and its content read. 836 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool) ![]const u8 { 837 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 838 const prefix = if (loaded) "ai:" else "a:"; 839 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 840 } 841 842 // the url `line` value for a content window: its first line's 1-based number, 843 // except the first window, which serializes as unset (0). 844 fn lineNumber(window_start: usize) usize { 845 return if (window_start == 0) 0 else window_start + 1; 846 } 847 848 // `dir`/`name`, or just `name` at the root. 849 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 850 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 851 } 852 853 // `dir` with its last segment removed ("" when `dir` has a single segment). 854 fn parentDir(dir: []const u8) []const u8 { 855 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 856 return dir[0..slash]; 857 } 858 859 // the "<ref_or_oid> <value>" banner shown above the listing. 860 pub const Header = struct { 861 content: []const u8, 862 863 // `value` arrives url-encoded, so decode it for display. 864 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 865 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 866 return .{ 867 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 868 }; 869 } 870 871 pub const View = struct { 872 box: wgt.Box(ui.Widget), 873 data: *const Header, 874 875 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, identity: []const u8) !Header.View { 876 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 877 errdefer box.deinit(allocator); 878 879 if (!session.data.is_local and (session.data.clone_http_port != null or session.data.clone_ssh_port != null)) { 880 var clone_url = try ui.CloneUrl.View.init(allocator, session, identity); 881 errdefer clone_url.deinit(allocator); 882 const min_width = clone_url.minWidth(); 883 box.getFocus().child_id = clone_url.getFocus().id; 884 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .clone_url = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 } }); 885 } 886 887 var spacer = try ui.Spacer.init(allocator); 888 errdefer spacer.deinit(allocator); 889 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 890 891 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 892 errdefer text_box.deinit(allocator); 893 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .shrink = true }); 894 895 return .{ .box = box, .data = data }; 896 } 897 898 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 899 self.box.deinit(allocator); 900 } 901 902 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 903 self.clearGrid(); 904 try self.box.build(allocator, constraint, root_focus); 905 } 906 907 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 908 const clone_url = self.cloneUrl() orelse return; 909 try clone_url.input(allocator, key, root_focus); 910 } 911 912 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 913 const clone_url = self.cloneUrl() orelse return false; 914 root_focus.setFocus(clone_url.getFocus().id); 915 return true; 916 } 917 918 pub fn hasCloneUrl(self: *Header.View) bool { 919 return self.cloneUrl() != null; 920 } 921 922 fn cloneUrl(self: *Header.View) ?*ui.CloneUrl.View { 923 for (self.box.children.values()) |*child| switch (child.widget) { 924 .clone_url => |*clone_url| return clone_url, 925 else => {}, 926 }; 927 return null; 928 } 929 930 pub fn clearGrid(self: *Header.View) void { 931 self.box.clearGrid(); 932 } 933 934 pub fn getGrid(self: Header.View) ?Grid { 935 return self.box.getGrid(); 936 } 937 938 pub fn getFocus(self: *Header.View) *Focus { 939 return self.box.getFocus(); 940 } 941 }; 942 }; 943