╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Files.zig ────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
403 // the bordered box wrapping the content scroll (border toggles with focus). 404 fn detailScrollFrame(self: *View) *wgt.Box(ui.Widget) { 405 return &self.detailOuter().children.values()[detail_scroll_index].widget.box; 406 } 407 408 fn detailScroll(self: *View) *wgt.Scroll(ui.Widget) { 409 return &self.detailScrollFrame().children.values()[0].widget.scroll; 410 } 411 412 fn detailInner(self: *View) *wgt.Box(ui.Widget) { 413 return &self.detailScroll().child.box; 414 } 415 416 fn detailActive(self: *View) bool { 417 const content = self.contentBox(); 418 const cid = content.getFocus().child_id orelse return false; 419 return content.children.getIndex(cid) == detail_index; 420 } 421 422 // the selected list row's index, or null when nothing is focused. 423 fn selectedRowIndex(self: *View) ?usize { 424 const lb = self.listBox(); 425 const cid = lb.getFocus().child_id orelse return null; 426 return lb.children.getIndex(cid); 427 } 428 429 // the entry the selected row points at, or null for the ".." row (which 430 // isn't an entry). directory rows return their entry too, though they carry 431 // no contents. 432 fn selectedEntry(self: *View) ?Entry { 433 const idx = self.selectedRowIndex() orelse return null; 434 const base: usize = if (self.data.dir.len != 0) 1 else 0; // skip the ".." row 435 if (idx < base) return null; 436 const e = idx - base; 437 if (e >= self.data.entries.len) return null; 438 return self.data.entries[e]; 439 } 440 441 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 442 self.clearGrid(); 443 444 // swap the detail pane to the selected entry when the selection changes. 445 try self.refreshDetail(allocator); 446 447 // mirror the selected file into the url so it updates as the selection 448 // moves, but only while focus is inside this view (so landing on the tab 449 // keeps the page's base route). 450 if (root_focus.grandchild_id) |g| { 451 if (self.box.getFocus().children.contains(g)) { 452 var buf: [ui.RoutablePage.repo_route_max_len]u8 = undefined; 453 var sel_path = self.data.dir; 454 var sel_line: usize = 0; 455 if (self.selectedEntry()) |entry| { 456 if (!entry.is_dir and entry.loaded) { 457 sel_path = if (self.data.dir.len == 0) 458 entry.name 459 else 460 std.fmt.bufPrint(&buf, "{s}/{s}", .{ self.data.dir, entry.name }) catch self.data.dir; 461 sel_line = lineNumber(entry.window_start); 462 } 463 } 464 if (ui.RoutablePage.repoFilesRoute(self.data.identity, self.data.ref_or_oid, self.data.ref_or_oid_value, sel_path, sel_line)) |route| 465 self.session.data.current_page = route; 466 } 467 } 468 469 // the selected list row shows a border (the focused TextBox upgrades it 470 // to a double border itself); the rest stay borderless. 471 const lb = self.listBox(); 472 for (lb.children.keys(), lb.children.values()) |id, *child| { 473 switch (child.widget) { 474 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 475 else => {}, 476 } 477 } 478 479 // the content scroll's border is single normally and double when the 480 // content is focused (the content box itself is borderless). 481 const has_content = self.detailInner().children.count() > 0; 482 const content_focused = if (root_focus.grandchild_id) |g| self.detailInner().children.contains(g) else false; 483 self.detailScrollFrame().options.border_style = if (!has_content) null else if (content_focused) .double else .single; 484 485 // cap the list at list_max_width only while the detail pane fits beside 486 // it. the box drops the detail pane when the width can't hold both 487 // minimums, so when it's that narrow we lift the cap and let the list 488 // fill the whole width. 489 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + detail_min_width else true; 490 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 491 492 // stretch the detail pane and its bordered scroll frame across the rest 493 // of the width so the pane fills the area rather than shrinking to its 494 // content, like the refs view's columns. the content scroll inside keeps 495 // its natural width: forcing it to the full inner width would make it one 496 // column too wide whenever the vertical scrollbar appears, leaving a 497 // phantom horizontal scroll. (height fills via the scroll clipping to the 498 // viewport.) 499 if (constraint.max_size.width) |w| { 500 const detail_w = if (both_panes_fit) w - list_max_width else w; 501 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_w, .height = null }; 502 self.detailOuter().children.values()[detail_scroll_index].min_size = .{ .width = detail_w, .height = null }; 503 } else { 504 self.contentBox().children.values()[detail_index].min_size = .{ .width = detail_min_width, .height = null }; 505 self.detailOuter().children.values()[detail_scroll_index].min_size = null; 506 } 507 508 // crossing panes only re-selects the content box's child (focusList / 509 // focusDetail); when the window is too narrow to show both, the pane that 510 // was holding focus is dropped here. the framework recovers focus after 511 // the top-level build by re-deriving it down the selected-child chain, so 512 // there's nothing to fix up afterward. 513 try self.box.build(allocator, constraint, root_focus); 514 515 // clamp the list's initial offset (a selected row near the end of the 516 // list) against this build's grids, for the next frame. initRoot's 517 // pre-sizing build is unbounded — its full-content "viewport" would 518 // clamp the offset to zero, so only a bounded build counts. 519 if (constraint.max_size.height != null) self.listScroll().clampToContent(); 520 } 521 522 fn refreshDetail(self: *View, allocator: std.mem.Allocator) !void { 523 const cur = self.selectedRowIndex(); 524 if (std.meta.eql(cur, self.shown_index)) return; 525 try self.populateDetail(allocator); 526 self.shown_index = cur; 527 } 528 529 fn populateDetail(self: *View, allocator: std.mem.Allocator) !void { 530 const nav_box = self.navBox(); 531 const inner = self.detailInner(); 532 533 for (nav_box.children.values()) |*child| child.widget.deinit(allocator); 534 nav_box.children.clearAndFree(allocator); 535 nav_box.getFocus().child_id = null; 536 for (inner.children.values()) |*child| child.widget.deinit(allocator); 537 inner.children.clearAndFree(allocator); 538 inner.getFocus().child_id = null; 539 540 // only files have contents to show; directories and the ".." row leave 541 // the pane empty. 542 if (self.selectedEntry()) |entry| { 543 if (!entry.is_dir) { 544 // the window links sit above the scroll, so they stay put 545 // while the content scrolls. 546 if (entry.window_start > 0) try self.addNavLink(allocator, nav_box, "scroll to top", entry, 0); 547 if (entry.has_more) try self.addNavLink(allocator, nav_box, "next lines →", entry, lineNumber(entry.window_start + file_page)); 548 if (!entry.loaded) { 549 // the placeholder carries the file's "a:" link, so enter 550 // (or a click) here loads the file like on its list row. 551 const path = try childDir(self.session.page_arena.allocator(), self.data.dir, entry.name); 552 const link = try fileLink(self.session.page_arena, self.data, path, false); 553 try self.addContentBox(allocator, inner, "(press enter to load this file)", link); 554 } else if (entry.is_binary) { 555 try self.addContentBox(allocator, inner, "(binary file)", ""); 556 } else { 557 const text = try numberedContent(self.session.page_arena.allocator(), entry.lines, entry.window_start); 558 try self.addContentBox(allocator, inner, text, ""); 559 } 560 } 561 } 562 // point each box at its first child so focus can descend into it. 563 if (nav_box.children.count() > 0) nav_box.getFocus().child_id = nav_box.children.keys()[0]; 564 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 565 566 // reserve the nav row's height (the shrink scroll frame yields it). 567 self.detailOuter().children.values()[detail_nav_index].min_size = 568 if (nav_box.children.count() > 0) .{ .width = null, .height = row_height } else null; 569 570 // reset the scroll to the top for the newly-shown file: directly on the 571 // terminal (the wasm offset), and via a version bump on the web (so the 572 // renderer's scroll id changes and JS drops the preserved position). 573 const sc = self.detailScroll(); 574 sc.x = 0; 575 sc.y = 0; 576 sc.getFocus().version +%= 1; 577 } 578 579 // a focusable "next" link above the content. it's an `a:` link to the 580 // selected file at `target_start`, so activating it (the host follows the 581 // link) reloads the page on the next window. 582 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, entry: Entry, target_start: usize) !void { 583 const page_arena = self.session.page_arena; 584 const path = try childDir(page_arena.allocator(), self.data.dir, entry.name); 585 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; 586 const link = try std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 587 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 588 errdefer tb.deinit(allocator); 589 tb.getFocus().focusable = true; 590 tb.getFocus().kind = .{ .custom = link }; 591 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 592 } 593 594 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 595 if (self.headerActive()) { 596 if (key == .arrow_down) { 597 root_focus.setFocus(self.contentBox().getFocus().id); 598 } else { 599 try self.header().input(allocator, key, root_focus); 600 } 601 return; 602 } 603 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 604 if (self.detailActive()) { 605 try self.detailInput(key, root_focus); 606 } else { 607 try self.listInput(key, root_focus); 608 } 609 } 610 611 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 612 // up/down (and the scroll wheel) move the selection; page up/down jump a 613 // fixed amount. right/Enter cross into the detail pane. Enter/clicks on a 614 // directory (or "..") row are turned into navigation by the host 615 // (crossPageLink) before reaching here. 616 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 617 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 618 return; 619 } 620 switch (key) { 621 .enter, .arrow_right => try self.focusDetail(root_focus), 622 else => {}, 623 } 624 } 625 626 // whether focus is on the content box (vs. a "previous"/"next" nav link). 627 fn focusOnContent(self: *View, root_focus: *Focus) bool { 628 const g = root_focus.grandchild_id orelse return false; 629 return self.detailInner().children.contains(g); 630 } 631 632 fn detailInput(self: *View, key: Key, root_focus: *Focus) !void { 633 const sc = self.detailScroll(); 634 const on_content = self.focusOnContent(root_focus); 635 // the "next" link sits above the content scroll. on the link, left returns 636 // to the file list and down drops into the content. in the content, the 637 // terminal scrolls by offset and reaching the top then pressing up crosses 638 // back up to the link; on the web vertical scrolling is the browser's job 639 // so up just crosses to the link. 640 switch (key) { 641 .arrow_left => { 642 if (on_content) { 643 if (sc.x > 0) { 644 sc.x -= 1; 645 self.detailScroll().clampToContent(); 646 } else try self.focusList(root_focus); 647 } else if (!self.moveNav(root_focus, -1)) { 648 try self.focusList(root_focus); 649 } 650 }, 651 .arrow_right => { 652 if (on_content) { 653 sc.x += 1; 654 self.detailScroll().clampToContent(); 655 } else _ = self.moveNav(root_focus, 1); 656 }, 657 .arrow_up => { 658 if (on_content) { 659 if (self.session.is_terminal and sc.y > 0) { 660 sc.y -= 1; 661 self.detailScroll().clampToContent(); 662 } else self.focusNav(root_focus); // cross up to the links 663 } 664 }, 665 .arrow_down => { 666 if (on_content) { 667 if (self.session.is_terminal) { 668 sc.y += 1; 669 self.detailScroll().clampToContent(); 670 } 671 } else self.focusContent(root_focus); // links -> content 672 }, 673 .page_up => if (on_content and self.session.is_terminal) { 674 sc.y -= 10; 675 self.detailScroll().clampToContent(); 676 }, 677 .page_down => if (on_content and self.session.is_terminal) { 678 sc.y += 10; 679 self.detailScroll().clampToContent(); 680 }, 681 .home => if (on_content and self.session.is_terminal) { 682 sc.y = 0; 683 self.detailScroll().clampToContent(); 684 }, 685 .end => if (on_content and self.session.is_terminal) { 686 sc.y = std.math.maxInt(isize); 687 self.detailScroll().clampToContent(); 688 }, 689 .mouse => |mouse| switch (mouse.action) { 690 .scroll => |dir| if (on_content and self.session.is_terminal) { 691 sc.y += if (dir == .up) -5 else 5; 692 self.detailScroll().clampToContent(); 693 }, 694 else => {}, 695 }, 696 else => {}, 697 } 698 } 699 700 // focus the first (left-most) nav link; a no-op when there are none, leaving 701 // focus on the content. 702 fn focusNav(self: *View, root_focus: *Focus) void { 703 const nav_box = self.navBox(); 704 if (nav_box.children.count() == 0) return; 705 root_focus.setFocus(nav_box.children.keys()[0]); 706 } 707 708 fn focusContent(self: *View, root_focus: *Focus) void { 709 const inner = self.detailInner(); 710 const id = inner.getFocus().child_id orelse (if (inner.children.count() > 0) inner.children.keys()[0] else return); 711 root_focus.setFocus(id); 712 } 713 714 // move focus `delta` rows within the nav box; returns false (without moving) 715 // when that would step off either end. 716 fn moveNav(self: *View, root_focus: *Focus, delta: isize) bool { 717 const nav_box = self.navBox(); 718 const keys = nav_box.children.keys(); 719 const cur_id = nav_box.getFocus().child_id orelse return false; 720 const cur: isize = @intCast(nav_box.children.getIndex(cur_id) orelse return false); 721 const target = cur + delta; 722 if (target < 0 or target >= @as(isize, @intCast(keys.len))) return false; 723 root_focus.setFocus(keys[@intCast(target)]); 724 return true; 725 } 726 727 // enter the detail pane, landing on the "next" link when there is one (so 728 // right-arrow reaches it), else on the content. a directory or ".." row has 729 // neither, so there's nothing to enter. selecting the frame's child (then 730 // focusing the frame) also handles the too-narrow case where the pane isn't 731 // laid out yet — focus lands inside it after the next build. 732 fn focusDetail(self: *View, root_focus: *Focus) !void { 733 const frame = self.detailOuter(); 734 if (self.navBox().children.count() > 0) { 735 frame.getFocus().child_id = self.navBox().getFocus().id; 736 } else if (self.detailInner().children.count() > 0) { 737 frame.getFocus().child_id = self.detailScrollFrame().getFocus().id; 738 } else return; 739 root_focus.setFocus(frame.getFocus().id); 740 } 741 742 // return to the list. 743 fn focusList(self: *View, root_focus: *Focus) !void { 744 root_focus.setFocus(self.listScroll().getFocus().id); 745 } 746 747 pub fn clearGrid(self: *View) void { 748 self.box.clearGrid(); 749 } 750 751 pub fn getGrid(self: View) ?Grid { 752 return self.box.getGrid(); 753 } 754 755 pub fn getFocus(self: *View) *Focus { 756 return self.box.getFocus(); 757 } 758 759 fn contentAtTop(self: *View) bool { 760 if (self.detailActive()) { 761 if (self.detailOuter().getFocus().child_id == self.navBox().getFocus().id) return true; 762 return self.navBox().children.count() == 0 and 763 (!self.session.is_terminal or self.detailScroll().y == 0); 764 } 765 const lb = self.listBox(); 766 const cid = lb.getFocus().child_id orelse return true; 767 return lb.children.getIndex(cid) == 0; 768 } 769 770 // only the clone-url row sits directly below the repository header. 771 pub fn getSelectedIndex(self: *View) ?usize { 772 if (self.headerActive()) return 0; 773 return if (self.header().hasCloneUrl() or !self.contentAtTop()) 1 else 0; 774 } 775 776 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 777 return self.header().focusCloneUrl(root_focus); 778 } 779 }; 780 781 // join `lines` into one string, each prefixed with its 1-based file line number 782 // (right-aligned in a column wide enough for the last number, then a space), 783 // like the diffs view. `start` is the window's 0-based first line, so numbers 784 // continue across paginated windows. 785 fn numberedContent(arena: std.mem.Allocator, lines: []const []const u8, start: usize) ![]const u8 { 786 if (lines.len == 0) return ""; 787 const width = std.fmt.count("{d}", .{start + lines.len}); 788 const indent = try arena.alloc(u8, width); 789 @memset(indent, ' '); 790 791 var out: std.ArrayList(u8) = .empty; 792 for (lines, 0..) |line, i| { 793 if (i != 0) try out.append(arena, '\n'); 794 const num_str = try std.fmt.allocPrint(arena, "{d}", .{start + i + 1}); 795 try out.appendSlice(arena, indent[0 .. width - num_str.len]); 796 try out.appendSlice(arena, num_str); 797 try out.append(arena, ' '); 798 try out.appendSlice(arena, line); 799 } 800 return out.toOwnedSlice(arena); 801 } 802 803 // the index of the "README"/"README.md" file entry (case-insensitive), or null 804 // if none. used to pick the default selection at the repo root. 805 fn readmeIndex(entries: []const Entry) ?usize { 806 for (entries, 0..) |entry, i| { 807 if (!entry.is_dir and isReadme(entry.name)) return i; 808 } 809 return null; 810 } 811 812 fn isReadme(name: []const u8) bool { 813 return std.ascii.eqlIgnoreCase(name, "README") or std.ascii.eqlIgnoreCase(name, "README.md"); 814 } 815 816 // the index of the entry the route named (data.selected_file), or null if none. 817 fn selectedFileIndex(data: *const Self) ?usize { 818 const name = data.selected_file orelse return null; 819 for (data.entries, 0..) |entry, i| { 820 if (!entry.is_dir and std.mem.eql(u8, entry.name, name)) return i; 821 } 822 return null; 823 } 824 825 // an "a:" link to the files route at `path`, pinned to the listing's ref, so 826 // following it navigates to that directory's listing. 827 fn dirLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8) ![]const u8 { 828 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 829 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{try route.toUrl(page_arena)}); 830 } 831 832 // a link to the file's route. a preloaded file gets an "ai:" link 833 // (crossPageLink ignores it, so a click selects the row in place and shows its 834 // contents); an unloaded file gets an "a:" link, so activating it reloads the 835 // page with that file selected and its content read. 836 fn fileLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, path: []const u8, loaded: bool) ![]const u8 { 837 const route = ui.RoutablePage.repoFilesRoute(data.identity, data.ref_or_oid, data.ref_or_oid_value, path, 0) orelse return error.RouteTooLong; 838 const prefix = if (loaded) "ai:" else "a:"; 839 return std.fmt.allocPrint(page_arena.allocator(), "{s}{s}", .{ prefix, try route.toUrl(page_arena) }); 840 } 841 842 // the url `line` value for a content window: its first line's 1-based number, 843 // except the first window, which serializes as unset (0). 844 fn lineNumber(window_start: usize) usize { 845 return if (window_start == 0) 0 else window_start + 1; 846 } 847 848 // `dir`/`name`, or just `name` at the root. 849 pub fn childDir(arena: std.mem.Allocator, dir: []const u8, name: []const u8) ![]const u8 { 850 return if (dir.len == 0) name else std.fmt.allocPrint(arena, "{s}/{s}", .{ dir, name }); 851 } 852 853 // `dir` with its last segment removed ("" when `dir` has a single segment). 854 fn parentDir(dir: []const u8) []const u8 { 855 const slash = std.mem.lastIndexOfScalar(u8, dir, '/') orelse return ""; 856 return dir[0..slash]; 857 } 858 859 // the "<ref_or_oid> <value>" banner shown above the listing. 860 pub const Header = struct { 861 content: []const u8, 862 863 // `value` arrives url-encoded, so decode it for display. 864 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 865 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 866 return .{ 867 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 868 }; 869 } 870 871 pub const View = struct { 872 box: wgt.Box(ui.Widget), 873 data: *const Header, 874 875 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, identity: []const u8) !Header.View { 876 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 877 errdefer box.deinit(allocator); 878 879 if (!session.data.is_local and (session.data.clone_http_port != null or session.data.clone_ssh_port != null)) { 880 var clone_url = try ui.CloneUrl.View.init(allocator, session, identity); 881 errdefer clone_url.deinit(allocator); 882 const min_width = clone_url.minWidth(); 883 box.getFocus().child_id = clone_url.getFocus().id; 884 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .clone_url = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 } }); 885 } 886 887 var spacer = try ui.Spacer.init(allocator); 888 errdefer spacer.deinit(allocator); 889 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 890 891 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 892 errdefer text_box.deinit(allocator); 893 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .shrink = true }); 894 895 return .{ .box = box, .data = data }; 896 } 897 898 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 899 self.box.deinit(allocator); 900 } 901 902 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 903 self.clearGrid(); 904 try self.box.build(allocator, constraint, root_focus); 905 } 906 907 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 908 const clone_url = self.cloneUrl() orelse return; 909 try clone_url.input(allocator, key, root_focus); 910 } 911 912 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 913 const clone_url = self.cloneUrl() orelse return false; 914 root_focus.setFocus(clone_url.getFocus().id); 915 return true; 916 } 917 918 pub fn hasCloneUrl(self: *Header.View) bool { 919 return self.cloneUrl() != null; 920 } 921 922 fn cloneUrl(self: *Header.View) ?*ui.CloneUrl.View { 923 for (self.box.children.values()) |*child| switch (child.widget) { 924 .clone_url => |*clone_url| return clone_url, 925 else => {}, 926 }; 927 return null; 928 } 929 930 pub fn clearGrid(self: *Header.View) void { 931 self.box.clearGrid(); 932 } 933 934 pub fn getGrid(self: Header.View) ?Grid { 935 return self.box.getGrid(); 936 } 937 938 pub fn getFocus(self: *Header.View) *Focus { 939 return self.box.getFocus(); 940 } 941 }; 942 }; 943