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