╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
303 for (hunk.edits.items) |edit| { 304 const text = switch (edit) { 305 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 306 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 307 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 308 }; 309 defer switch (edit) { 310 .eql, .ins => hunk_iter.line_iter_b.free(text), 311 .del => hunk_iter.line_iter_a.free(text), 312 }; 313 const prefix: u8 = switch (edit) { 314 .eql => ' ', 315 .ins => '+', 316 .del => '-', 317 }; 318 const num_str = try std.fmt.allocPrint(arena, "{d}", .{editLineNum(edit) + 1}); 319 try lines.append(arena, try std.fmt.allocPrint(arena, "{s}{s} {c}{s}", .{ 320 indent[0 .. width - num_str.len], num_str, prefix, text, 321 })); 322 } 323 } 324 325 // the line number to show for an edit: the new-side number for kept and inserted 326 // lines, the old-side number for deletions (0-based in the diff machinery). 327 fn editLineNum(edit: df.Edit) usize { 328 return switch (edit) { 329 .eql => |e| e.new_line.num, 330 .ins => |e| e.new_line.num, 331 .del => |e| e.old_line.num, 332 }; 333 } 334 335 // "YYYY-MM-DD" for a unix timestamp. 336 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 337 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 338 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 339 const month_day = year_day.calculateMonthDay(); 340 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 341 year_day.year, 342 month_day.month.numeric(), 343 month_day.day_index + 1, 344 }); 345 } 346 347 // the commit message, trimmed and read into `aa`, plus whether it ran past 348 // `limit` and was cut short there. 349 fn readMessage( 350 comptime repo_kind: rp.RepoKind, 351 comptime repo_opts: rp.RepoOpts(repo_kind), 352 aa: std.mem.Allocator, 353 commit_object: *xit.object.Object(repo_kind, repo_opts), 354 limit: usize, 355 ) !struct { []const u8, bool } { 356 var message: std.ArrayList(u8) = .empty; 357 // a message past the limit keeps the bytes read so far 358 const truncated = if (commit_object.readMessage(aa, &message, .limited(limit))) |_| 359 false 360 else |err| switch (err) { 361 error.StreamTooLong => true, 362 else => |e| return e, 363 }; 364 // the cut lands wherever the limit falls, so a truncated message keeps 365 // only its whole lines: a line break can't fall inside a codepoint, and 366 // the widgets need valid utf-8. a first line past the limit is instead 367 // cut at the last codepoint boundary. 368 const text = if (truncated) 369 (if (std.mem.lastIndexOfScalar(u8, message.items, '\n')) |nl| message.items[0..nl] else trimIncompleteCodepoint(message.items)) 370 else 371 message.items; 372 return .{ std.mem.trim(u8, text, " \t\r\n"), truncated }; 373 } 374 375 /// drop any incomplete UTF data from the end of a byte array 376 fn trimIncompleteCodepoint(bytes: []const u8) []const u8 { 377 var i = bytes.len; 378 while (i > 0 and bytes.len - i < 4) { 379 i -= 1; 380 const byte = bytes[i]; 381 if (byte & 0b1100_0000 == 0b1000_0000) continue; // continuation byte 382 const seq_len = std.unicode.utf8ByteSequenceLength(byte) catch return bytes; 383 return if (i + seq_len > bytes.len) bytes[0..i] else bytes; 384 } 385 return bytes; 386 } 387 388 // a focusable, word-wrapping box holding a commit message. `bottom_label` 389 // marks a message the page only shows part of ("" when it's whole). 390 fn messageBox(allocator: std.mem.Allocator, message: []const u8, bottom_label: []const u8) !wgt.TextBox { 391 var tb = try wgt.TextBox.init(allocator, message, .{ 392 .border_style = .single, 393 .rounded_corners = true, 394 .wrap_kind = .word, 395 .label = " message ", 396 .bottom_label = bottom_label, 397 }); 398 tb.getFocus().focusable = true; 399 return tb; 400 } 401 402 // the first line of a commit message, for the one-row list entries. 403 fn firstLine(message: []const u8) []const u8 { 404 const nl = std.mem.indexOfScalar(u8, message, '\n'); 405 return if (nl) |i| std.mem.trimEnd(u8, message[0..i], " \t\r") else message; 406 } 407 408 pub const View = struct { 409 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 410 // split with the commit list on the left and a diff pane on the right 411 // showing the selected commit's diff. 412 box: wgt.Box(ui.Widget), // vert: [header_index] = banner, [content_index] = split 413 data: *const Self, 414 session: *ui.Session, 415 // the commit whose diff the pane currently shows (index into data.commits). 416 diffed_index: ?usize, 417 418 const header_index: usize = 0; 419 const content_index: usize = 1; 420 // indices within the content box (the horizontal split). 421 const list_index: usize = 0; 422 const diff_index: usize = 1; 423 const list_max_width: usize = 35; 424 const diff_min_width: usize = 40; 425 426 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 427 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 428 errdefer outer.deinit(allocator); 429 430 // the ref banner at the top. 431 { 432 var header_view = try Header.View.init(allocator, &data.header, session, data.identity); 433 errdefer header_view.deinit(allocator); 434 try outer.children.put(allocator, header_view.getFocus().id, .{ .widget = .{ .repo_commits_header = header_view }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 435 } 436 437 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 438 errdefer box.deinit(allocator); 439 440 // the commit list (one focusable row each), plus a "next" link 441 { 442 var list_scroll = blk: { 443 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert, .stretch = true }); 444 errdefer list_box.deinit(allocator); 445 for (data.commits, 0..) |commit, index| { 446 // an in-page "ai:" anchor so a commit row is clickable with 447 // js off (the browser follows it, rooting the list there); 448 // with wasm the click just selects it and swaps the diff pane. 449 const content = if (index == 0) data.content else Content{ .diff = .{} }; 450 try addRow(allocator, &list_box, firstLine(commit.message), try commitRowLink(session.page_arena, data.identity, commit, content)); 451 } 452 if (data.next_start) |next| { 453 try addRow(allocator, &list_box, "next →", try commitsLink(session.page_arena, data.identity, next, 0, "")); 454 } 455 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 456 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 457 }; 458 errdefer list_scroll.deinit(allocator); 459 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 } }); 460 } 461 462 // the diff pane — a frame around a scroll of the hunks 463 { 464 var diff_outer = blk: { 465 var diff_scroll = blk2: { 466 var diff_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 467 errdefer diff_inner.deinit(allocator); 468 // fill the pane (content top-left, scroll bars pinned to the 469 // edges) rather than shrinking to the diff content. 470 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = diff_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 471 }; 472 errdefer diff_scroll.deinit(allocator); 473 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 474 errdefer frame.deinit(allocator); 475 // the frame's selected child is its scroll, so the focus chain 476 // reaches a hunk (populateDiff points the scroll's inner box at 477 // one), letting focus recovery descend into the diff pane after 478 // it's laid out beside a too-narrow list. 479 frame.getFocus().child_id = diff_scroll.getFocus().id; 480 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .scroll = diff_scroll }, .rect = null, .min_size = null }); 481 break :blk frame; 482 }; 483 errdefer diff_outer.deinit(allocator); 484 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 485 } 486 487 box.getFocus().child_id = box.children.keys()[list_index]; 488 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 489 490 // focus lives in the split; the banner isn't focusable. 491 outer.getFocus().child_id = outer.children.keys()[content_index]; 492 493 return .{ 494 .box = outer, 495 .data = data, 496 .session = session, 497 .diffed_index = null, 498 }; 499 } 500 501 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 502 var row = try wgt.TextBox.init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 503 errdefer row.deinit(allocator); 504 row.getFocus().focusable = true; 505 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 506 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 507 } 508 509 // one hunk as a focusable multi-line text box. its lines sit flush (the 510 // inner Text rows have no border). `lines` are joined into the page arena 511 // (text_box borrows it). 512 fn addHunkBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), lines: []const []const u8) !void { 513 if (lines.len == 0) return; 514 const text = try std.mem.join(self.session.page_arena.allocator(), "\n", lines); 515 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 516 errdefer tb.deinit(allocator); 517 tb.getFocus().focusable = true; 518 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 519 } 520 521 // a focusable file-path label shown above the first hunk of each file. it 522 // links to this page filtered to that file, so activating it navigates 523 // there. 524 fn addPathBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), path: []const u8, oid: []const u8) !void { 525 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, 0, path); 526 var tb = try wgt.TextBox.init(allocator, path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 527 errdefer tb.deinit(allocator); 528 tb.getFocus().focusable = true; 529 tb.getFocus().kind = .{ .custom = link }; 530 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 531 } 532 533 // a focusable window-navigation row ("previous"/"next"). it's a link to this 534 // commit's route at `target_start`, so activating it (the host follows the 535 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 536 fn addNavLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, oid: []const u8, target_start: usize, path: []const u8) !void { 537 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, target_start, path); 538 var tb = try wgt.TextBox.init(allocator, label, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 539 errdefer tb.deinit(allocator); 540 tb.getFocus().focusable = true; 541 tb.getFocus().kind = .{ .custom = link }; 542 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 543 } 544 545 // a focusable row for the top of the diff pane linking to the files tab at 546 // this commit's tree, so its files are always viewable as of this object. 547 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 548 const link = try filesObjectLink(self.session.page_arena, self.data.identity, oid); 549 var tb = try wgt.TextBox.init(allocator, "view files at this commit", .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 550 errdefer tb.deinit(allocator); 551 tb.getFocus().focusable = true; 552 tb.getFocus().kind = .{ .custom = link }; 553 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 554 } 555 556 // how much of the message a pane's box holds: a truncated preview links to 557 // the pane holding the whole thing, which is that link's destination. 558 const MessageBox = enum { preview, whole }; 559 560 // the selected commit's message. 561 fn addMessageBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), commit: Commit, kind: MessageBox) !void { 562 // a truncated message keeps its box even with no whole line to show, 563 // since the box is what links to the whole thing. 564 if (commit.message.len == 0 and !commit.message_truncated) return; 565 const cut_short = commit.message_truncated and kind == .preview; 566 var tb = try messageBox(allocator, commit.message, if (cut_short) " click or press enter to see more " else ""); 567 errdefer tb.deinit(allocator); 568 if (cut_short) tb.getFocus().kind = .{ .custom = try messageLink(self.session.page_arena, self.data.identity, commit.oid) }; 569 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 570 } 571 572 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 573 self.box.deinit(allocator); 574 } 575 576 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 577 return &self.box.children.values()[content_index].widget.box; 578 } 579 580 fn header(self: *View) *Header.View { 581 return &self.box.children.values()[header_index].widget.repo_commits_header; 582 } 583 584 fn headerActive(self: *View) bool { 585 return self.box.getFocus().child_id == self.header().getFocus().id; 586 } 587 588 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 589 return &self.contentBox().children.values()[list_index].widget.scroll; 590 } 591 592 fn listBox(self: *View) *wgt.Box(ui.Widget) { 593 return &self.listScroll().child.box; 594 } 595 596 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 597 return &self.contentBox().children.values()[diff_index].widget.box; 598 } 599 600 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 601 return &self.diffOuter().children.values()[0].widget.scroll; 602 } 603 604 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 605 return &self.diffScroll().child.box; 606 } 607 608 fn diffActive(self: *View) bool { 609 const content = self.contentBox(); 610 const cid = content.getFocus().child_id orelse return false; 611 return content.children.getIndex(cid) == diff_index; 612 } 613 614 // what the pane shows for the commit at `sel`. the page's content only 615 // applies to the commit it walks from; the rest show their plain diff. 616 fn paneContent(self: *View, sel: usize) Self.Content { 617 return if (sel == 0) self.data.content else .{ .diff = .{} }; 618 } 619 620 // the selected commit's index, or null when the "next" row is selected. 621 fn selectedCommitIndex(self: *View) ?usize { 622 const lb = self.listBox(); 623 const cid = lb.getFocus().child_id orelse return null; 624 const idx = lb.children.getIndex(cid) orelse return null; 625 if (idx >= self.data.commits.len) return null; 626 return idx; 627 } 628 629 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 630 self.clearGrid(); 631 632 // swap the diff pane to the selected commit when it changes. 633 try self.refreshDiff(allocator); 634 635 // the selected list row shows a border (the focused TextBox upgrades it 636 // to a double border itself); the rest stay borderless. 637 const lb = self.listBox(); 638 for (lb.children.keys(), lb.children.values()) |id, *child| { 639 switch (child.widget) { 640 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 641 else => {}, 642 } 643 } 644 645 // cap the list at list_max_width only while the diff pane fits beside it. 646 // the box drops the diff when the width can't hold both minimums, so when 647 // it's that narrow we lift the cap and let the list fill the whole width. 648 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 649 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 650 651 // stretch the diff pane across the rest of the width so it fills the area 652 // rather than shrinking to its content; its scroll fills the pane. when 653 // too narrow for both, it fills the whole width. 654 const diff_width: usize = if (constraint.max_size.width) |w| 655 (if (both_panes_fit) w - list_max_width else w) 656 else 657 diff_min_width; 658 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_width, .height = null }; 659 660 // the message is the pane's only wrapping row, and wrapping needs a 661 // bounded width, which the pane's scroll doesn't grant (it scrolls 662 // horizontally too). cap it to the pane, leaving room for the border 663 // and the scrollbar column. re-capped each build so it tracks resizes. 664 for (self.diffInner().children.values()) |*child| switch (child.widget) { 665 .text_box => |text_box| if (text_box.options.wrap_kind == .word) { 666 child.max_size = .{ .width = diff_width -| 3, .height = null }; 667 }, 668 else => {}, 669 }; 670 671 // the web bounds the layout to the browser viewport like the terminal; 672 // each Scroll's web-native mode hands its full content to a real 673 // scrollable element, so we no longer build unbounded here. 674 // 675 // crossing panes only re-selects the content box's child (focusDiff / 676 // focusList); when the window is too narrow to show both, the pane that 677 // held focus is dropped here. the framework recovers focus after the 678 // top-level build by re-deriving it down the selected-child chain, so 679 // there's nothing to fix up afterward. 680 try self.box.build(allocator, constraint, root_focus); 681 } 682 683 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 684 const sel = self.selectedCommitIndex() orelse return; 685 if (self.diffed_index) |d| if (d == sel) return; 686 try self.populateDiff(allocator, sel); 687 self.diffed_index = sel; 688 } 689 690 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 691 const commit = self.data.commits[sel]; 692 const inner = self.diffInner(); 693 694 for (inner.children.values()) |*child| child.widget.deinit(allocator); 695 inner.children.clearAndFree(allocator); 696 inner.getFocus().child_id = null; 697 698 switch (self.paneContent(sel)) { 699 .message => { 700 try self.addNavLink(allocator, inner, "← back to diff", commit.oid, 0, ""); 701 try self.addMessageBox(allocator, inner, commit, .whole); 702 }, 703 .diff => |d| { 704 if (d.path.len != 0) { 705 // the file's path, then a row returning to this commit's unfiltered diff. 706 var label = try wgt.TextBox.init(allocator, d.path, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 707 errdefer label.deinit(allocator); 708 label.getFocus().focusable = true; 709 try inner.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = null }); 710 try self.addNavLink(allocator, inner, "← all files", commit.oid, 0, ""); 711 } else { 712 try self.addMessageBox(allocator, inner, commit, .preview); 713 if (commit.author != .unknown) { 714 var tb = try ui.authorBox(allocator, self.session.page_arena, commit.author); 715 errdefer tb.deinit(allocator); 716 try inner.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 717 } 718 try self.addViewFilesLink(allocator, inner, commit.oid); 719 } 720 721 // a "previous" row and a "next" row at the bottom reload the page 722 // on the adjacent diff window. 723 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page, d.path); 724 for (commit.hunks) |hunk| { 725 if (d.path.len == 0) if (hunk.path) |hunk_path| try self.addPathBox(allocator, inner, hunk_path, commit.oid); 726 try self.addHunkBox(allocator, inner, hunk.lines); 727 } 728 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page, d.path); 729 }, 730 } 731 732 // point the pane at its first row so focus recovery can land here. 733 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 734 735 // reset the scroll to the top for the newly-shown commit: directly on the 736 // terminal (the wasm offset), and via a version bump on the web (so the 737 // renderer's scroll id changes and JS drops the preserved position). 738 const sc = self.diffScroll(); 739 sc.x = 0; 740 sc.y = 0; 741 sc.getFocus().version +%= 1; 742 } 743 744 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 745 if (self.headerActive()) { 746 if (key == .arrow_down) { 747 root_focus.setFocus(self.contentBox().getFocus().id); 748 } else { 749 try self.header().input(allocator, key, root_focus); 750 } 751 return; 752 } 753 if (key == .arrow_up and self.contentAtTop() and self.header().focusCloneUrl(root_focus)) return; 754 if (self.diffActive()) { 755 try self.diffInput(key, root_focus); 756 } else { 757 try self.listInput(key, root_focus); 758 } 759 } 760 761 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 762 // up/down (and the scroll wheel) move the selection a row; page up/down 763 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 764 // on the "next" row become navigation in the host before reaching here. 765 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 766 ui.widget.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 767 return; 768 } 769 switch (key) { 770 .enter => if (self.selectedCommitIndex() != null) 771 try self.focusDiff(root_focus) 772 else if (self.data.next_start) |next| { 773 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0, "")) |route| 774 try self.session.navigate(route); 775 }, 776 .arrow_right => try self.focusDiff(root_focus), 777 else => {}, 778 } 779 } 780 781 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 782 const sc = self.diffScroll(); 783 switch (key) { 784 // left scrolls horizontally, then leaves for the list once flush left. 785 .arrow_left => { 786 if (sc.x > 0) { 787 sc.x -= 1; 788 self.diffScroll().clampToContent(); 789 } else try self.focusList(root_focus); 790 }, 791 .arrow_right => { 792 sc.x += 1; 793 self.diffScroll().clampToContent(); 794 }, 795 .arrow_up => try self.moveDiff(root_focus, -1), 796 .arrow_down => try self.moveDiff(root_focus, 1), 797 .page_up => try self.pageDiff(root_focus, -10), 798 .page_down => try self.pageDiff(root_focus, 10), 799 .home => try self.jumpDiff(root_focus, false), 800 .end => try self.jumpDiff(root_focus, true), 801 // Enter / a click on a "previous"/"next" row follow its "a:" link in 802 // the host (it reloads the adjacent window), so they don't reach here. 803 .mouse => |mouse| switch (mouse.action) { 804 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 805 else => {}, 806 }, 807 else => {}, 808 } 809 } 810 811 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 812 // line count unless there is a new hunk visible (in which case it is a 813 // hunk count). on the web `delta` is always a hunk count. 814 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 815 const inner = self.diffInner(); 816 const keys = inner.children.keys(); 817 if (keys.len == 0) return; 818 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 819 const target = @as(isize, @intCast(cur)) + delta; 820 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 821 822 if (self.session.is_terminal) { 823 if (in_range and self.hunkVisible(@intCast(target))) { 824 root_focus.setFocus(keys[@intCast(target)]); 825 return; 826 } 827 828 const sc = self.diffScroll(); 829 sc.y += delta * 5; // magnify because it's a line count 830 self.diffScroll().clampToContent(); 831 if (in_range and self.hunkVisible(@intCast(target))) { 832 root_focus.setFocus(keys[@intCast(target)]); 833 } 834 } else { 835 if (in_range) { 836 root_focus.setFocus(keys[@intCast(target)]); 837 } 838 } 839 } 840 841 // page a fixed number of lines, then focus the leading visible hunk 842 // (bottom-most when paging down, top-most when paging up). in the 843 // terminal, `delta` is a line count, and on the web it's a hunk count. 844 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 845 if (self.session.is_terminal) { 846 const sc = self.diffScroll(); 847 sc.y += delta * 5; // magnify because it's a line count 848 self.diffScroll().clampToContent(); 849 try self.focusVisible(root_focus, delta > 0); 850 } else { 851 const inner = self.diffInner(); 852 const keys = inner.children.keys(); 853 if (keys.len == 0) return; 854 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 855 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 856 root_focus.setFocus(keys[target]); 857 } 858 } 859 860 // jump to the first or last hunk. on the web the browser scrolls to the 861 // focused hunk; on the terminal pin the scroll to the top/bottom too. 862 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 863 const inner = self.diffInner(); 864 const keys = inner.children.keys(); 865 if (keys.len == 0) return; 866 if (self.session.is_terminal) { 867 const sc = self.diffScroll(); 868 sc.y = if (to_end) std.math.maxInt(isize) else 0; 869 self.diffScroll().clampToContent(); 870 } 871 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 872 } 873 874 // whether hunk `index` is at least partly within the diff viewport, per the 875 // last build's layout rects (content space) and the current scroll offset. 876 fn hunkVisible(self: *View, index: usize) bool { 877 const inner = self.diffInner(); 878 const sc = self.diffScroll(); 879 const vp = sc.grid orelse return false; 880 const r = inner.children.values()[index].rect orelse return false; 881 const top = sc.y; 882 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 883 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 884 } 885 886 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 887 // the bottom-most visible (for downward motion), else the top-most. 888 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 889 const inner = self.diffInner(); 890 const sc = self.diffScroll(); 891 const vp = sc.grid orelse return; 892 const top = sc.y; 893 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 894 var chosen: ?usize = null; 895 for (inner.children.keys(), inner.children.values()) |id, *child| { 896 const r = child.rect orelse continue; // content-space layout rect 897 const r_top = r.y; 898 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 899 if (r_bot <= top or r_top >= bottom) continue; // not visible 900 chosen = id; 901 if (!prefer_last) break; // first visible 902 } 903 if (chosen) |id| root_focus.setFocus(id); 904 } 905 906 // enter the diff pane. the host arrives here on right-arrow or Enter from the 907 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 908 // case where the pane isn't laid out yet (it gets selected, then focused after 909 // the next build, landing on its remembered hunk). 910 fn focusDiff(self: *View, root_focus: *Focus) !void { 911 if (self.diffInner().children.count() == 0) return; 912 root_focus.setFocus(self.diffOuter().getFocus().id); 913 } 914 915 // return to the list. 916 fn focusList(self: *View, root_focus: *Focus) !void { 917 root_focus.setFocus(self.listScroll().getFocus().id); 918 } 919 920 pub fn clearGrid(self: *View) void { 921 self.box.clearGrid(); 922 } 923 924 pub fn getGrid(self: View) ?Grid { 925 return self.box.getGrid(); 926 } 927 928 pub fn getFocus(self: *View) *Focus { 929 return self.box.getFocus(); 930 } 931 932 fn contentAtTop(self: *View) bool { 933 if (self.diffActive()) { 934 const inner = self.diffInner(); 935 const cid = inner.getFocus().child_id orelse return true; 936 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 937 return first_focused and self.diffScroll().y == 0; 938 } 939 const lb = self.listBox(); 940 const cid = lb.getFocus().child_id orelse return true; 941 return lb.children.getIndex(cid) == 0; 942 } 943 944 // only the clone-url row sits directly below the repository header. 945 pub fn getSelectedIndex(self: *View) ?usize { 946 if (self.headerActive()) return 0; 947 return if (self.header().hasCloneUrl() or !self.contentAtTop()) 1 else 0; 948 } 949 950 pub fn focusCloneUrl(self: *View, root_focus: *Focus) bool { 951 return self.header().focusCloneUrl(root_focus); 952 } 953 }; 954 955 // the "a:" navigation link for the commits page walking from commit `oid` within 956 // `identity`, windowing the selected commit's diff from hunk `start`, filtered 957 // to `path` ("" = every file). 958 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 959 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, start, path) orelse return error.RouteTooLong; 960 const url = try route.toUrl(page_arena); 961 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 962 } 963 964 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 965 // directory, within `identity` ("owner/name"). 966 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 967 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 968 const url = try route.toUrl(page_arena); 969 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 970 } 971 972 // the in-page "ai:" anchor for selecting a commit with its current pane content 973 // and window. the href is only followed with js off. 974 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, commit: Commit, content: Content) ![]const u8 { 975 const route = (switch (content) { 976 .message => ui.RoutablePage.repoCommitMessageRoute(identity, .object, commit.oid), 977 .diff => |diff| ui.RoutablePage.repoCommitsRoute(identity, .object, commit.oid, commit.window_start, diff.path), 978 }) orelse return error.RouteTooLong; 979 const url = try route.toUrl(page_arena); 980 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 981 } 982 983 // the "a:" link to the page showing `oid`'s message on its own. 984 fn messageLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 985 const route = ui.RoutablePage.repoCommitMessageRoute(identity, .object, oid) orelse return error.RouteTooLong; 986 const url = try route.toUrl(page_arena); 987 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 988 } 989 990 // the "<ref_or_oid> <value>" banner shown above the log. 991 pub const Header = struct { 992 content: []const u8, 993 994 // `value` arrives url-encoded, so decode it for display. 995 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 996 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, value)); 997 return .{ 998 .content = try std.fmt.allocPrint(aa, "{s} {s}", .{ @tagName(ref_or_oid), decoded }), 999 }; 1000 } 1001 1002 pub const View = struct { 1003 box: wgt.Box(ui.Widget), 1004 data: *const Header, 1005 1006 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session, identity: []const u8) !Header.View { 1007 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 1008 errdefer box.deinit(allocator); 1009 1010 if (!session.data.is_local and (session.data.git_http_port != null or session.data.git_ssh_port != null)) { 1011 var clone_url = try ui.widget.CopyableText.initClone(allocator, session, identity); 1012 errdefer clone_url.deinit(allocator); 1013 const min_width = clone_url.minWidth(); 1014 box.getFocus().child_id = clone_url.getFocus().id; 1015 try box.children.put(allocator, clone_url.getFocus().id, .{ .widget = .{ .copyable_text = clone_url }, .rect = null, .min_size = .{ .width = min_width, .height = 3 }, .max_size = .{ .width = min_width, .height = 3 } }); 1016 } 1017 1018 var spacer = try ui.widget.Spacer.init(allocator); 1019 errdefer spacer.deinit(allocator); 1020 try box.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 1021 1022 var text_box = try wgt.TextBox.init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 1023 errdefer text_box.deinit(allocator); 1024 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null, .flex = .shrink }); 1025 1026 return .{ .box = box, .data = data }; 1027 } 1028 1029 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 1030 self.box.deinit(allocator); 1031 } 1032 1033 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 1034 self.clearGrid(); 1035 try self.box.build(allocator, constraint, root_focus); 1036 } 1037 1038 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 1039 const clone_url = self.cloneUrl() orelse return; 1040 try clone_url.input(allocator, key, root_focus); 1041 } 1042 1043 pub fn focusCloneUrl(self: *Header.View, root_focus: *Focus) bool { 1044 const clone_url = self.cloneUrl() orelse return false; 1045 root_focus.setFocus(clone_url.getFocus().id); 1046 return true; 1047 } 1048 1049 pub fn hasCloneUrl(self: *Header.View) bool { 1050 return self.cloneUrl() != null; 1051 } 1052 1053 fn cloneUrl(self: *Header.View) ?*ui.widget.CopyableText { 1054 for (self.box.children.values()) |*child| switch (child.widget) { 1055 .copyable_text => |*copyable_text| return copyable_text, 1056 else => {}, 1057 }; 1058 return null; 1059 } 1060 1061 pub fn clearGrid(self: *Header.View) void { 1062 self.box.clearGrid(); 1063 } 1064 1065 pub fn getGrid(self: Header.View) ?Grid { 1066 return self.box.getGrid(); 1067 } 1068 1069 pub fn getFocus(self: *Header.View) *Focus { 1070 return self.box.getFocus(); 1071 } 1072 }; 1073 }; 1074