╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
641 .arrow_up => { 642 if (on_content) { 643 if (self.session.is_terminal and sc.y > 0) { 644 sc.y -= 1; 645 self.detailScroll().clampToContent(); 646 } else self.focusNav(root_focus); // cross up to the links 647 } 648 }, 649 .arrow_down => { 650 if (on_content) { 651 if (self.session.is_terminal) { 652 sc.y += 1; 653 self.detailScroll().clampToContent(); 654 } 655 } else self.focusContent(root_focus); // links -> content 656 }, 657 .page_up => if (on_content and self.session.is_terminal) { 658 sc.y -= 10; 659 self.detailScroll().clampToContent(); 660 }, 661 .page_down => if (on_content and self.session.is_terminal) { 662 sc.y += 10; 663 self.detailScroll().clampToContent(); 664 }, 665 .home => if (on_content and self.session.is_terminal) { 666 sc.y = 0; 667 self.detailScroll().clampToContent(); 668 }, 669 .end => if (on_content and self.session.is_terminal) { 670 sc.y = std.math.maxInt(isize); 671 self.detailScroll().clampToContent(); 672 }, 673 .mouse => |mouse| switch (mouse.action) { 674 .scroll => |dir| if (on_content and self.session.is_terminal) { 675 sc.y += if (dir == .up) -5 else 5; 676 self.detailScroll().clampToContent(); 677 }, 678 else => {}, 679 }, 680 else => {}, 681 } 682 } 683 684 // focus the first (left-most) nav link; a no-op when there are none, leaving 685 // focus on the content. 686 fn focusNav(self: *View, root_focus: *Focus) void { 687 const nav_box = self.navBox(); 688 if (nav_box.children.count() == 0) return; 689 root_focus.setFocus(nav_box.children.keys()[0]); 690 } 691 692 fn focusContent(self: *View, root_focus: *Focus) void { 693 const inner = self.detailInner(); 694 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 695 root_focus.setFocus(id); 696 } 697 698 // move focus `delta` rows within the nav box; returns false (without moving) 699 // when that would step off either end. 700 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 701 const nav_box = self.navBox(); 702 const keys = nav_box.children.keys(); 703 const cur_id = nav_box.getFocus().child_id orelse return false; 704 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 705 const target = cur + delta; 706 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 707 root_focus.setFocus(keys[@intCast(target)]); 708 return true; 709 } 710 711 // enter the detail pane, landing on the "next" link when there is one (so 712 // right-arrow reaches it), else on the content. a directory or ".." row has 713 // neither, so there's nothing to enter. selecting the frame's child (then 714 // focusing the frame) also handles the too-narrow case where the pane isn't 715 // laid out yet — focus lands inside it after the next build. 716 fn focusDetail(self: *View, root_focus: *Focus) !void { 717 const frame = self.detailOuter(); 718 if (self.navBox().children.count() > 0) { 719 frame.getFocus().child_id = self.navBox().getFocus().id; 720 } else if (self.detailInner().children.count() > 0) { 721 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 722 } else return; 723 root_focus.setFocus(frame.getFocus().id); 724 } 725 726 // return to the list. 727 fn focusList(self: *View, root_focus: *Focus) !void { 728 root_focus.setFocus(self.listScroll().getFocus().id); 729 } 730 731 pub fn clearGrid(self: *View) void { 732 self.box.clearGrid(); 733 } 734 735 pub fn getGrid(self: View) ?Grid { 736 return self.box.getGrid(); 737 } 738 739 pub fn getFocus(self: *View) *Focus { 740 return self.box.getFocus(); 741 } 742 743 // for the parent's "up at the top jumps to the header" check: report 0 744 // whenever up would otherwise do nothing here. 745 pub fn getSelectedIndex(self: *View) ?usize { 746 if (self.detailActive()) { 747 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return 0; 748 if (self.navBox().children.count() == 0 and 749 (!self.session.is_terminal or self.detailScroll().y == 0)) return 0; 750 return 1; 751 } 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 and isReadme(entry.name)) return i; 785 } 786 return null; 787 } 788 789 fn isReadme(name: []const u8) bool { 790 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 791 } 792 793 // the index of the entry the route named (data.selected_file), or null if none. 794 fn selectedFileIndex(data: *const Self) ?usize { 795 const name = data.selected_file orelse return null; 796 for (data.entries, 0..) |entry, i| { 797 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 798 } 799 return null; 800 } 801 802 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 803 // following it navigates to that directory's listing. 804 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 805 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 806 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 807 } 808 809 // a link to the file's route. a preloaded file gets an "ai:" link 810 // (crossPageLink ignores it, so a click selects the row in place and shows its 811 // contents); an unloaded file gets an "a:" link, so activating it reloads the 812 // page with that file selected and its content read. 813 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool) ![]const u8 { 814 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 815 const prefix = if (loaded) "ai:" else "a:"; 816 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 817 } 818 819 // the url `line` value for a content window: its first line's 1-based number, 820 // except the first window, which serializes as unset (0). 821 fn lineNumber(window_start: usize) usize { 822 return if (window_start == 0) 0 else window_start + 1; 823 } 824 825 // `dir`/`name`, or just `name` at the root. 826 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 827 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 828 } 829 830 // `dir` with its last segment removed ("" when `dir` has a single segment). 831 fn parentDir(dir: []const u8) []const u8 { 832 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 833 return dir[0..slash]; 834 } 835 836 // the "viewing <ref_or_oid> <value>" banner shown above the listing. 837 pub const Header = struct { 838 content: []const u8, 839 840 // `value` arrives url-encoded, so decode it for display. 841 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 842 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 843 return .{ 844 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), decoded }), 845 }; 846 } 847 848 pub const View = struct { 849 box: wgt.Box(ui.Widget), 850 data: *const Header, 851 852 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 853 _ = session; 854 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 855 errdefer box.deinit(allocator); 856 857 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 858 errdefer text_box.deinit(allocator); 859 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 860 861 return .{ .box = box, .data = data }; 862 } 863 864 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 865 self.box.deinit(allocator); 866 } 867 868 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 869 self.clearGrid(); 870 try self.box.build(allocator, constraint, root_focus); 871 } 872 873 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 874 _ = self; 875 _ = allocator; 876 _ = key; 877 _ = root_focus; 878 } 879 880 pub fn clearGrid(self: *Header.View) void { 881 self.box.clearGrid(); 882 } 883 884 pub fn getGrid(self: Header.View) ?Grid { 885 return self.box.getGrid(); 886 } 887 888 pub fn getFocus(self: *Header.View) *Focus { 889 return self.box.getFocus(); 890 } 891 }; 892 }; 893