╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
584 tb.getFocus().kind = .{ .custom = link }; 585 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 586 } 587 588 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 589 _ = allocator; 590 if (self.detailActive()) { 591 try self.detailInput(key, root_focus); 592 } else { 593 try self.listInput(key, root_focus); 594 } 595 } 596 597 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 598 // up/down (and the scroll wheel) move the selection; page up/down jump a 599 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 600 // directory (or "..") row are turned into navigation by the host 601 // (crossPageLink) before reaching here. 602 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 603 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 604 return; 605 } 606 switch (key) { 607 .enter, .arrow_right => try self.focusDetail(root_focus), 608 else => {}, 609 } 610 } 611 612 // whether focus is on the content box (vs. a "previous"/"next" nav link). 613 fn focusOnContent(self: *View, root_focus: *Focus) bool { 614 const g = root_focus.grandchild_id orelse return false; 615 return self.detailInner().children.contains(g); 616 } 617 618 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 619 const sc = self.detailScroll(); 620 const on_content = self.focusOnContent(root_focus); 621 // the "next" link sits above the content scroll. on the link, left returns 622 // to the file list and down drops into the content. in the content, the 623 // terminal scrolls by offset and reaching the top then pressing up crosses 624 // back up to the link; on the web vertical scrolling is the browser's job 625 // so up just crosses to the link. 626 switch (key) { 627 .arrow_left => { 628 if (on_content) { 629 if (sc.x > 0) { 630 sc.x -= 1; 631 self.detailScroll().clampToContent(); 632 } else try self.focusList(root_focus); 633 } else if (!self.moveNav(root_focus, -1)) { 634 try self.focusList(root_focus); 635 } 636 }, 637 .arrow_right => { 638 if (on_content) { 639 sc.x += 1; 640 self.detailScroll().clampToContent(); 641 } else _ = self.moveNav(root_focus, 1); 642 }, 643 .arrow_up => { 644 if (on_content) { 645 if (self.session.is_terminal and sc.y > 0) { 646 sc.y -= 1; 647 self.detailScroll().clampToContent(); 648 } else self.focusNav(root_focus); // cross up to the links 649 } 650 }, 651 .arrow_down => { 652 if (on_content) { 653 if (self.session.is_terminal) { 654 sc.y += 1; 655 self.detailScroll().clampToContent(); 656 } 657 } else self.focusContent(root_focus); // links -> content 658 }, 659 .page_up => if (on_content and self.session.is_terminal) { 660 sc.y -= 10; 661 self.detailScroll().clampToContent(); 662 }, 663 .page_down => if (on_content and self.session.is_terminal) { 664 sc.y += 10; 665 self.detailScroll().clampToContent(); 666 }, 667 .home => if (on_content and self.session.is_terminal) { 668 sc.y = 0; 669 self.detailScroll().clampToContent(); 670 }, 671 .end => if (on_content and self.session.is_terminal) { 672 sc.y = std.math.maxInt(isize); 673 self.detailScroll().clampToContent(); 674 }, 675 .mouse => |mouse| switch (mouse.action) { 676 .scroll => |dir| if (on_content and self.session.is_terminal) { 677 sc.y += if (dir == .up) -5 else 5; 678 self.detailScroll().clampToContent(); 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.urlAlloc(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.urlAlloc(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