╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
184 185 // load the commit so we can diff it against its first parent. 186 var start_oids = [_][xit.hash.hexLen(repo_opts.hash)]u8{oid}; 187 var commit_iter = repo.log(io, gpa, start_oids[0..1]) catch return empty; 188 defer commit_iter.deinit(); 189 const commit_object = (commit_iter.next(gpa) catch return empty) orelse return empty; 190 defer commit_object.deinit(); 191 192 const parent_maybe = commit_object.content.commit.metadata.firstParent(); 193 194 var tree_diff = repo.treeDiff(io, gpa, parent_maybe, &commit_object.oid) catch return empty; 195 defer tree_diff.deinit(); 196 197 var file_iter = repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }) catch return empty; 198 199 var hunks: std.ArrayList(Hunk) = .empty; 200 var index: usize = 0; // running hunk index across all files 201 var has_more = false; 202 203 file_loop: while (file_iter.next() catch null) |pair_val| { 204 var pair = pair_val; 205 defer pair.deinit(); 206 207 if (path.len != 0 and !std.mem.eql(u8, pair.path, path)) continue; 208 209 var hunk_iter = df.HunkIterator(repo_kind, repo_opts).init(gpa, &pair.a, &pair.b) catch continue; 210 defer hunk_iter.deinit(gpa); 211 212 // the path label rides on the first of this file's hunks we actually show. 213 var path_attached = false; 214 while (try hunk_iter.next(gpa)) |hunk_val| { 215 var hunk = hunk_val; 216 defer hunk.deinit(gpa); 217 218 const i = index; 219 index += 1; 220 if (i < start) continue; // before this window 221 if (hunks.items.len >= len) { 222 has_more = true; 223 break :file_loop; 224 } 225 var lines: std.ArrayList([]const u8) = .empty; 226 try appendHunkLines(repo_kind, repo_opts, arena, &lines, &hunk_iter, &hunk); 227 try hunks.append(arena, .{ 228 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 229 .lines = try lines.toOwnedSlice(arena), 230 }); 231 path_attached = true; 232 } 233 } 234 235 return .{ .hunks = try hunks.toOwnedSlice(arena), .has_prev = start > 0, .has_more = has_more }; 236 } 237 238 // append a hunk's header and its edit lines, each prefixed with a right-aligned 239 // line number in a column wide enough for the hunk's largest number, then a space 240 fn appendHunkLines( 241 comptime repo_kind: rp.RepoKind, 242 comptime repo_opts: rp.RepoOpts(repo_kind), 243 arena: std.mem.Allocator, 244 lines: *std.ArrayList([]const u8), 245 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 246 hunk: *df.Hunk(repo_kind, repo_opts), 247 ) !void { 248 var max_num: usize = 1; 249 for (hunk.edits.items) |edit| { 250 const n = editLineNum(edit) + 1; 251 if (n > max_num) max_num = n; 252 } 253 const width = std.fmt.count("{d}", .{max_num}); 254 // a run of spaces sliced to each line's leading pad. 255 const indent = try arena.alloc(u8, width); 256 @memset(indent, ' '); 257 258 for (hunk.edits.items) |edit| { 259 const text = switch (edit) { 260 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 261 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 262 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 263 }; 264 defer switch (edit) { 265 .eql, .ins => hunk_iter.line_iter_b.free(text), 266 .del => hunk_iter.line_iter_a.free(text), 267 }; 268 const prefix: u8 = switch (edit) { 269 .eql => ' ', 270 .ins => '+', 271 .del => '-', 272 }; 273 const num_str = try std.fmt.allocPrint(arena, "{d}", .{editLineNum(edit) + 1}); 274 try lines.append(arena, try std.fmt.allocPrint(arena, "{s}{s} {c}{s}", .{ 275 indent[0 .. width - num_str.len], num_str, prefix, text, 276 })); 277 } 278 } 279 280 // the line number to show for an edit: the new-side number for kept and inserted 281 // lines, the old-side number for deletions (0-based in the diff machinery). 282 fn editLineNum(edit: df.Edit) usize { 283 return switch (edit) { 284 .eql => |e| e.new_line.num, 285 .ins => |e| e.new_line.num, 286 .del => |e| e.old_line.num, 287 }; 288 } 289 290 // "YYYY-MM-DD" for a unix timestamp. 291 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 292 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 293 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 294 const month_day = year_day.calculateMonthDay(); 295 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 296 year_day.year, 297 month_day.month.numeric(), 298 month_day.day_index + 1, 299 }); 300 } 301 302 // the first line of a commit message, with surrounding whitespace trimmed. 303 fn firstLine(message: []const u8) []const u8 { 304 const trimmed = std.mem.trim(u8, message, " \t\r\n"); 305 const nl = std.mem.indexOfScalar(u8, trimmed, '\n'); 306 return if (nl) |i| trimmed[0..i] else trimmed; 307 } 308 309 pub const View = struct { 310 // a vertical stack: the "viewing <ref>" banner on top, then a horizontal 311 // split with the commit list on the left and a diff pane on the right 312 // showing the selected commit's diff. 313 box: wgt.Box(ui.Widget), // vert: [header_index] = banner, [content_index] = split 314 data: *const Self, 315 session: *ui.Session, 316 // the commit whose diff the pane currently shows (index into data.commits). 317 diffed_index: ?usize, 318 319 const header_index: usize = 0; 320 const content_index: usize = 1; 321 // indices within the content box (the horizontal split). 322 const list_index: usize = 0; 323 const diff_index: usize = 1; 324 const list_max_width: usize = 40; 325 const diff_min_width: usize = 40; 326 327 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 328 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 329 errdefer outer.deinit(allocator); 330 331 // the ref banner at the top. 332 { 333 var header = try Header.View.init(allocator, &data.header, session); 334 errdefer header.deinit(allocator); 335 try outer.children.put(allocator, header.getFocus().id, .{ .widget = .{ .repo_commits_header = header }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 336 } 337 338 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 339 errdefer box.deinit(allocator); 340 341 // the commit list (one focusable row each), plus a "next" link 342 { 343 var list_scroll = blk: { 344 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 345 errdefer list_box.deinit(allocator); 346 for (data.commits) |commit| { 347 // an in-page "ai:" anchor so a commit row is clickable with 348 // js off (the browser follows it, rooting the list there); 349 // with wasm the click just selects it and swaps the diff pane. 350 try addRow(allocator, &list_box, commit.message, try commitRowLink(session.page_arena, data.identity, commit.oid, "")); 351 } 352 if (data.next_start) |next| { 353 try addRow(allocator, &list_box, "next →", try commitsLink(session.page_arena, data.identity, next, 0, "")); 354 } 355 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 356 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 357 }; 358 errdefer list_scroll.deinit(allocator); 359 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 } }); 360 } 361 362 // the diff pane — a frame around a scroll of the hunks 363 { 364 var diff_outer = blk: { 365 var diff_scroll = blk2: { 366 var diff_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 367 errdefer diff_inner.deinit(allocator); 368 // fill the pane (content top-left, scroll bars pinned to the 369 // edges) rather than shrinking to the diff content. 370 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = diff_inner }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }); 371 }; 372 errdefer diff_scroll.deinit(allocator); 373 var frame = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 374 errdefer frame.deinit(allocator); 375 // the frame's selected child is its scroll, so the focus chain 376 // reaches a hunk (populateDiff points the scroll's inner box at 377 // one), letting focus recovery descend into the diff pane after 378 // it's laid out beside a too-narrow list. 379 frame.getFocus().child_id = diff_scroll.getFocus().id; 380 try frame.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .scroll = diff_scroll }, .rect = null, .min_size = null }); 381 break :blk frame; 382 }; 383 errdefer diff_outer.deinit(allocator); 384 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 385 } 386 387 box.getFocus().child_id = box.children.keys()[list_index]; 388 try outer.children.put(allocator, box.getFocus().id, .{ .widget = .{ .box = box }, .rect = null, .min_size = null }); 389 390 // focus lives in the split; the banner isn't focusable. 391 outer.getFocus().child_id = outer.children.keys()[content_index]; 392 393 return .{ 394 .box = outer, 395 .data = data, 396 .session = session, 397 .diffed_index = null, 398 }; 399 } 400 401 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 402 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 403 errdefer row.deinit(allocator); 404 row.getFocus().focusable = true; 405 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 406 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 407 } 408 409 // one hunk as a focusable multi-line text box. its lines sit flush (the 410 // inner Text rows have no border); the box's hidden border reserves the 411 // space the double border occupies when focused, so focusing doesn't shift 412 // layout. `lines` are joined into the page arena (text_box borrows it). 413 fn addHunkBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), lines: []const []const u8) !void { 414 if (lines.len == 0) return; 415 const text = try std.mem.join(self.session.page_arena.allocator(), "\n", lines); 416 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 417 errdefer tb.deinit(allocator); 418 tb.getFocus().focusable = true; 419 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 420 } 421 422 // a focusable file-path label shown above the first hunk of each file. it 423 // links to this page filtered to that file, so activating it navigates 424 // there. 425 fn addPathBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), path: []const u8, oid: []const u8) !void { 426 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, 0, path); 427 var tb = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 428 errdefer tb.deinit(allocator); 429 tb.getFocus().focusable = true; 430 tb.getFocus().kind = .{ .custom = link }; 431 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 432 } 433 434 // a focusable window-navigation row ("previous"/"next"). it's a link to this 435 // commit's route at `target_start`, so activating it (the host follows the 436 // "a:" link) reloads the page on the adjacent window — same on TUI and web. 437 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 { 438 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, target_start, path); 439 var tb = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 440 errdefer tb.deinit(allocator); 441 tb.getFocus().focusable = true; 442 tb.getFocus().kind = .{ .custom = link }; 443 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 444 } 445 446 // a focusable row for the top of the diff pane linking to the files tab at 447 // this commit's tree, so its files are always viewable as of this object. 448 fn addViewFilesLink(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8) !void { 449 const link = try filesObjectLink(self.session.page_arena, self.data.identity, oid); 450 var tb = try wgt.TextBox(ui.Widget).init(allocator, "view files at this commit", .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 451 errdefer tb.deinit(allocator); 452 tb.getFocus().focusable = true; 453 tb.getFocus().kind = .{ .custom = link }; 454 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 455 } 456 457 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 458 self.box.deinit(allocator); 459 } 460 461 fn contentBox(self: *View) *wgt.Box(ui.Widget) { 462 return &self.box.children.values()[content_index].widget.box; 463 } 464 465 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 466 return &self.contentBox().children.values()[list_index].widget.scroll; 467 } 468 469 fn listBox(self: *View) *wgt.Box(ui.Widget) { 470 return &self.listScroll().child.box; 471 } 472 473 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 474 return &self.contentBox().children.values()[diff_index].widget.box; 475 } 476 477 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 478 return &self.diffOuter().children.values()[0].widget.scroll; 479 } 480 481 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 482 return &self.diffScroll().child.box; 483 } 484 485 fn diffActive(self: *View) bool { 486 const content = self.contentBox(); 487 const cid = content.getFocus().child_id orelse return false; 488 return content.children.getIndex(cid) == diff_index; 489 } 490 491 // the selected commit's index, or null when the "next" row is selected. 492 fn selectedCommitIndex(self: *View) ?usize { 493 const lb = self.listBox(); 494 const cid = lb.getFocus().child_id orelse return null; 495 const idx = lb.children.getIndex(cid) orelse return null; 496 if (idx >= self.data.commits.len) return null; 497 return idx; 498 } 499 500 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 501 self.clearGrid(); 502 503 // swap the diff pane to the selected commit when it changes. 504 try self.refreshDiff(allocator); 505 506 // mirror the focused commit into the url so it updates as the selection 507 // moves, but only while focus is inside this view (the list or diff). when 508 // focus sits on the header tab, use the page's base /commits route. 509 if (root_focus.grandchild_id) |g| { 510 if (self.box.getFocus().children.contains(g)) { 511 if (self.selectedCommitIndex()) |sel| { 512 // mirror the commit's current diff window so the url stays 513 // linkable (0 for any commit but the windowed start one). 514 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, self.data.commits[sel].oid, self.data.commits[sel].window_start, if (sel == 0) self.data.path else "")) |route| 515 self.session.data.current_page = route; 516 } 517 } 518 } 519 520 // the selected list row shows a border (the focused TextBox upgrades it 521 // to a double border itself); the rest stay borderless. 522 const lb = self.listBox(); 523 for (lb.children.keys(), lb.children.values()) |id, *child| { 524 switch (child.widget) { 525 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 526 else => {}, 527 } 528 } 529 530 // the selected hunk (or a window-navigation row) shows a single border 531 // (the focused TextBox upgrades it to a double border itself, so it 532 // stays single when focus is on the commit list); the rest keep their 533 // space-reserving hidden border. 534 const inner = self.diffInner(); 535 for (inner.children.keys(), inner.children.values()) |id, *child| { 536 switch (child.widget) { 537 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 538 else => {}, 539 } 540 } 541 542 // cap the list at list_max_width only while the diff pane fits beside it. 543 // the box drops the diff when the width can't hold both minimums, so when 544 // it's that narrow we lift the cap and let the list fill the whole width. 545 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 546 self.contentBox().children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 547 548 // stretch the diff pane across the rest of the width so it fills the area 549 // rather than shrinking to its content; its scroll fills the pane. when 550 // too narrow for both, it fills the whole width. 551 if (constraint.max_size.width) |w| { 552 self.contentBox().children.values()[diff_index].min_size = .{ .width = if (both_panes_fit) w - list_max_width else w, .height = null }; 553 } else { 554 self.contentBox().children.values()[diff_index].min_size = .{ .width = diff_min_width, .height = null }; 555 } 556 557 // the web bounds the layout to the browser viewport like the terminal; 558 // each Scroll's web-native mode hands its full content to a real 559 // scrollable element, so we no longer build unbounded here. 560 // 561 // crossing panes only re-selects the content box's child (focusDiff / 562 // focusList); when the window is too narrow to show both, the pane that 563 // held focus is dropped here. the framework recovers focus after the 564 // top-level build by re-deriving it down the selected-child chain, so 565 // there's nothing to fix up afterward. 566 try self.box.build(allocator, constraint, root_focus); 567 } 568 569 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 570 const sel = self.selectedCommitIndex() orelse return; 571 if (self.diffed_index) |d| if (d == sel) return; 572 try self.populateDiff(allocator, sel); 573 self.diffed_index = sel; 574 } 575 576 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 577 const commit = self.data.commits[sel]; 578 // the path filter only applies to the top commit (the walk root). 579 const path = if (sel == 0) self.data.path else ""; 580 const inner = self.diffInner(); 581 582 for (inner.children.values()) |*child| child.widget.deinit(allocator); 583 inner.children.clearAndFree(allocator); 584 inner.getFocus().child_id = null; 585 586 if (path.len != 0) { 587 // the file's path, then a row returning to this commit's unfiltered diff. 588 var label = try wgt.TextBox(ui.Widget).init(allocator, path, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 589 errdefer label.deinit(allocator); 590 label.getFocus().focusable = true; 591 try inner.children.put(allocator, label.getFocus().id, .{ .widget = .{ .text_box = label }, .rect = null, .min_size = null }); 592 try self.addNavLink(allocator, inner, "← all files", commit.oid, 0, ""); 593 } else { 594 // the first row always links to viewing the repo's files at this commit. 595 try self.addViewFilesLink(allocator, inner, commit.oid); 596 } 597 598 // a "previous" row and a "next" row at the bottom reload the page on the 599 // adjacent diff window. 600 if (commit.has_prev) try self.addNavLink(allocator, inner, "← previous", commit.oid, commit.window_start -| diff_page, path); 601 for (commit.hunks) |hunk| { 602 if (path.len == 0) if (hunk.path) |hunk_path| try self.addPathBox(allocator, inner, hunk_path, commit.oid); 603 try self.addHunkBox(allocator, inner, hunk.lines); 604 } 605 if (commit.has_more) try self.addNavLink(allocator, inner, "next →", commit.oid, commit.window_start + diff_page, path); 606 607 // point the pane at its first row so focus recovery can land here. 608 if (inner.children.count() > 0) inner.getFocus().child_id = inner.children.keys()[0]; 609 610 // reset the scroll to the top for the newly-shown commit: directly on the 611 // terminal (the wasm offset), and via a version bump on the web (so the 612 // renderer's scroll id changes and JS drops the preserved position). 613 const sc = self.diffScroll(); 614 sc.x = 0; 615 sc.y = 0; 616 sc.getFocus().version +%= 1; 617 } 618 619 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 620 _ = allocator; 621 if (self.diffActive()) { 622 try self.diffInput(key, root_focus); 623 } else { 624 try self.listInput(key, root_focus); 625 } 626 } 627 628 fn listInput(self: *View, key: Key, root_focus: *Focus) !void { 629 // up/down (and the scroll wheel) move the selection a row; page up/down 630 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 631 // on the "next" row become navigation in the host before reaching here. 632 if (inp.rowDelta(key, @intCast(self.listBox().children.count()))) |delta| { 633 ui.moveRowFocus(self.listBox(), self.listScroll(), root_focus, delta); 634 return; 635 } 636 switch (key) { 637 .enter => if (self.selectedCommitIndex() != null) 638 try self.focusDiff(root_focus) 639 else if (self.data.next_start) |next| { 640 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, .object, next, 0, "")) |route| 641 try self.session.navigate(route); 642 }, 643 .arrow_right => try self.focusDiff(root_focus), 644 else => {}, 645 } 646 } 647 648 fn diffInput(self: *View, key: Key, root_focus: *Focus) !void { 649 const sc = self.diffScroll(); 650 switch (key) { 651 // left scrolls horizontally, then leaves for the list once flush left. 652 .arrow_left => { 653 if (sc.x > 0) { 654 sc.x -= 1; 655 self.diffScroll().clampToContent(); 656 } else try self.focusList(root_focus); 657 }, 658 .arrow_right => { 659 sc.x += 1; 660 self.diffScroll().clampToContent(); 661 }, 662 .arrow_up => try self.moveDiff(root_focus, -1), 663 .arrow_down => try self.moveDiff(root_focus, 1), 664 .page_up => try self.pageDiff(root_focus, -10), 665 .page_down => try self.pageDiff(root_focus, 10), 666 .home => try self.jumpDiff(root_focus, false), 667 .end => try self.jumpDiff(root_focus, true), 668 // Enter / a click on a "previous"/"next" row follow its "a:" link in 669 // the host (it reloads the adjacent window), so they don't reach here. 670 .mouse => |mouse| switch (mouse.action) { 671 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 672 else => {}, 673 }, 674 else => {}, 675 } 676 } 677 678 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 679 // line count unless there is a new hunk visible (in which case it is a 680 // hunk count). on the web `delta` is always a hunk count. 681 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) !void { 682 const inner = self.diffInner(); 683 const keys = inner.children.keys(); 684 if (keys.len == 0) return; 685 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 686 const target = @as(isize, @intCast(cur)) + delta; 687 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 688 689 if (self.session.is_terminal) { 690 if (in_range and self.hunkVisible(@intCast(target))) { 691 root_focus.setFocus(keys[@intCast(target)]); 692 return; 693 } 694 695 const sc = self.diffScroll(); 696 sc.y += delta * 5; // magnify because it's a line count 697 self.diffScroll().clampToContent(); 698 if (in_range and self.hunkVisible(@intCast(target))) { 699 root_focus.setFocus(keys[@intCast(target)]); 700 } 701 } else { 702 if (in_range) { 703 root_focus.setFocus(keys[@intCast(target)]); 704 } 705 } 706 } 707 708 // page a fixed number of lines, then focus the leading visible hunk 709 // (bottom-most when paging down, top-most when paging up). in the 710 // terminal, `delta` is a line count, and on the web it's a hunk count. 711 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 712 if (self.session.is_terminal) { 713 const sc = self.diffScroll(); 714 sc.y += delta * 5; // magnify because it's a line count 715 self.diffScroll().clampToContent(); 716 try self.focusVisible(root_focus, delta > 0); 717 } else { 718 const inner = self.diffInner(); 719 const keys = inner.children.keys(); 720 if (keys.len == 0) return; 721 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(inner.children.getIndex(g) orelse 0) else 0; 722 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 723 root_focus.setFocus(keys[target]); 724 } 725 } 726 727 // jump to the first or last hunk. on the web the browser scrolls to the 728 // focused hunk; on the terminal pin the scroll to the top/bottom too. 729 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) !void { 730 const inner = self.diffInner(); 731 const keys = inner.children.keys(); 732 if (keys.len == 0) return; 733 if (self.session.is_terminal) { 734 const sc = self.diffScroll(); 735 sc.y = if (to_end) std.math.maxInt(isize) else 0; 736 self.diffScroll().clampToContent(); 737 } 738 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 739 } 740 741 // whether hunk `index` is at least partly within the diff viewport, per the 742 // last build's layout rects (content space) and the current scroll offset. 743 fn hunkVisible(self: *View, index: usize) bool { 744 const inner = self.diffInner(); 745 const sc = self.diffScroll(); 746 const vp = sc.grid orelse return false; 747 const r = inner.children.values()[index].rect orelse return false; 748 const top = sc.y; 749 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 750 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 751 } 752 753 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 754 // the bottom-most visible (for downward motion), else the top-most. 755 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 756 const inner = self.diffInner(); 757 const sc = self.diffScroll(); 758 const vp = sc.grid orelse return; 759 const top = sc.y; 760 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 761 var chosen: ?usize = null; 762 for (inner.children.keys(), inner.children.values()) |id, *child| { 763 const r = child.rect orelse continue; // content-space layout rect 764 const r_top = r.y; 765 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 766 if (r_bot <= top or r_top >= bottom) continue; // not visible 767 chosen = id; 768 if (!prefer_last) break; // first visible 769 } 770 if (chosen) |id| root_focus.setFocus(id); 771 } 772 773 // enter the diff pane. the host arrives here on right-arrow or Enter from the 774 // list. a diff with no rows can't be entered. setFocus handles the too-narrow 775 // case where the pane isn't laid out yet (it gets selected, then focused after 776 // the next build, landing on its remembered hunk). 777 fn focusDiff(self: *View, root_focus: *Focus) !void { 778 if (self.diffInner().children.count() == 0) return; 779 root_focus.setFocus(self.diffOuter().getFocus().id); 780 } 781 782 // return to the list. 783 fn focusList(self: *View, root_focus: *Focus) !void { 784 root_focus.setFocus(self.listScroll().getFocus().id); 785 } 786 787 pub fn clearGrid(self: *View) void { 788 self.box.clearGrid(); 789 } 790 791 pub fn getGrid(self: View) ?Grid { 792 return self.box.getGrid(); 793 } 794 795 pub fn getFocus(self: *View) *Focus { 796 return self.box.getFocus(); 797 } 798 799 // for the parent's "scroll up at the top jumps to the header" check. in the 800 // diff pane that means the first hunk is focused and the diff can't scroll up 801 // any further; otherwise up-arrow stays in the pane (scrolling or moving 802 // between hunks). when the list holds focus, report its selection directly. 803 pub fn getSelectedIndex(self: *View) ?usize { 804 if (self.diffActive()) { 805 const inner = self.diffInner(); 806 const cid = inner.getFocus().child_id orelse return 1; 807 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 808 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 809 } 810 const lb = self.listBox(); 811 const cid = lb.getFocus().child_id orelse return null; 812 return lb.children.getIndex(cid); 813 } 814 }; 815 816 // the "a:" navigation link for the commits page walking from commit `oid` within 817 // `identity`, windowing the selected commit's diff from hunk `start`, filtered 818 // to `path` ("" = every file). 819 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, start: usize, path: []const u8) ![]const u8 { 820 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, start, path) orelse return error.RouteTooLong; 821 const url = try route.toUrl(page_arena); 822 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 823 } 824 825 // the "a:" link to the files tab at commit `oid` (an object ref), at its root 826 // directory, within `identity` ("owner/name"). 827 fn filesObjectLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8) ![]const u8 { 828 const route = ui.RoutablePage.repoFilesRoute(identity, .object, oid, "", 0) orelse return error.RouteTooLong; 829 const url = try route.toUrl(page_arena); 830 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 831 } 832 833 // the in-page "ai:" anchor for selecting `oid` in `identity`'s commit list. it 834 // points at the same route as commitsLink but the "ai:" prefix keeps wasm clicks 835 // in-page (crossPageLink ignores it); the href is only followed with js off. 836 fn commitRowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, oid: []const u8, path: []const u8) ![]const u8 { 837 const route = ui.RoutablePage.repoCommitsRoute(identity, .object, oid, 0, path) orelse return error.RouteTooLong; 838 const url = try route.toUrl(page_arena); 839 return std.fmt.allocPrint(page_arena.allocator(), "ai:{s}", .{url}); 840 } 841 842 // the "viewing <ref_or_oid> <value>" banner shown above the log. 843 pub const Header = struct { 844 content: []const u8, 845 846 pub fn init(aa: std.mem.Allocator, ref_or_oid: ui.RoutablePage.RefOrOid, value: []const u8) !Header { 847 return .{ 848 .content = try std.fmt.allocPrint(aa, "viewing {s} {s}", .{ @tagName(ref_or_oid), value }), 849 }; 850 } 851 852 pub const View = struct { 853 box: wgt.Box(ui.Widget), 854 data: *const Header, 855 856 pub fn init(allocator: std.mem.Allocator, data: *const Header, session: *ui.Session) !Header.View { 857 _ = session; 858 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 859 errdefer box.deinit(allocator); 860 861 var text_box = try wgt.TextBox(ui.Widget).init(allocator, data.content, .{ .border_style = .hidden, .wrap_kind = .none }); 862 errdefer text_box.deinit(allocator); 863 try box.children.put(allocator, text_box.getFocus().id, .{ .widget = .{ .text_box = text_box }, .rect = null, .min_size = null }); 864 865 return .{ .box = box, .data = data }; 866 } 867 868 pub fn deinit(self: *Header.View, allocator: std.mem.Allocator) void { 869 self.box.deinit(allocator); 870 } 871 872 pub fn build(self: *Header.View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 873 self.clearGrid(); 874 try self.box.build(allocator, constraint, root_focus); 875 } 876 877 pub fn input(self: *Header.View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 878 _ = self; 879 _ = allocator; 880 _ = key; 881 _ = root_focus; 882 } 883 884 pub fn clearGrid(self: *Header.View) void { 885 self.box.clearGrid(); 886 } 887 888 pub fn getGrid(self: Header.View) ?Grid { 889 return self.box.getGrid(); 890 } 891 892 pub fn getFocus(self: *Header.View) *Focus { 893 return self.box.getFocus(); 894 } 895 }; 896 }; 897