╭ Commits.zig ──────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const builtin = @import("builtin"); 3 const evt = @import("../../event.zig"); 4 const ui = @import("../../ui.zig"); 5 const xit = @import("xit"); 6 const rp = xit.repo; 7 const df = xit.diff; 8 const xitui = xit.xitui; 9 const wgt = xitui.widget; 10 const layout = xitui.layout; 11 const inp = xitui.input; 12 const Grid = xitui.grid.Grid; 13 const Focus = xitui.focus.Focus; 14 15 // how many commits a page shows before a "next" link appears. 16 const page_size = 20; 17 // how many diff hunks a commit shows per page; "load more" reveals another page. 18 const diff_page = 10; 19 20 pub const Hunk = struct { 21 lines: []const []const u8, 22 }; 23 24 // one commit on the current page, with its diff against its first parent 25 // pre-rendered up to the budget so the web client can show it without a repo. 26 pub const Commit = struct { 27 oid: []const u8, 28 date: []const u8, // "YYYY-MM-DD" 29 message: []const u8, // first line only 30 hunks: []const Hunk, 31 // how many hunks `hunks` covers; the resume cursor for "load more". 32 shown_hunks: usize, 33 // true when the diff was truncated at the budget and more hunks remain. 34 has_more: bool, 35 }; 36 37 // "owner/name", so the view can build /repo/owner/name/commits/<oid> links. 38 identity: []const u8, 39 commits: []const Commit, 40 // the first oid of the next page, or null when this is the last page. 41 next_start: ?[]const u8, 42 43 const Self = @This(); 44 45 pub fn init( 46 arena: *std.heap.ArenaAllocator, 47 session: *ui.Session, 48 event_id: *const [evt.event_id_size]u8, 49 identity: []const u8, 50 start_oid: []const u8, 51 // how many hunks the selected commit (the first one, == start_oid) shows; 52 // 0 means the default page. raised by "load more" via the url. 53 after: usize, 54 ) !Self { 55 const aa = arena.allocator(); 56 const hex = std.fmt.bytesToHex(event_id.*, .lower); 57 const empty: Self = .{ 58 .identity = try aa.dupe(u8, identity), 59 .commits = &.{}, 60 .next_start = null, 61 }; 62 63 // no filesystem (wasm) or nowhere to look: empty listing. the wasm path 64 // never calls init anyway — it rebuilds from the serialized snapshot. 65 const io = session.io orelse return empty; 66 const repos_dir = session.repos_dir orelse return empty; 67 68 const repo_path = try std.fs.path.join(aa, &.{ repos_dir, &hex }); 69 70 // walk the log with the arena's backing allocator (transient; the commits 71 // we keep are duped into the page arena so they outlive it). 72 const gpa = arena.child_allocator; 73 var repo = rp.Repo(.xit, .{}).open(io, gpa, .{ .path = repo_path }) catch return empty; 74 defer repo.deinit(io, gpa); 75 76 // page 1 walks from HEAD (null); a later page starts at its first oid. 77 var start_arr: [1][xit.hash.hexLen(.sha1)]u8 = undefined; 78 const start_oids: ?[]const [xit.hash.hexLen(.sha1)]u8 = if (start_oid.len == 0) null else blk: { 79 if (start_oid.len != start_arr[0].len) return empty; // malformed oid 80 @memcpy(&start_arr[0], start_oid); 81 break :blk start_arr[0..1]; 82 }; 83 84 // collect this page's commit metadata, plus a peek at the one after it (its 85 // oid is the next page's start). the diff for each is rendered afterward, so 86 // the log iterator is closed before opening per-commit diff iterators. 87 var buf: [page_size]Commit = undefined; 88 var oids: [page_size][xit.hash.hexLen(.sha1)]u8 = undefined; 89 var count: usize = 0; 90 var next_start: ?[]const u8 = null; 91 { 92 var iter = repo.log(io, gpa, start_oids) catch return empty; 93 defer iter.deinit(); 94 while (try iter.next(gpa)) |commit_object| { 95 defer commit_object.deinit(); 96 if (count == page_size) { 97 next_start = try aa.dupe(u8, &commit_object.oid); 98 break; 99 } 100 const md = commit_object.content.commit.metadata; 101 @memcpy(&oids[count], &commit_object.oid); 102 buf[count] = .{ 103 .oid = try aa.dupe(u8, &commit_object.oid), 104 .date = try formatDate(aa, md.timestamp), 105 .message = try aa.dupe(u8, firstLine(md.message orelse "")), 106 .hunks = &.{}, 107 .shown_hunks = 0, 108 .has_more = false, 109 }; 110 count += 1; 111 } 112 } 113 114 // render each commit's diff (best effort: a failed diff leaves it empty). 115 // the diff machinery isn't wasm-clean and the wasm client never runs it (it 116 // renders from the snapshot), so gate it out of the wasm build. 117 if (!builtin.cpu.arch.isWasm()) { 118 for (buf[0..count], oids[0..count], 0..) |*commit, oid, i| { 119 // the selected commit (the first, == start_oid) shows `after` hunks 120 // when the url asks; the rest show one default page. 121 const max_hunks = if (i == 0 and after > 0) after else diff_page; 122 const rendered = renderCommitDiff(io, gpa, aa, &repo, oid, max_hunks) catch 123 RenderedDiff{ .hunks = &.{}, .shown_hunks = 0, .has_more = false }; 124 commit.hunks = rendered.hunks; 125 commit.shown_hunks = rendered.shown_hunks; 126 commit.has_more = rendered.has_more; 127 } 128 } 129 130 return .{ 131 .identity = empty.identity, 132 .commits = try aa.dupe(Commit, buf[0..count]), 133 .next_start = next_start, 134 }; 135 } 136 137 const RenderedDiff = struct { 138 hunks: []const Hunk, 139 shown_hunks: usize, 140 has_more: bool, 141 }; 142 143 // render a commit's diff against its first parent into `arena`-owned hunks, 144 // emitting at most `max_hunks` hunks (has_more is set when more remain) 145 fn renderCommitDiff( 146 io: std.Io, 147 gpa: std.mem.Allocator, 148 arena: std.mem.Allocator, 149 repo: *rp.Repo(.xit, .{}), 150 oid: [xit.hash.hexLen(.sha1)]u8, 151 max_hunks: usize, 152 ) !RenderedDiff { 153 const empty = RenderedDiff{ .hunks = &.{}, .shown_hunks = 0, .has_more = false }; 154 155 // load the commit so we can diff it against its first parent. 156 var start = [_][xit.hash.hexLen(.sha1)]u8{oid}; 157 var commit_iter = repo.log(io, gpa, start[0..1]) catch return empty; 158 defer commit_iter.deinit(); 159 const commit_object = (commit_iter.next(gpa) catch return empty) orelse return empty; 160 defer commit_object.deinit(); 161 162 const parent_maybe = commit_object.content.commit.metadata.firstParent(); 163 164 var tree_diff = repo.treeDiff(io, gpa, parent_maybe, &commit_object.oid) catch return empty; 165 defer tree_diff.deinit(); 166 167 var file_iter = repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }) catch return empty; 168 169 var hunks: std.ArrayList(Hunk) = .empty; 170 var has_more = false; 171 172 file_loop: while (file_iter.next() catch null) |pair_val| { 173 var pair = pair_val; 174 defer pair.deinit(); 175 176 var hunk_iter = df.HunkIterator(.xit, .{}).init(gpa, &pair.a, &pair.b) catch continue; 177 defer hunk_iter.deinit(gpa); 178 179 // the file header rides on the first of this file's hunks we actually show. 180 var file_header_emitted = false; 181 while (try hunk_iter.next(gpa)) |hunk_val| { 182 var hunk = hunk_val; 183 defer hunk.deinit(gpa); 184 185 if (hunks.items.len >= max_hunks) { 186 has_more = true; 187 break :file_loop; 188 } 189 var lines: std.ArrayList([]const u8) = .empty; 190 if (!file_header_emitted) { 191 for (hunk_iter.header_lines.items) |hl| { 192 try lines.append(arena, try arena.dupe(u8, hl)); 193 } 194 file_header_emitted = true; 195 } 196 try appendHunkLines(arena, &lines, &hunk_iter, &hunk); 197 try hunks.append(arena, .{ .lines = try lines.toOwnedSlice(arena) }); 198 } 199 } 200 201 const shown = hunks.items.len; 202 return .{ .hunks = try hunks.toOwnedSlice(arena), .shown_hunks = shown, .has_more = has_more }; 203 } 204 205 // append a hunk's header and its edit lines 206 fn appendHunkLines( 207 arena: std.mem.Allocator, 208 lines: *std.ArrayList([]const u8), 209 hunk_iter: *df.HunkIterator(.xit, .{}), 210 hunk: *df.Hunk(.xit, .{}), 211 ) !void { 212 const o = hunk.offsets(); 213 try lines.append(arena, try std.fmt.allocPrint(arena, "@@ -{d},{d} +{d},{d} @@", .{ 214 o.del_start, o.del_count, o.ins_start, o.ins_count, 215 })); 216 for (hunk.edits.items) |edit| { 217 const text = switch (edit) { 218 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 219 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 220 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 221 }; 222 defer switch (edit) { 223 .eql, .ins => hunk_iter.line_iter_b.free(text), 224 .del => hunk_iter.line_iter_a.free(text), 225 }; 226 const prefix: u8 = switch (edit) { 227 .eql => ' ', 228 .ins => '+', 229 .del => '-', 230 }; 231 try lines.append(arena, try std.fmt.allocPrint(arena, "{c}{s}", .{ prefix, text })); 232 } 233 } 234 235 // "YYYY-MM-DD" for a unix timestamp. 236 fn formatDate(arena: std.mem.Allocator, timestamp: u64) ![]const u8 { 237 const epoch_secs = std.time.epoch.EpochSeconds{ .secs = timestamp }; 238 const year_day = epoch_secs.getEpochDay().calculateYearDay(); 239 const month_day = year_day.calculateMonthDay(); 240 return std.fmt.allocPrint(arena, "{d:0>4}-{d:0>2}-{d:0>2}", .{ 241 year_day.year, 242 month_day.month.numeric(), 243 month_day.day_index + 1, 244 }); 245 } 246 247 // the first line of a commit message, with surrounding whitespace trimmed. 248 fn firstLine(message: []const u8) []const u8 { 249 const trimmed = std.mem.trim(u8, message, " \t\r\n"); 250 const nl = std.mem.indexOfScalar(u8, trimmed, '\n'); 251 return if (nl) |i| trimmed[0..i] else trimmed; 252 } 253 254 pub const View = struct { 255 // a horizontal split: the commit list on the left and a diff pane on the 256 // right showing the selected commit's diff 257 box: wgt.Box(ui.Widget), // horiz: [list_index] = list scroll, [diff_index] = diff pane 258 data: *const Self, 259 session: *ui.Session, 260 // the commit whose diff the pane currently shows (index into data.commits). 261 diffed_index: ?usize, 262 263 const list_index: usize = 0; 264 const diff_index: usize = 1; 265 const list_max_width: usize = 40; 266 const diff_min_width: usize = 40; 267 // how many rows a page up/down jumps, and the scroll-wheel step. 268 const page_rows = 10; 269 270 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 271 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 272 errdefer box.deinit(allocator); 273 274 // the commit list (one focusable row each), plus a "next" link 275 { 276 var list_scroll = blk: { 277 var list_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 278 errdefer list_box.deinit(allocator); 279 for (data.commits) |commit| { 280 try addRow(allocator, &list_box, commit.message, ""); 281 } 282 if (data.next_start) |next| { 283 try addRow(allocator, &list_box, "next", try commitsLink(session.page_arena, data.identity, next, 0)); 284 } 285 if (list_box.children.count() > 0) list_box.getFocus().child_id = list_box.children.keys()[0]; 286 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = list_box }, .{ .direction = .vert }); 287 }; 288 errdefer list_scroll.deinit(allocator); 289 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 } }); 290 } 291 292 // the diff pane — a frame around a scroll of the hunks 293 { 294 var diff_outer = blk: { 295 var diff_scroll = blk2: { 296 var diff_inner = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 297 errdefer diff_inner.deinit(allocator); 298 break :blk2 try wgt.Scroll(ui.Widget).init(allocator, .{ .box = diff_inner }, .{ .direction = .both }); 299 }; 300 errdefer diff_scroll.deinit(allocator); 301 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = .hidden, .direction = .vert }); 302 errdefer outer.deinit(allocator); 303 try outer.children.put(allocator, diff_scroll.getFocus().id, .{ .widget = .{ .scroll = diff_scroll }, .rect = null, .min_size = null }); 304 break :blk outer; 305 }; 306 errdefer diff_outer.deinit(allocator); 307 try box.children.put(allocator, diff_outer.getFocus().id, .{ .widget = .{ .box = diff_outer }, .rect = null, .min_size = .{ .width = diff_min_width, .height = null } }); 308 } 309 310 box.getFocus().child_id = box.children.keys()[list_index]; 311 312 return .{ 313 .box = box, 314 .data = data, 315 .session = session, 316 .diffed_index = null, 317 }; 318 } 319 320 fn addRow(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), label: []const u8, link: []const u8) !void { 321 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .word }); 322 errdefer row.deinit(allocator); 323 row.getFocus().focusable = true; 324 if (link.len != 0) row.getFocus().kind = .{ .custom = link }; 325 try box.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null, .max_size = .{ .width = null, .height = 5 } }); 326 } 327 328 // one hunk as a focusable multi-line text box. its lines sit flush (the 329 // inner Text rows have no border); the box's hidden border reserves the 330 // space the double border occupies when focused, so focusing doesn't shift 331 // layout. `lines` are joined into the page arena (text_box borrows it). 332 fn addHunkBox(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), lines: []const []const u8) !void { 333 if (lines.len == 0) return; 334 const text = try std.mem.join(self.session.page_arena.allocator(), "\n", lines); 335 var tb = try wgt.TextBox(ui.Widget).init(allocator, text, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 336 errdefer tb.deinit(allocator); 337 tb.getFocus().focusable = true; 338 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 339 } 340 341 // the focusable "load more" row. it's a navigation link to this commit's 342 // route with a larger `after`, so activating it (the host follows the "a:" 343 // link) reloads the page showing more hunks — the same on the TUI and web. 344 fn addLoadMore(self: *View, allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), oid: []const u8, next_after: usize) !void { 345 const link = try commitsLink(self.session.page_arena, self.data.identity, oid, next_after); 346 var tb = try wgt.TextBox(ui.Widget).init(allocator, "load more", .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 347 errdefer tb.deinit(allocator); 348 tb.getFocus().focusable = true; 349 tb.getFocus().kind = .{ .custom = link }; 350 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null }); 351 } 352 353 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 354 self.box.deinit(allocator); 355 } 356 357 fn listScroll(self: *View) *wgt.Scroll(ui.Widget) { 358 return &self.box.children.values()[list_index].widget.scroll; 359 } 360 361 fn listBox(self: *View) *wgt.Box(ui.Widget) { 362 return &self.listScroll().child.box; 363 } 364 365 fn diffOuter(self: *View) *wgt.Box(ui.Widget) { 366 return &self.box.children.values()[diff_index].widget.box; 367 } 368 369 fn diffScroll(self: *View) *wgt.Scroll(ui.Widget) { 370 return &self.diffOuter().children.values()[0].widget.scroll; 371 } 372 373 fn diffInner(self: *View) *wgt.Box(ui.Widget) { 374 return &self.diffScroll().child.box; 375 } 376 377 fn diffActive(self: *View) bool { 378 const cid = self.box.getFocus().child_id orelse return false; 379 return self.box.children.getIndex(cid) == diff_index; 380 } 381 382 // the selected commit's index, or null when the "next" row is selected. 383 fn selectedCommitIndex(self: *View) ?usize { 384 const lb = self.listBox(); 385 const cid = lb.getFocus().child_id orelse return null; 386 const idx = lb.children.getIndex(cid) orelse return null; 387 if (idx >= self.data.commits.len) return null; 388 return idx; 389 } 390 391 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 392 self.clearGrid(); 393 394 // swap the diff pane to the selected commit when it changes. 395 try self.refreshDiff(allocator); 396 397 // mirror the focused commit into the url so it updates as the selection 398 // moves, but only while focus is inside this view (the list or diff). when 399 // focus sits on the header tab, use the page's base /commits route. 400 if (root_focus.grandchild_id) |g| { 401 if (self.box.getFocus().children.contains(g)) { 402 if (self.selectedCommitIndex()) |sel| { 403 // selection moves reset the diff expansion (after = 0). 404 if (ui.RoutablePage.repoCommitsRoute(self.data.identity, self.data.commits[sel].oid, 0)) |route| 405 self.session.data.current_page = route; 406 } 407 } 408 } 409 410 // the selected list row shows a border (the focused TextBox upgrades it 411 // to a double border itself); the rest stay borderless. 412 const lb = self.listBox(); 413 for (lb.children.keys(), lb.children.values()) |id, *child| { 414 switch (child.widget) { 415 .text_box => |*tb| tb.options.border_style = if (lb.getFocus().child_id == id) .single else .hidden, 416 else => {}, 417 } 418 } 419 420 // the selected hunk (or the "load more" row) shows a single border (the 421 // focused TextBox upgrades it to a double border itself, so it stays 422 // single when focus is on the commit list); the rest keep their 423 // space-reserving hidden border. 424 const inner = self.diffInner(); 425 for (inner.children.keys(), inner.children.values()) |id, *child| { 426 switch (child.widget) { 427 .text_box => |*tb| tb.options.border_style = if (inner.getFocus().child_id == id) .single else .hidden, 428 else => {}, 429 } 430 } 431 432 // cap the list at list_max_width only while the diff pane fits beside it. 433 // the box drops the diff when the width can't hold both minimums, so when 434 // it's that narrow we lift the cap and let the list fill the whole width. 435 const both_panes_fit = if (constraint.max_size.width) |w| w >= list_max_width + diff_min_width else true; 436 self.box.children.values()[list_index].max_size = if (both_panes_fit) .{ .width = list_max_width, .height = null } else null; 437 438 // web browsers provide scrolling, so let every widget grow to 439 // its natural size instead of clipping inside the diff scroll 440 const build_constraint = if (self.session.is_terminal) constraint else layout.Constraint{ 441 .min_size = .{ .width = null, .height = null }, 442 .max_size = .{ .width = null, .height = null }, 443 }; 444 try self.box.build(allocator, build_constraint, root_focus); 445 446 // if the diff pane is selected but focus is still elsewhere in this view, 447 // the pane was too narrow to lay out when focus crossed over. it's laid 448 // out now, so focus its hunk. 449 if (self.diffActive() and inner.children.count() > 0) { 450 if (root_focus.grandchild_id) |g| { 451 const in_view = self.box.getFocus().children.contains(g); 452 const in_diff = inner.children.contains(g); 453 if (in_view and !in_diff) 454 try root_focus.setFocus(inner.getFocus().child_id orelse inner.children.keys()[0]); 455 } 456 } 457 } 458 459 fn refreshDiff(self: *View, allocator: std.mem.Allocator) !void { 460 const sel = self.selectedCommitIndex() orelse return; 461 if (self.diffed_index) |d| if (d == sel) return; 462 try self.populateDiff(allocator, sel); 463 self.diffed_index = sel; 464 } 465 466 fn populateDiff(self: *View, allocator: std.mem.Allocator, sel: usize) !void { 467 const commit = self.data.commits[sel]; 468 const inner = self.diffInner(); 469 470 for (inner.children.values()) |*child| child.widget.deinit(allocator); 471 inner.children.clearAndFree(allocator); 472 inner.getFocus().child_id = null; 473 474 for (commit.hunks) |hunk| try self.addHunkBox(allocator, inner, hunk.lines); 475 // the load-more link reloads showing one more page of this commit's hunks. 476 if (commit.has_more) try self.addLoadMore(allocator, inner, commit.oid, commit.shown_hunks + diff_page); 477 478 const sc = self.diffScroll(); 479 sc.x = 0; 480 sc.y = 0; 481 } 482 483 pub fn input(self: *View, allocator: std.mem.Allocator, key: inp.Key, root_focus: *Focus) !void { 484 _ = allocator; 485 if (self.diffActive()) { 486 try self.diffInput(key, root_focus); 487 } else { 488 try self.listInput(key, root_focus); 489 } 490 } 491 492 fn listInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 493 // up/down (and the scroll wheel) move the selection a row; page up/down 494 // jump a fixed amount. right/Enter cross into the diff pane. Enter/clicks 495 // on the "next" row become navigation in the host before reaching here. 496 switch (key) { 497 .arrow_right, .enter => try self.focusDiff(root_focus), 498 .arrow_down => try self.moveSelection(root_focus, 1), 499 .arrow_up => try self.moveSelection(root_focus, -1), 500 .page_down => try self.moveSelection(root_focus, page_rows), 501 .page_up => try self.moveSelection(root_focus, -page_rows), 502 .mouse => |mouse| switch (mouse.action) { 503 .scroll => |dir| try self.moveSelection(root_focus, if (dir == .up) -1 else 1), 504 else => {}, 505 }, 506 else => {}, 507 } 508 } 509 510 fn diffInput(self: *View, key: inp.Key, root_focus: *Focus) !void { 511 const sc = self.diffScroll(); 512 switch (key) { 513 // left scrolls horizontally, then leaves for the list once flush left. 514 .arrow_left => { 515 if (sc.x > 0) { 516 sc.x -= 1; 517 self.clampDiffScroll(); 518 } else try self.focusList(root_focus); 519 }, 520 .arrow_right => { 521 sc.x += 1; 522 self.clampDiffScroll(); 523 }, 524 .arrow_up => try self.moveDiff(root_focus, -1), 525 .arrow_down => try self.moveDiff(root_focus, 1), 526 .page_up => try self.pageDiff(root_focus, -page_rows), 527 .page_down => try self.pageDiff(root_focus, page_rows), 528 // Enter / a click on the "load more" row follow its "a:" link in the 529 // host (it reloads the page with more hunks), so they don't reach here. 530 .mouse => |mouse| switch (mouse.action) { 531 .scroll => |dir| try self.moveDiff(root_focus, if (dir == .up) -1 else 1), 532 else => {}, 533 }, 534 else => {}, 535 } 536 } 537 538 // move focus one hunk in `dir` (+1 down, -1 up). if the adjacent hunk is 539 // already visible, just move focus to it — don't scroll. only when it isn't 540 // visible yet do we scroll one line toward it (and focus it once it appears), 541 // so scrolling never skips past an on-screen hunk. when nothing fits to 542 // scroll (e.g. the web, where the browser scrolls the page), the clamp keeps 543 // the offset put and focus simply steps to the adjacent hunk. 544 fn moveDiff(self: *View, root_focus: *Focus, dir: isize) !void { 545 const inner = self.diffInner(); 546 const keys = inner.children.keys(); 547 if (keys.len == 0) return; 548 const cur: usize = if (root_focus.grandchild_id) |g| (inner.children.getIndex(g) orelse 0) else 0; 549 const target = @as(isize, @intCast(cur)) + dir; 550 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 551 552 if (in_range and self.hunkVisible(@intCast(target))) { 553 try root_focus.setFocus(keys[@intCast(target)]); 554 return; 555 } 556 557 const sc = self.diffScroll(); 558 sc.y += dir; 559 self.clampDiffScroll(); 560 if (in_range and self.hunkVisible(@intCast(target))) 561 try root_focus.setFocus(keys[@intCast(target)]); 562 } 563 564 // page a fixed number of lines, then focus the leading visible hunk 565 // (bottom-most when paging down, top-most when paging up). 566 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) !void { 567 const sc = self.diffScroll(); 568 sc.y += delta; 569 self.clampDiffScroll(); 570 try self.focusVisible(root_focus, delta > 0); 571 } 572 573 // whether hunk `index` is at least partly within the diff viewport, per the 574 // last build's layout rects (content space) and the current scroll offset. 575 fn hunkVisible(self: *View, index: usize) bool { 576 const inner = self.diffInner(); 577 const sc = self.diffScroll(); 578 const vp = sc.grid orelse return false; 579 const r = inner.children.values()[index].rect orelse return false; 580 const top = sc.y; 581 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 582 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 583 } 584 585 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 586 // the bottom-most visible (for downward motion), else the top-most. 587 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) !void { 588 const inner = self.diffInner(); 589 const sc = self.diffScroll(); 590 const vp = sc.grid orelse return; 591 const top = sc.y; 592 const bottom = sc.y + @as(isize, @intCast(vp.size.height)); 593 var chosen: ?usize = null; 594 for (inner.children.keys(), inner.children.values()) |id, *child| { 595 const r = child.rect orelse continue; // content-space layout rect 596 const r_top = r.y; 597 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 598 if (r_bot <= top or r_top >= bottom) continue; // not visible 599 chosen = id; 600 if (!prefer_last) break; // first visible 601 } 602 if (chosen) |id| try root_focus.setFocus(id); 603 } 604 605 // keep the diff scroll within its content, using the last build's grids. the 606 // scroll bar's reserved column/row isn't part of the content viewport, so 607 // exclude it (as scrollToRect does) or the last column/row stays unreachable. 608 fn clampDiffScroll(self: *View) void { 609 const sc = self.diffScroll(); 610 const vp = sc.grid orelse return; 611 const content = sc.child.box.grid orelse return; 612 const view_w = vp.size.width - sc.bar_w; 613 const view_h = vp.size.height - sc.bar_h; 614 const max_y: isize = if (content.size.height > view_h) @intCast(content.size.height - view_h) else 0; 615 const max_x: isize = if (content.size.width > view_w) @intCast(content.size.width - view_w) else 0; 616 sc.y = std.math.clamp(sc.y, 0, max_y); 617 sc.x = std.math.clamp(sc.x, 0, max_x); 618 } 619 620 // enter the diff pane by focusing the hunk that was focused last (the box's 621 // child_id remembers it across the trip to the commit list); fall back to the 622 // first hunk on a fresh diff. the host arrives here on right-arrow or Enter 623 // from the list. a diff with no hunks can't be entered. 624 fn focusDiff(self: *View, root_focus: *Focus) !void { 625 const inner = self.diffInner(); 626 if (inner.children.count() == 0) return; 627 const target = inner.getFocus().child_id orelse inner.children.keys()[0]; 628 try root_focus.setFocus(target); 629 if (root_focus.grandchild_id == target) return; 630 // the diff pane wasn't laid out last build (too narrow to show beside 631 // the list), so its hunks aren't in the focus tree yet. select the pane 632 // at the box level, and the hunk will be focused after the next build. 633 self.box.getFocus().child_id = self.diffOuter().getFocus().id; 634 } 635 636 fn focusList(self: *View, root_focus: *Focus) !void { 637 const lb = self.listBox(); 638 const id = lb.getFocus().child_id orelse (if (lb.children.count() > 0) lb.children.keys()[0] else return); 639 try root_focus.setFocus(id); 640 } 641 642 fn moveSelection(self: *View, root_focus: *Focus, delta: isize) !void { 643 const lb = self.listBox(); 644 const keys = lb.children.keys(); 645 if (keys.len == 0) return; 646 const cur_id = lb.getFocus().child_id orelse return; 647 const cur: isize = @intCast(lb.children.getIndex(cur_id) orelse return); 648 const last: isize = @intCast(keys.len - 1); 649 const next: usize = @intCast(std.math.clamp(cur + delta, 0, last)); 650 if (next == @as(usize, @intCast(cur))) return; 651 try root_focus.setFocus(keys[next]); 652 if (lb.children.values()[next].rect) |rect| self.listScroll().scrollToRect(rect); 653 } 654 655 pub fn clearGrid(self: *View) void { 656 self.box.clearGrid(); 657 } 658 659 pub fn getGrid(self: View) ?Grid { 660 return self.box.getGrid(); 661 } 662 663 pub fn getFocus(self: *View) *Focus { 664 return self.box.getFocus(); 665 } 666 667 // for the parent's "scroll up at the top jumps to the header" check. in the 668 // diff pane that means the first hunk is focused and the diff can't scroll up 669 // any further; otherwise up-arrow stays in the pane (scrolling or moving 670 // between hunks). when the list holds focus, report its selection directly. 671 pub fn getSelectedIndex(self: *View) ?usize { 672 if (self.diffActive()) { 673 const inner = self.diffInner(); 674 const cid = inner.getFocus().child_id orelse return 1; 675 const first_focused = inner.children.count() > 0 and cid == inner.children.keys()[0]; 676 return if (first_focused and self.diffScroll().y == 0) 0 else 1; 677 } 678 const lb = self.listBox(); 679 const cid = lb.getFocus().child_id orelse return null; 680 return lb.children.getIndex(cid); 681 } 682 }; 683 684 // the "a:" navigation link for the commits page starting at `start_oid` within 685 // `identity`, showing the selected commit's first `after` hunks (0 = default). 686 fn commitsLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, start_oid: []const u8, after: usize) ![]const u8 { 687 const route = ui.RoutablePage.repoCommitsRoute(identity, start_oid, after) orelse return error.RouteTooLong; 688 const url = try route.urlAlloc(page_arena); 689 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 690 } 691