╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
474 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 475 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 476 477 // cap the list at list_max_width only while the detail pane fits beside 478 // it. the box drops the detail pane when the width can't hold both 479 // minimums, so when it's that narrow we lift the cap and let the list 480 // fill the whole width. 481 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 482 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 483 484 // stretch the detail pane and its bordered scroll frame across the rest 485 // of the width so the pane fills the area rather than shrinking to its 486 // content, like the refs view's columns. the content scroll inside keeps 487 // its natural width: forcing it to the full inner width would make it one 488 // column too wide whenever the vertical scrollbar appears, leaving a 489 // phantom horizontal scroll. (height fills via the scroll clipping to the 490 // viewport.) 491 if (constraint.max_size.width) |w| { 492 const detail_w = if (both_panes_fit) w - list_max_width else w; 493 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 494 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 495 } else { 496 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 497 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 498 } 499 500 // crossing panes only re-selects the content box's child (focusList / 501 // focusDetail); when the window is too narrow to show both, the pane that 502 // was holding focus is dropped here. the framework recovers focus after 503 // the top-level build by re-deriving it down the selected-child chain, so 504 // there's nothing to fix up afterward. 505 try self.box.build(allocator, constraint, root_focus); 506 507 // clamp the list's initial offset (a selected row near the end of the 508 // list) against this build's grids, for the next frame. initRoot's 509 // pre-sizing build is unbounded — its full-content "viewport" would 510 // clamp the offset to zero, so only a bounded build counts. 511 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 512 } 513 514 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 515 const cur = self.selectedRowIndex(); 516 if (std.meta.eql(cur, self.shown_index)) return; 517 try self.populateDetail(allocator); 518 self.shown_index = cur; 519 } 520 521 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 522 const nav_box = self.navBox(); 523 const inner = self.detailInner(); 524 525 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 526 nav_box.children.clearAndFree(allocator); 527 nav_box.getFocus().child_id = null; 528 for (inner.children.values()) |*child| child.widget.deinit(allocator); 529 inner.children.clearAndFree(allocator); 530 inner.getFocus().child_id = null; 531 532 // only files have contents to show; directories and the ".." row leave 533 // the pane empty. 534 if (self.selectedEntry()) |entry| { 535 if (!entry.is_dir) { 536 // the window links sit above the scroll, so they stay put 537 // while the content scrolls. 538 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 539 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 540 if (!entry.loaded) { 541 // the placeholder carries the file's "a:" link, so enter 542 // (or a click) here loads the file like on its list row. 543 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 544 const link = try fileLink(self.session.page_arena, self.data, path, false); 545 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 546 } else if (entry.is_binary) { 547 try self.addContentBox(allocator, inner, "(binary file)", ""); 548 } else { 549 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 550 try self.addContentBox(allocator, inner, text, ""); 551 } 552 } 553 } 554 // point each box at its first child so focus can descend into it. 555 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 556 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 557 558 // reserve the nav row's height (the shrink scroll frame yields it). 559 self.detailOuter().children.values()[detail_nav_index].min_size = 560 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 561 562 // reset the scroll to the top for the newly-shown file: directly on the 563 // terminal (the wasm offset), and via a version bump on the web (so the 564 // renderer's scroll id changes and JS drops the preserved position). 565 const sc = self.detailScroll(); 566 sc.x = 0; 567 sc.y = 0; 568 sc.getFocus().version +%= 1; 569 } 570 571 // a focusable "next" link above the content. it's an `a:` link to the 572 // selected file at `target_start`, so activating it (the host follows the 573 // link) reloads the page on the next window. 574 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 575 const page_arena = self.session.page_arena; 576 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 577 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; 578 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 579 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 580 errdefer tb.deinit(allocator); 581 tb.getFocus().focusable = true; 582 tb.getFocus().kind = .{ .custom = link }; 583 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 584 } 585 586 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 587 _ = allocator; 588 if (self.detailActive()) { 589 try self.detailInput(key, root_focus); 590 } else { 591 try self.listInput(key, root_focus); 592 } 593 } 594 595 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 596 // up/down (and the scroll wheel) move the selection; page up/down jump a 597 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 598 // directory (or "..") row are turned into navigation by the host 599 // (crossPageLink) before reaching here. 600 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 601 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 602 return; 603 } 604 switch (key) { 605 .enter, .arrow_right => try self.focusDetail(root_focus), 606 else => {}, 607 } 608 } 609 610 // whether focus is on the content box (vs. a "previous"/"next" nav link). 611 fn focusOnContent(self: *View, root_focus: *Focus) bool { 612 const g = root_focus.grandchild_id orelse return false; 613 return self.detailInner().children.contains(g); 614 } 615 616 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 617 const sc = self.detailScroll(); 618 const on_content = self.focusOnContent(root_focus); 619 // the "next" link sits above the content scroll. on the link, left returns 620 // to the file list and down drops into the content. in the content, the 621 // terminal scrolls by offset and reaching the top then pressing up crosses 622 // back up to the link; on the web vertical scrolling is the browser's job 623 // so up just crosses to the link. 624 switch (key) { 625 .arrow_left => { 626 if (on_content) { 627 if (sc.x > 0) { 628 sc.x -= 1; 629 self.detailScroll().clampToContent(); 630 } else try self.focusList(root_focus); 631 } else if (!self.moveNav(root_focus, -1)) { 632 try self.focusList(root_focus); 633 } 634 }, 635 .arrow_right => { 636 if (on_content) { 637 sc.x += 1; 638 self.detailScroll().clampToContent(); 639 } else _ = self.moveNav(root_focus, 1); 640 }, 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 "scroll up at the top jumps to the header" check. inside 744 // the detail pane up-arrow stays in the pane (scrolling); when the list holds 745 // focus, report its selection directly so up-arrow at the top row ascends. 746 pub fn getSelectedIndex(self: *View) ?usize { 747 if (self.detailActive()) return 1; 748 const lb = self.listBox(); 749 const cid = lb.getFocus().child_id orelse return null; 750 return lb.children.getIndex(cid); 751 } 752 }; 753 754 // join `lines` into one string, each prefixed with its 1-based file line number 755 // (right-aligned in a column wide enough for the last number, then a space), 756 // like the diffs view. `start` is the window's 0-based first line, so numbers 757 // continue across paginated windows. 758 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 759 if (lines.len == 0) return ""; 760 const width = std.fmt.count("{d}", .{start + lines.len}); 761 const indent = try arena.alloc(u8, width); 762 @memset(indent, ' '); 763 764 var out: std.ArrayList(u8) = .empty; 765 for (lines, 0..) |line, i| { 766 if (i != 0) try out.append(arena, '\n'); 767 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 768 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 769 try out.appendSlice(arena, num_str); 770 try out.append(arena, ' '); 771 try out.appendSlice(arena, line); 772 } 773 return out.toOwnedSlice(arena); 774 } 775 776 // the index of the "README"/"README.md" file entry (case-insensitive), or null 777 // if none. used to pick the default selection at the repo root. 778 fn readmeIndex(entries: []const Entry) ?usize { 779 for (entries, 0..) |entry, i| { 780 if (!entry.is_dir and isReadme(entry.name)) return i; 781 } 782 return null; 783 } 784 785 fn isReadme(name: []const u8) bool { 786 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 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 // a link to the file's route. a preloaded file gets an "ai:" link 806 // (crossPageLink ignores it, so a click selects the row in place and shows its 807 // contents); an unloaded file gets an "a:" link, so activating it reloads the 808 // page with that file selected and its content read. 809 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool) ![]const u8 { 810 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 811 const prefix = if (loaded) "ai:" else "a:"; 812 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 813 } 814 815 // the url `line` value for a content window: its first line's 1-based number, 816 // except the first window, which serializes as unset (0). 817 fn lineNumber(window_start: usize) usize { 818 return if (window_start == 0) 0 else window_start + 1; 819 } 820 821 // `dir`/`name`, or just `name` at the root. 822 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 823 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 824 } 825 826 // `dir` with its last segment removed ("" when `dir` has a single segment). 827 fn parentDir(dir: []const u8) []const u8 { 828 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 829 return dir[0..slash]; 830 } 831 832 // the "viewing <ref_or_oid> <value>" banner shown above the listing. 833 pub const Header = struct { 834 content: []const u8, 835 836 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 837 return .{ 838 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), value }), 839 }; 840 } 841 842 pub const View = struct { 843 box: wgt.Box(ui.Widget), 844 data: *const Header, 845 846 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 847 _ = session; 848 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 849 errdefer box.deinit(allocator); 850 851 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 852 errdefer text_box.deinit(allocator); 853 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 854 855 return .{ .box = box, .data = data }; 856 } 857 858 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 859 self.box.deinit(allocator); 860 } 861 862 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 863 self.clearGrid(); 864 try self.box.build(allocator, constraint, root_focus); 865 } 866 867 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 868 _ = self; 869 _ = allocator; 870 _ = key; 871 _ = root_focus; 872 } 873 874 pub fn clearGrid(self: *Header.View) void { 875 self.box.clearGrid(); 876 } 877 878 pub fn getGrid(self: Header.View) ?Grid { 879 return self.box.getGrid(); 880 } 881 882 pub fn getFocus(self: *Header.View) *Focus { 883 return self.box.getFocus(); 884 } 885 }; 886 }; 887