╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
559 // (or a click) here loads the file like on its list row. 560 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 561 const link = try fileLink(self.session.page_arena, self.data, path, false, 0); 562 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 563 } else if (entry.is_binary) { 564 try self.addContentBox(allocator, inner, "(binary file)", ""); 565 } else { 566 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 567 try self.addContentBox(allocator, inner, text, ""); 568 } 569 } 570 } 571 // point each box at its first child so focus can descend into it. 572 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 573 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 574 575 // reserve the nav row's height (the shrink scroll frame yields it). 576 self.detailOuter().children.values()[detail_nav_index].min_size = 577 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 578 579 // reset the scroll to the top for the newly-shown file: directly on the 580 // terminal (the wasm offset), and via a version bump on the web (so the 581 // renderer's scroll id changes and JS drops the preserved position). 582 const sc = self.detailScroll(); 583 sc.x = 0; 584 sc.y = 0; 585 sc.getFocus().version +%= 1; 586 } 587 588 // a focusable "next" link above the content. it's an `a:` link to the 589 // selected file at `target_start`, so activating it (the host follows the 590 // link) reloads the page on the next window. 591 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 592 const page_arena = self.session.page_arena; 593 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 594 const route = self.data.filesRoute(path, target_start) orelse return error.RouteTooLong; 595 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 596 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 597 errdefer tb.deinit(allocator); 598 tb.getFocus().mode = .all; 599 tb.getFocus().kind = .{ .custom = link }; 600 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 601 } 602 603 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 604 if (self.headerActive()) { 605 if (key == .arrow_down) { 606 root_focus.setFocus(self.contentBox().getFocus().id); 607 } else { 608 try self.header().input(allocator, key, root_focus); 609 } 610 return; 611 } 612 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 613 if (self.detailActive()) { 614 try self.detailInput(key, root_focus); 615 } else { 616 try self.listInput(key, root_focus); 617 } 618 } 619 620 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 621 // up/down (and the scroll wheel) move the selection; page up/down jump a 622 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 623 // directory (or "..") row are turned into navigation by the host 624 // (crossPageLink) before reaching here. 625 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 626 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 627 return; 628 } 629 switch (key) { 630 .enter, .arrow_right => try self.focusDetail(root_focus), 631 else => {}, 632 } 633 } 634 635 // whether focus is on the content box (vs. a "previous"/"next" nav link). 636 fn focusOnContent(self: *View, root_focus: *Focus) bool { 637 const g = root_focus.grandchild_id orelse return false; 638 return self.detailInner().children.contains(g); 639 } 640 641 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 642 const sc = self.detailScroll(); 643 const on_content = self.focusOnContent(root_focus); 644 // the "next" link sits above the content scroll. on the link, left returns 645 // to the file list and down drops into the content. in the content, the 646 // terminal scrolls by offset and reaching the top then pressing up crosses 647 // back up to the link; on the web vertical scrolling is the browser's job 648 // so up just crosses to the link. 649 switch (key) { 650 .arrow_left => { 651 if (on_content) { 652 if (sc.x > 0) { 653 sc.x -= 1; 654 self.detailScroll().clampToContent(); 655 } else try self.focusList(root_focus); 656 } else if (!self.moveNav(root_focus, -1)) { 657 try self.focusList(root_focus); 658 } 659 }, 660 .arrow_right => { 661 if (on_content) { 662 sc.x += 1; 663 self.detailScroll().clampToContent(); 664 } else _ = self.moveNav(root_focus, 1); 665 }, 666 .arrow_up => { 667 if (on_content) { 668 if (self.session.is_terminal and sc.y > 0) { 669 sc.y -= 1; 670 self.detailScroll().clampToContent(); 671 } else self.focusNav(root_focus); // cross up to the links 672 } 673 }, 674 .arrow_down => { 675 if (on_content) { 676 if (self.session.is_terminal) { 677 sc.y += 1; 678 self.detailScroll().clampToContent(); 679 } 680 } else self.focusContent(root_focus); // links -> content 681 }, 682 .page_up => if (on_content and self.session.is_terminal) { 683 sc.y -= 10; 684 self.detailScroll().clampToContent(); 685 }, 686 .page_down => if (on_content and self.session.is_terminal) { 687 sc.y += 10; 688 self.detailScroll().clampToContent(); 689 }, 690 .home => if (on_content and self.session.is_terminal) { 691 sc.y = 0; 692 self.detailScroll().clampToContent(); 693 }, 694 .end => if (on_content and self.session.is_terminal) { 695 sc.y = std.math.maxInt(isize); 696 self.detailScroll().clampToContent(); 697 }, 698 .mouse => |mouse| switch (mouse.action) { 699 .scroll => |dir| if (on_content and self.session.is_terminal) { 700 sc.y += if (dir == .up) -5 else 5; 701 self.detailScroll().clampToContent(); 702 }, 703 else => {}, 704 }, 705 else => {}, 706 } 707 } 708 709 // focus the first (left-most) nav link; a no-op when there are none, leaving 710 // focus on the content. 711 fn focusNav(self: *View, root_focus: *Focus) void { 712 const nav_box = self.navBox(); 713 if (nav_box.children.count() == 0) return; 714 root_focus.setFocus(nav_box.children.keys()[0]); 715 } 716 717 fn focusContent(self: *View, root_focus: *Focus) void { 718 const inner = self.detailInner(); 719 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 720 root_focus.setFocus(id); 721 } 722 723 // move focus `delta` rows within the nav box; returns false (without moving) 724 // when that would step off either end. 725 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 726 const nav_box = self.navBox(); 727 const keys = nav_box.children.keys(); 728 const cur_id = nav_box.getFocus().child_id orelse return false; 729 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 730 const target = cur + delta; 731 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 732 root_focus.setFocus(keys[@intCast(target)]); 733 return true; 734 } 735 736 // enter the detail pane, landing on the "next" link when there is one (so 737 // right-arrow reaches it), else on the content. a directory or ".." row has 738 // neither, so there's nothing to enter. selecting the frame's child (then 739 // focusing the frame) also handles the too-narrow case where the pane isn't 740 // laid out yet — focus lands inside it after the next build. 741 fn focusDetail(self: *View, root_focus: *Focus) !void { 742 const frame = self.detailOuter(); 743 if (self.navBox().children.count() > 0) { 744 frame.getFocus().child_id = self.navBox().getFocus().id; 745 } else if (self.detailInner().children.count() > 0) { 746 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 747 } else return; 748 root_focus.setFocus(frame.getFocus().id); 749 } 750 751 // return to the list. 752 fn focusList(self: *View, root_focus: *Focus) !void { 753 root_focus.setFocus(self.listScroll().getFocus().id); 754 } 755 756 pub fn clearGrid(self: *View) void { 757 self.box.clearGrid(); 758 } 759 760 pub fn getGrid(self: View) ?Grid { 761 return self.box.getGrid(); 762 } 763 764 pub fn getFocus(self: *View) *Focus { 765 return self.box.getFocus(); 766 } 767 768 fn contentAtTop(self: *View) bool { 769 if (self.detailActive()) { 770 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 771 return self.navBox().children.count() == 0 and 772 (!self.session.is_terminal or self.detailScroll().y == 0); 773 } 774 const lb = self.listBox(); 775 const cid = lb.getFocus().child_id orelse return true; 776 return lb.children.getIndex(cid) == 0; 777 } 778 779 // only the clone-url row sits directly below the repository header. 780 pub fn atTop(self: *View) bool { 781 return self.headerActive() or (!self.header().hasCloneUrl() and self.contentAtTop()); 782 } 783 784 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 785 return self.header().focusCloneUrl(root_focus); 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 and isReadme(entry.name)) return i; 816 } 817 return null; 818 } 819 820 fn isReadme(name: []const u8) bool { 821 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 822 } 823 824 // the index of the entry the route named (data.selected_file), or null if none. 825 fn selectedFileIndex(data: *const Self) ?usize { 826 const name = data.selected_file orelse return null; 827 for (data.entries, 0..) |entry, i| { 828 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 829 } 830 return null; 831 } 832 833 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 834 // following it navigates to that directory's listing. 835 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 836 const route = data.filesRoute(path, 0) orelse return error.RouteTooLong; 837 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 838 } 839 840 // a link to the file's route. a preloaded file gets an "ai:" link 841 // (crossPageLink ignores it, so a click selects the row in place and shows its 842 // contents); an unloaded file gets an "a:" link, so activating it reloads the 843 // page with that file selected and its content read. 844 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool, line: usize) ![]const u8 { 845 const route = data.filesRoute(path, line) orelse return error.RouteTooLong; 846 const prefix = if (loaded) "ai:" else "a:"; 847 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 848 } 849 850 // the url `line` value for a content window: its first line's 1-based number, 851 // except the first window, which serializes as unset (0). 852 fn lineNumber(window_start: usize) usize { 853 return if (window_start == 0) 0 else window_start + 1; 854 } 855 856 // `dir`/`name`, or just `name` at the root. 857 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 858 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 859 } 860 861 // `dir` with its last segment removed ("" when `dir` has a single segment). 862 fn parentDir(dir: []const u8) []const u8 { 863 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 864 return dir[0..slash]; 865 } 866 867 // the "<ref_or_oid> <value>" banner shown above the listing. 868 pub const Header = struct { 869 content: []const u8, 870 871 // `value` arrives url-encoded, so decode it for display. 872 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 873 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 874 return .{ 875 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 876 }; 877 } 878 879 pub const View = struct { 880 box: wgt.Box(ui.Widget), 881 data: *const Header, 882 883 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, location: ui.RoutablePage.RepoLocation) !Header.View { 884 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 885 errdefer box.deinit(allocator); 886 887 if (session.data.host_kind == .server) { 888 if (try ui.widget.CopyableText.initClone(allocator, session, location)) |value| { 889 var clone_url = value; 890 errdefer clone_url.deinit(allocator); 891 const min_width = clone_url.minWidth(); 892 box.getFocus().child_id = clone_url.getFocus().id; 893 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 894 } 895 } 896 897 var spacer = try ui.widget.Spacer.init(allocator); 898 errdefer spacer.deinit(allocator); 899 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 900 901 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 902 errdefer text_box.deinit(allocator); 903 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 904 905 return .{ .box = box, .data = data }; 906 } 907 908 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 909 self.box.deinit(allocator); 910 } 911 912 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 913 self.clearGrid(); 914 try self.box.build(allocator, constraint, root_focus); 915 } 916 917 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 918 const clone_url = self.cloneUrl() orelse return; 919 try clone_url.input(allocator, key, root_focus); 920 } 921 922 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 923 const clone_url = self.cloneUrl() orelse return false; 924 root_focus.setFocus(clone_url.getFocus().id); 925 return true; 926 } 927 928 pub fn hasCloneUrl(self: *Header.View) bool { 929 return self.cloneUrl() != null; 930 } 931 932 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 933 for (self.box.children.values()) |*child| switch (child.widget) { 934 .copyable_text => |*copyable_text| return copyable_text, 935 else => {}, 936 }; 937 return null; 938 } 939 940 pub fn clearGrid(self: *Header.View) void { 941 self.box.clearGrid(); 942 } 943 944 pub fn getGrid(self: Header.View) ?Grid { 945 return self.box.getGrid(); 946 } 947 948 pub fn getFocus(self: *Header.View) *Focus { 949 return self.box.getFocus(); 950 } 951 }; 952 }; 953