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