╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
679 }, 680 else => {}, 681 }, 682 else => {}, 683 } 684 } 685 686 // focus the first (left-most) nav link; a no-op when there are none, leaving 687 // focus on the content. 688 fn focusNav(self: *View, root_focus: *Focus) void { 689 const nav_box = self.navBox(); 690 if (nav_box.children.count() == 0) return; 691 root_focus.setFocus(nav_box.children.keys()[0]); 692 } 693 694 fn focusContent(self: *View, root_focus: *Focus) void { 695 const inner = self.detailInner(); 696 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 697 root_focus.setFocus(id); 698 } 699 700 // move focus `delta` rows within the nav box; returns false (without moving) 701 // when that would step off either end. 702 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 703 const nav_box = self.navBox(); 704 const keys = nav_box.children.keys(); 705 const cur_id = nav_box.getFocus().child_id orelse return false; 706 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 707 const target = cur + delta; 708 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 709 root_focus.setFocus(keys[@intCast(target)]); 710 return true; 711 } 712 713 // enter the detail pane, landing on the "next" link when there is one (so 714 // right-arrow reaches it), else on the content. a directory or ".." row has 715 // neither, so there's nothing to enter. selecting the frame's child (then 716 // focusing the frame) also handles the too-narrow case where the pane isn't 717 // laid out yet — focus lands inside it after the next build. 718 fn focusDetail(self: *View, root_focus: *Focus) !void { 719 const frame = self.detailOuter(); 720 if (self.navBox().children.count() > 0) { 721 frame.getFocus().child_id = self.navBox().getFocus().id; 722 } else if (self.detailInner().children.count() > 0) { 723 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 724 } else return; 725 root_focus.setFocus(frame.getFocus().id); 726 } 727 728 // return to the list. 729 fn focusList(self: *View, root_focus: *Focus) !void { 730 root_focus.setFocus(self.listScroll().getFocus().id); 731 } 732 733 pub fn clearGrid(self: *View) void { 734 self.box.clearGrid(); 735 } 736 737 pub fn getGrid(self: View) ?Grid { 738 return self.box.getGrid(); 739 } 740 741 pub fn getFocus(self: *View) *Focus { 742 return self.box.getFocus(); 743 } 744 745 // for the parent's "scroll up at the top jumps to the header" check. inside 746 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 747 // focus, report its selection directly so up-arrow at the top row ascends. 748 pub fn getSelectedIndex(self: *View) ?usize { 749 if (self.detailActive()) return 1; 750 const lb = self.listBox(); 751 const cid = lb.getFocus().child_id orelse return null; 752 return lb.children.getIndex(cid); 753 } 754 }; 755 756 // join `lines` into one string, each prefixed with its 1-based file line number 757 // (right-aligned in a column wide enough for the last number, then a space), 758 // like the diffs view. `start` is the window's 0-based first line, so numbers 759 // continue across paginated windows. 760 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 761 if (lines.len == 0) return ""; 762 const width = std.fmt.count("{d}", .{start + lines.len}); 763 const indent = try arena.alloc(u8, width); 764 @memset(indent, ' '); 765 766 var out: std.ArrayList(u8) = .empty; 767 for (lines, 0..) |line, i| { 768 if (i != 0) try out.append(arena, '\n'); 769 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 770 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 771 try out.appendSlice(arena, num_str); 772 try out.append(arena, ' '); 773 try out.appendSlice(arena, line); 774 } 775 return out.toOwnedSlice(arena); 776 } 777 778 // the index of the "README"/"README.md" file entry (case-insensitive), or null 779 // if none. used to pick the default selection at the repo root. 780 fn readmeIndex(entries: []const Entry) ?usize { 781 for (entries, 0..) |entry, i| { 782 if (entry.is_dir) continue; 783 if (std.ascii.eqlIgnoreCase(entry.name, "README") or std.ascii.eqlIgnoreCase(entry.name, "README.md")) 784 return i; 785 } 786 return null; 787 } 788 789 // the index of the entry the route named (data.selected_file), or null if none. 790 fn selectedFileIndex(data: *const Self) ?usize { 791 const name = data.selected_file orelse return null; 792 for (data.entries, 0..) |entry, i| { 793 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 794 } 795 return null; 796 } 797 798 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 799 // following it navigates to that directory's listing. 800 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 801 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 802 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 803 } 804 805 // an "ai:" link to the file's route: crossPageLink ignores it, so a wasm click 806 // selects the row in place and shows its contents in the detail pane. 807 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 808 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 809 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 810 } 811 812 // `dir`/`name`, or just `name` at the root. 813 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 814 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 815 } 816 817 // `dir` with its last segment removed ("" when `dir` has a single segment). 818 fn parentDir(dir: []const u8) []const u8 { 819 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 820 return dir[0..slash]; 821 } 822 823 // the "viewing <ref_or_oid> <value>" banner shown above the listing. 824 pub const Header = struct { 825 content: []const u8, 826 827 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 828 return .{ 829 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), value }), 830 }; 831 } 832 833 pub const View = struct { 834 box: wgt.Box(ui.Widget), 835 data: *const Header, 836 837 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 838 _ = session; 839 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 840 errdefer box.deinit(allocator); 841 842 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 843 errdefer text_box.deinit(allocator); 844 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 845 846 return .{ .box = box, .data = data }; 847 } 848 849 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 850 self.box.deinit(allocator); 851 } 852 853 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 854 self.clearGrid(); 855 try self.box.build(allocator, constraint, root_focus); 856 } 857 858 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 859 _ = self; 860 _ = allocator; 861 _ = key; 862 _ = root_focus; 863 } 864 865 pub fn clearGrid(self: *Header.View) void { 866 self.box.clearGrid(); 867 } 868 869 pub fn getGrid(self: Header.View) ?Grid { 870 return self.box.getGrid(); 871 } 872 873 pub fn getFocus(self: *Header.View) *Focus { 874 return self.box.getFocus(); 875 } 876 }; 877 }; 878