╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
326 .message => .message, 327 }; 328 } 329 330 // render the window [start, start+len) of a commit's diff against its first 331 // parent into `arena`-owned hunks. start/has_more identify adjacent windows. 332 // a non-empty `path` filters to that file, so the window indexes its hunks. 333 fn renderCommitDiff( 334 comptime repo_kind: rp.RepoKind, 335 comptime repo_opts: rp.RepoOpts(repo_kind), 336 io: std.Io, 337 gpa: std.mem.Allocator, 338 arena: std.mem.Allocator, 339 repo: *rp.Repo(repo_kind, repo_opts), 340 oid: [xit.hash.hexLen(repo_opts.hash)]u8, 341 start: usize, 342 path: []const u8, 343 ) !Diff.Window { 344 const empty = Diff.Window{ .start = start }; 345 346 // load the commit so we can diff it against its first parent. 347 var commit_iter = repo.log(io, gpa, .{ .start_oids = &.{oid} }) catch return empty; 348 defer commit_iter.deinit(); 349 const commit_object = (commit_iter.next(gpa) catch return empty) orelse return empty; 350 defer commit_object.deinit(); 351 352 const parent_maybe = commit_object.content.commit.metadata.firstParent(); 353 354 return Diff.render(repo_kind, repo_opts, io, gpa, arena, repo, parent_maybe, oid, start, path); 355 } 356 357 // "YYYY-MM-DD" for a unix timestamp. 358 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 359 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 360 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 361 const month_day = year_day.calculateMonthDay(); 362 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 363 year_day.year, 364 month_day.month.numeric(), 365 month_day.day_index + 1, 366 }); 367 } 368 369 // the commit message, trimmed and read into `aa`, plus whether its preview was 370 // cut short. 371 fn readMessage( 372 comptime repo_kind: rp.RepoKind, 373 comptime repo_opts: rp.RepoOpts(repo_kind), 374 aa: std.mem.Allocator, 375 commit_object: *xit.object.Object(repo_kind, repo_opts), 376 full_message: bool, 377 ) !struct { []const u8, bool } { 378 var message: std.ArrayList(u8) = .empty; 379 const byte_truncated = if (commit_object.readMessage(aa, &message, .limited(max_message_size))) |_| 380 false 381 else |err| switch (err) { 382 error.StreamTooLong => true, 383 else => |e| return e, 384 }; 385 const preview_end = if (full_message) null else ui.detailPreviewEnd(message.items); 386 const text = if (preview_end) |end| 387 message.items[0..end] 388 else if (byte_truncated) 389 trimIncompleteCodepoint(message.items) 390 else 391 message.items; 392 return .{ std.mem.trim(u8, text, " \t\r\n"), byte_truncated or preview_end != null }; 393 } 394 395 /// drop any incomplete UTF data from the end of a byte array 396 fn trimIncompleteCodepoint(bytes: []const u8) []const u8 { 397 var i = bytes.len; 398 while (i > 0 and bytes.len - i < 4) { 399 i -= 1; 400 const byte = bytes[i]; 401 if (byte & 0b1100_0000 == 0b1000_0000) continue; // continuation byte 402 const seq_len = std.unicode.utf8ByteSequenceLength(byte) catch return bytes; 403 return if (i + seq_len > bytes.len) bytes[0..i] else bytes; 404 } 405 return bytes; 406 } 407 408 // a focusable, word-wrapping box holding a commit message. `bottom_label` 409 // marks a message the page only shows part of ("" when it's whole). 410 fn messageBox(allocator: std.mem.Allocator, message: []const u8, bottom_label: []const u8) !wgt.TextBox { 411 var tb = try wgt.TextBox.init(allocator, message, .{ 412 .border_style = .single, 413 .rounded_corners = true, 414 .wrap_kind = .word, 415 .label = " message ", 416 .bottom_label = bottom_label, 417 }); 418 tb.getFocus().mode = .all; 419 return tb; 420 } 421 422 // the first line of a commit message, for the one-row list entries. 423 fn firstLine(message: []const u8) []const u8 { 424 const nl = std.mem.indexOfScalar(u8, message, '\n'); 425 return if (nl) |i| std.mem.trimEnd(u8, message[0..i], " \t\r") else message; 426 } 427 428 pub const View = struct { 429 // a vertical stack: the clone url row on top, then a horizontal 430 // split with the commit list on the left and a diff pane on the right 431 // showing the selected commit's diff. 432 box: wgt.Box(ui.Widget), // vert: [header_index] = clone url row, [content_index] = split 433 data: *const Self, 434 session: *ui.Session, 435 // the commit whose diff the pane currently shows (index into data.commits). 436 diffed_index: ?usize, 437 438 // the sub-header leads the box where there is one; local mode has none 439 const header_index: usize = 0; 440 // indices within the content box (the horizontal split). 441 const list_index: usize = 0; 442 const diff_index: usize = 1; 443 const list_max_width: usize = 35; 444 const diff_min_width: usize = 40; 445 446 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 447 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 448 errdefer outer.deinit(allocator); 449 450 // the search box and the clone url at the top. local mode has neither. 451 if (session.data.host_kind == .server) { 452 var header_view = try ui.widget.SearchHeader.init(allocator, session, data.location, " search ", "search", data.search, data.search_available); 453 errdefer header_view.deinit(allocator); 454 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .search_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 455 } 456 457 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 458 errdefer box.deinit(allocator); 459 460 // the commit list (one focusable row each), plus a "next" link 461 { 462 var list_scroll = blk: { 463 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 464 errdefer list_box.deinit(allocator); 465 for (data.commits, 0..) |commit, index| { 466 // an in-page "ai:" anchor so a commit row is clickable with 467 // js off (the browser follows it, rooting the list there); 468 // with wasm the click just selects it and swaps the diff pane. 469 const content = if (index == 0) data.content else Content{ .diff = .{} }; 470 try addRow(allocator, &list_box, firstLine(commit.message), try commitRowLink(session.page_arena, data, commit, content), if (commit.merge) "(merge)" else ""); 471 } 472 if (data.next_start) |next| { 473 try addRow(allocator, &list_box, "next →", try nextPageLink(session.page_arena, data, next), ""); 474 } 475 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 476 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 477 }; 478 errdefer list_scroll.deinit(allocator); 479 try box.children.put(allocator, list_scroll.getFocus().id, .{ .widget = .{ .scroll = list_scroll }, .rect = null, .min_size = .{ .width = list_max_width, .height = null }, .max_size = .{ .width = list_max_width, .height = null } }); 480 } 481 482 // the diff pane — a frame around a scroll of the hunks 483 { 484 var diff_outer = blk: { 485 var diff_scroll = try Diff.View.initEmpty(allocator, session); 486 errdefer diff_scroll.deinit(allocator); 487 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 488 errdefer frame.deinit(allocator); 489 // the frame's selected child is its scroll, so the focus chain 490 // reaches a hunk (populateDiff points the scroll's inner box at 491 // one), letting focus recovery descend into the diff pane after 492 // it's laid out beside a too-narrow list. 493 frame.getFocus().child_id = diff_scroll.getFocus().id; 494 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .diff_view = diff_scroll }, .rect = null, .min_size = null }); 495 break :blk frame; 496 }; 497 errdefer diff_outer.deinit(allocator); 498 diff_outer.getFocus().mode = .mouse; 499 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 500 } 501 502 box.getFocus().child_id = box.children.keys()[list_index]; 503 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 504 505 // focus lives in the split, except that search results start in the 506 // search box so the query can be refined right away. 507 outer.getFocus().child_id = outer.children.keys()[if (data.search != null) header_index else outer.children.count() - 1]; 508 509 return .{ 510 .box = outer, 511 .data = data, 512 .session = session, 513 .diffed_index = null, 514 }; 515 } 516 517 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8, bottom_label: []const u8) !void { 518 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word, .bottom_label = bottom_label }); 519 errdefer row.deinit(allocator); 520 row.getFocus().mode = .all; 521 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 522 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 523 } 524 525 // a focusable row following the "a:" `link`. 526 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 527 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 528 errdefer tb.deinit(allocator); 529 tb.getFocus().mode = .all; 530 tb.getFocus().kind = .{ .custom = link }; 531 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 532 } 533 534 // how much of the message a pane's box holds: a truncated preview links to 535 // the pane holding the whole thing, which is that link's destination. 536 const MessageBox = enum { preview, whole }; 537 538 // the selected commit's message. 539 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 540 // a truncated message keeps its box even with no whole line to show, 541 // since the box is what links to the whole thing. 542 if (commit.message.len == 0 and !commit.message_truncated) return; 543 const cut_short = commit.message_truncated and kind == .preview; 544 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 545 errdefer tb.deinit(allocator); 546 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data, commit.oid) }; 547 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 548 } 549 550 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 551 self.box.deinit(allocator); 552 } 553 554 // the split is last, after the sub-header where there is one 555 fn contentIndex(self: *View) usize { 556 return self.box.children.count() - 1; 557 } 558 559 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 560 return &self.box.children.values()[self.contentIndex()].widget.box; 561 } 562 563 fn header(self: *View) ?*ui.widget.SearchHeader { 564 switch (self.box.children.values()[header_index].widget) { 565 .search_header => |*header_view| return header_view, 566 else => return null, 567 } 568 } 569 570 fn headerActive(self: *View) bool { 571 const header_view = self.header() orelse return false; 572 return self.box.getFocus().child_id == header_view.getFocus().id; 573 } 574 575 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 576 return &self.contentBox().children.values()[list_index].widget.scroll; 577 } 578 579 fn listBox(self: *View) *wgt.Box(ui.Widget) { 580 return &self.listScroll().child.box; 581 } 582 583 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 584 return &self.contentBox().children.values()[diff_index].widget.box; 585 } 586 587 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 588 return &self.diffOuter().children.values()[0].widget.diff_view.scroll; 589 } 590 591 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 592 return &self.diffScroll().child.box; 593 } 594 595 fn diffActive(self: *View) bool { 596 const content = self.contentBox(); 597 const cid = content.getFocus().child_id orelse return false; 598 return content.children.getIndex(cid) == diff_index; 599 } 600 601 // what the pane shows for the commit at `sel`. the page's content only 602 // applies to the commit it walks from; the rest show their plain diff. 603 fn paneContent(self: *View, sel: usize) Self.Content { 604 return if (sel == 0) self.data.content else .{ .diff = .{} }; 605 } 606 607 // the selected commit's index, or null when the "next" row is selected. 608 fn selectedCommitIndex(self: *View) ?usize { 609 const lb = self.listBox(); 610 const cid = lb.getFocus().child_id orelse return null; 611 const idx = lb.children.getIndex(cid) orelse return null; 612 if (idx >= self.data.commits.len) return null; 613 return idx; 614 } 615 616 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 617 self.clearGrid(); 618 619 // swap the diff pane to the selected commit when it changes. 620 try self.refreshDiff(allocator); 621 622 // the selected list row shows a border (the focused TextBox upgrades it 623 // to a double border itself); the rest stay borderless. 624 const lb = self.listBox(); 625 for (lb.children.keys(), lb.children.values()) |id, *child| { 626 switch (child.widget) { 627 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 628 else => {}, 629 } 630 } 631 632 // cap the list at list_max_width only while the diff pane fits beside it. 633 // the box drops the diff when the width can't hold both minimums, so when 634 // it's that narrow we lift the cap and let the list fill the whole width. 635 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 636 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 637 638 // stretch the diff pane across the rest of the width so it fills the area 639 // rather than shrinking to its content; its scroll fills the pane. when 640 // too narrow for both, it fills the whole width. 641 const diff_width: usize = if (constraint.max_size.width) |w| 642 (if (both_panes_fit) w - list_max_width else w) 643 else 644 diff_min_width; 645 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 646 647 // the message is the pane's only wrapping row, and wrapping needs a 648 // bounded width, which the pane's scroll doesn't grant (it scrolls 649 // horizontally too). cap it to the pane, leaving room for the border 650 // and the scrollbar column. re-capped each build so it tracks resizes. 651 for (self.diffInner().children.values()) |*child| switch (child.widget) { 652 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 653 child.max_size = .{ .width = diff_width -| 3, .height = null }; 654 }, 655 else => {}, 656 }; 657 658 // the web bounds the layout to the browser viewport like the terminal; 659 // each Scroll's web-native mode hands its full content to a real 660 // scrollable element, so we no longer build unbounded here. 661 // 662 // crossing panes only re-selects the content box's child (focusDiff / 663 // focusList); when the window is too narrow to show both, the pane that 664 // held focus is dropped here. the framework recovers focus after the 665 // top-level build by re-deriving it down the selected-child chain, so 666 // there's nothing to fix up afterward. 667 try self.box.build(allocator, constraint, root_focus); 668 } 669 670 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 671 const sel = self.selectedCommitIndex() orelse return; 672 if (self.diffed_index) |d| if (d == sel) return; 673 try self.populateDiff(allocator, sel); 674 self.diffed_index = sel; 675 } 676 677 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 678 const commit = self.data.commits[sel]; 679 const inner = self.diffInner(); 680 681 for (inner.children.values()) |*child| child.widget.deinit(allocator); 682 inner.children.clearAndFree(allocator); 683 inner.getFocus().child_id = null; 684 685 switch (self.paneContent(sel)) { 686 .message => { 687 try addLink(allocator, inner, "← back to diff", try commitsLink(self.session.page_arena, self.data, commit.oid, 0, "")); 688 try self.addMessageBox(allocator, inner, commit, .whole); 689 }, 690 .diff => |d| { 691 if (d.path.len == 0) { 692 try self.addMessageBox(allocator, inner, commit, .preview); 693 if (commit.author != .unknown or commit.committer != .unknown) { 694 var row = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 695 errdefer row.deinit(allocator); 696 if (commit.author != .unknown) { 697 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 698 errdefer tb.deinit(allocator); 699 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 700 } 701 if (commit.committer != .unknown) { 702 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.committer); 703 errdefer tb.deinit(allocator); 704 tb.options.label = " committer "; 705 try row.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 706 } 707 row.getFocus().child_id = row.children.keys()[0]; 708 try inner.children.put(allocator, row.getFocus().id, .{ .widget = .{ .box = row }, .rect = null, .min_size = null }); 709 } 710 { 711 var tb = try wgt.TextBox.init(allocator, commit.timestamp, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " timestamp " }); 712 errdefer tb.deinit(allocator); 713 tb.getFocus().mode = .all; 714 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 715 } 716 if (commit.merge) if (self.data.location.commitsMergeRoute(commit.oid)) |route| { 717 try addLink(allocator, inner, "view commits from this merge", try std.fmt.allocPrint(self.session.page_arena.allocator(), "a:{s}", .{try route.toUrl(self.session.page_arena)})); 718 }; 719 if (commit.stats) |stats| { 720 var text: std.Io.Writer.Allocating = .init(allocator); 721 defer text.deinit(); 722 try text.writer.print("lines changed: {d}", .{stats.lines_added + stats.lines_changed}); 723 if (stats.lines_removed != 0) try text.writer.print("\nlines removed: {d}", .{stats.lines_removed}); 724 const bytes_added = stats.bytes_added >= stats.bytes_removed; 725 const bytes = if (bytes_added) stats.bytes_added - stats.bytes_removed else stats.bytes_removed - stats.bytes_added; 726 try text.writer.print("\nbytes {s}: {d}\nfiles changed: {d}", .{ 727 if (bytes_added) "added" else "removed", bytes, stats.files_added + stats.files_changed, 728 }); 729 if (stats.files_removed != 0) try text.writer.print("\nfiles removed: {d}", .{stats.files_removed}); 730 var tb = try wgt.TextBox.init(allocator, text.written(), .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none, .label = " stats " }); 731 errdefer tb.deinit(allocator); 732 tb.getFocus().mode = .all; 733 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 734 } 735 try addLink(allocator, inner, "view files at this commit", try filesObjectLink(self.session.page_arena, self.data.location, commit.oid)); 736 } 737 738 try (Diff{ 739 .route = .{ .commit = .{ .location = self.data.location, .oid = commit.oid, .base_oid = self.data.base_oid } }, 740 .path = d.path, 741 .window = commit.window, 742 }).appendWindow(allocator, self.session, inner); 743 }, 744 } 745 746 // point the pane at its first row so focus recovery can land here. 747 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 748 749 // reset the scroll to the top for the newly-shown commit: directly on the 750 // terminal (the wasm offset), and via a version bump on the web (so the 751 // renderer's scroll id changes and JS drops the preserved position). 752 const sc = self.diffScroll(); 753 sc.x = 0; 754 sc.y = 0; 755 sc.getFocus().version +%= 1; 756 } 757 758 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 759 // scrolling crosses the sub header boundary the same way arrows do 760 const direction = inp.vertDirection(key); 761 if (self.headerActive()) { 762 // active means there is one 763 const header_view = self.header() orelse unreachable; 764 if (direction == .down) { 765 root_focus.setFocus(self.contentBox().getFocus().id); 766 return; 767 } 768 if (key == .enter) if (try header_view.submittedText(allocator)) |text| { 769 defer allocator.free(text); 770 return self.submit(text); 771 }; 772 return header_view.input(allocator, key, root_focus); 773 } 774 if (direction == .up and self.contentAtTop() and self.focusHeader(root_focus)) return; 775 if (self.diffActive()) { 776 const diff_view = &self.diffOuter().children.values()[0].widget.diff_view; 777 if (key == .arrow_left and self.diffScroll().x == 0) { 778 if (!diff_view.moveInRow(root_focus, false)) self.focusList(root_focus); 779 } else { 780 try diff_view.input(allocator, key, root_focus); 781 } 782 } else { 783 try self.listInput(key, root_focus); 784 } 785 } 786 787 // navigate to the typed query's results, pinned to the list's ref. an 788 // empty query leaves the results for the plain log. 789 fn submit(self: *View, text: []const u8) !void { 790 if (text.len == 0 and self.data.search == null) return; 791 const route = self.data.location.commitsRoute(self.data.ref_or_oid, self.data.ref_or_oid_value, 0, "", self.data.base_oid) orelse return; 792 try self.session.navigate(route.withSearch(text) orelse return); 793 } 794 795 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 796 // up/down (and the scroll wheel) move the selection a row; page up/down 797 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 798 // on the "next" row become navigation in the host before reaching here. 799 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 800 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 801 return; 802 } 803 switch (key) { 804 .enter => if (self.selectedCommitIndex() != null) 805 self.focusDiff(root_focus) 806 else if (self.data.next_start) |next| { 807 if (pageRoute(self.data, next)) |route| try self.session.navigate(route); 808 }, 809 .arrow_right => self.focusDiff(root_focus), 810 else => {}, 811 } 812 } 813 814 // enter the diff pane. the host arrives here on right-arrow or Enter from the 815 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 816 // case where the pane isn't laid out yet (it gets selected, then focused after 817 // the next build, landing on its remembered hunk). 818 fn focusDiff(self: *View, root_focus: *Focus) void { 819 if (self.diffInner().children.count() == 0) return; 820 root_focus.setFocus(self.diffOuter().getFocus().id); 821 } 822 823 // return to the list. 824 fn focusList(self: *View, root_focus: *Focus) void { 825 root_focus.setFocus(self.listScroll().getFocus().id); 826 } 827 828 pub fn clearGrid(self: *View) void { 829 self.box.clearGrid(); 830 } 831 832 pub fn getGrid(self: View) ?Grid { 833 return self.box.getGrid(); 834 } 835 836 pub fn getFocus(self: *View) *Focus { 837 return self.box.getFocus(); 838 } 839 840 fn contentAtTop(self: *View) bool { 841 if (self.diffActive()) { 842 const inner = self.diffInner(); 843 const cid = inner.getFocus().child_id orelse return true; 844 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 845 return first_focused and self.diffScroll().y == 0; 846 } 847 const lb = self.listBox(); 848 const cid = lb.getFocus().child_id orelse return true; 849 return lb.children.getIndex(cid) == 0; 850 } 851 852 // only the header's own row sits directly below the repository header. 853 pub fn atTop(self: *View) bool { 854 const focusable = if (self.header()) |header_view| header_view.hasFocusable() else false; 855 return self.headerActive() or (!focusable and self.contentAtTop()); 856 } 857 858 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 859 const header_view = self.header() orelse return false; 860 return header_view.focusHeader(root_focus); 861 } 862 }; 863 864 // the "a:" navigation link for the commits page walking from commit `oid` within 865 // `data.location`, windowing the selected commit's diff from hunk `start`, filtered 866 // to `path` ("" = every file). 867 fn commitsLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 868 const route = data.location.commitsRoute(.object, oid, start, path, data.base_oid) orelse return error.RouteTooLong; 869 const url = try route.toUrl(page_arena); 870 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 871 } 872 873 // this list's page starting at `oid`: a repo's branch or tag keeps its ref (and 874 // its query) and moves `from` along, so the search box survives paging. a fork 875 // or an object view roots the page at the commit itself. 876 fn pageRoute(data: *const Self, oid: []const u8) ?ui.RoutablePage { 877 if (data.location != .repo or data.ref_or_oid == .object) { 878 return data.location.commitsRoute(.object, oid, 0, "", data.base_oid); 879 } 880 const route = data.location.commitsRoute(data.ref_or_oid, data.ref_or_oid_value, 0, "", data.base_oid) orelse return null; 881 const paged = route.withFrom(oid) orelse return null; 882 return paged.withSearch(data.search orelse return paged); 883 } 884 885 fn nextPageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 886 const route = pageRoute(data, oid) orelse return error.RouteTooLong; 887 const url = try route.toUrl(page_arena); 888 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 889 } 890 891 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 892 // directory, within `location`. 893 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, location: ui.RoutablePage.RepoLocation, oid: []const u8) ![]const u8 { 894 const route = location.filesRoute(.object, oid, "", 0) orelse return error.RouteTooLong; 895 const url = try route.toUrl(page_arena); 896 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 897 } 898 899 // the in-page "ai:" anchor for selecting a commit with its current pane content 900 // and window. the href is only followed with js off. 901 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, commit: Commit, content: Content) ![]const u8 { 902 // a result roots the same results page at its commit, so the query 903 // survives following the row 904 if (data.search != null) { 905 const route = pageRoute(data, commit.oid) orelse return error.RouteTooLong; 906 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{try route.toUrl(page_arena)}); 907 } 908 const route = (switch (content) { 909 .message => data.location.commitMessageRoute(.object, commit.oid, data.base_oid), 910 .diff => |diff| data.location.commitsRoute(.object, commit.oid, commit.window.start, diff.path, data.base_oid), 911 }) orelse return error.RouteTooLong; 912 const url = try route.toUrl(page_arena); 913 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 914 } 915 916 // the "a:" link to the page showing `oid`'s message on its own. 917 fn messageLink(page_arena: *std.heap.ArenaAllocator, data: *const Self, oid: []const u8) ![]const u8 { 918 const route = data.location.commitMessageRoute(.object, oid, data.base_oid) orelse return error.RouteTooLong; 919 const url = try route.toUrl(page_arena); 920 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 921 } 922