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