╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
99 .path = path, 100 .window = try render(repo_kind, repo_opts, io, gpa, aa, repo, if (std.mem.allEqual(u8, &base_oid, '0')) null else &base_oid, head_oid, route.start, path), 101 }; 102 } 103 104 // render a window of the net diff between two commits or trees 105 pub fn render( 106 comptime repo_kind: rp.RepoKind, 107 comptime repo_opts: rp.RepoOpts(repo_kind), 108 io: std.Io, 109 gpa: std.mem.Allocator, 110 arena: std.mem.Allocator, 111 repo: *rp.Repo(repo_kind, repo_opts), 112 base_oid: ?*const [xit.hash.hexLen(repo_opts.hash)]u8, 113 head_oid: [xit.hash.hexLen(repo_opts.hash)]u8, 114 start: usize, 115 path: []const u8, 116 ) !Window { 117 var tree_diff = try repo.treeDiff(io, gpa, base_oid, &head_oid); 118 defer tree_diff.deinit(); 119 120 var file_iter = try repo.filePairs(io, gpa, .{ .tree = .{ .tree_diff = &tree_diff } }); 121 122 var hunks: std.ArrayList(Hunk) = .empty; 123 var index: usize = 0; // running hunk index across all files 124 var has_more = false; 125 126 file_loop: while (try file_iter.next()) |pair_val| { 127 var pair = pair_val; 128 defer pair.deinit(); 129 130 if (path.len != 0 and !std.mem.eql(u8, pair.path, path)) continue; 131 132 var hunk_iter = try df.HunkIterator(repo_kind, repo_opts).init(gpa, &pair.a, &pair.b); 133 defer hunk_iter.deinit(gpa); 134 135 // the path label rides on the first of this file's hunks we actually show. 136 var path_attached = false; 137 while (try hunk_iter.next(gpa)) |hunk_val| { 138 var hunk = hunk_val; 139 defer hunk.deinit(gpa); 140 141 const i = index; 142 index += 1; 143 if (i < start) continue; // before this window 144 if (hunks.items.len >= page_size) { 145 has_more = true; 146 break :file_loop; 147 } 148 const rendered = try renderHunk(repo_kind, repo_opts, arena, &hunk_iter, &hunk); 149 try hunks.append(arena, .{ 150 .path = if (path_attached) null else try arena.dupe(u8, pair.path), 151 .text = rendered.text, 152 .prefixes = rendered.prefixes, 153 }); 154 path_attached = true; 155 } 156 } 157 158 return .{ .hunks = try hunks.toOwnedSlice(arena), .start = start, .has_more = has_more }; 159 } 160 161 // render edits with right-aligned line numbers, alongside each line's prefix 162 fn renderHunk( 163 comptime repo_kind: rp.RepoKind, 164 comptime repo_opts: rp.RepoOpts(repo_kind), 165 arena: std.mem.Allocator, 166 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 167 hunk: *df.Hunk(repo_kind, repo_opts), 168 ) !struct { text: []const u8, prefixes: []const u8 } { 169 var out: std.Io.Writer.Allocating = .init(arena); 170 defer out.deinit(); 171 var prefixes: std.ArrayList(u8) = .empty; 172 var max_num: usize = 1; 173 for (hunk.edits.items) |edit| { 174 const n = editLineNum(edit) + 1; 175 if (n > max_num) max_num = n; 176 } 177 const width = std.fmt.count("{d}", .{max_num}); 178 for (hunk.edits.items, 0..) |edit, i| { 179 const text = switch (edit) { 180 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 181 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 182 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 183 }; 184 const prefix: u8 = switch (edit) { 185 .eql => ' ', 186 .ins => '+', 187 .del => '-', 188 }; 189 if (i != 0) try out.writer.writeByte('\n'); 190 const num = editLineNum(edit) + 1; 191 try out.writer.splatByteAll(' ', width - std.fmt.count("{d}", .{num})); 192 try out.writer.print("{d} {c}{s}", .{ num, prefix, text }); 193 try prefixes.append(arena, prefix); 194 } 195 return .{ .text = try out.toOwnedSlice(), .prefixes = try prefixes.toOwnedSlice(arena) }; 196 } 197 198 // the line number to show for an edit: the new-side number for kept and inserted 199 // lines, the old-side number for deletions (0-based in the diff machinery). 200 fn editLineNum(edit: df.Edit) usize { 201 return switch (edit) { 202 .eql => |e| e.new_line.num, 203 .ins => |e| e.new_line.num, 204 .del => |e| e.old_line.num, 205 }; 206 } 207 208 fn appendWindow(data: @This(), allocator: std.mem.Allocator, session: *ui.Session, box: *wgt.Box(ui.Widget)) !void { 209 if (data.path.len != 0) { 210 try addLink(allocator, box, data.path, ""); 211 try addLink(allocator, box, "← all files", try data.route.link(session.page_arena, 0, "")); 212 } 213 if (data.window.start > 0) try addLink(allocator, box, "← previous", try data.route.link(session.page_arena, data.window.start -| page_size, data.path)); 214 for (data.window.hunks) |hunk| { 215 if (data.path.len == 0) if (hunk.path) |path| try addLink(allocator, box, path, try data.route.link(session.page_arena, 0, path)); 216 if (hunk.text.len != 0) try addHunk(allocator, box, hunk); 217 } 218 if (data.window.has_more) try addLink(allocator, box, "next →", try data.route.link(session.page_arena, data.window.start + page_size, data.path)); 219 } 220 221 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 222 try addSpans(allocator, box, &.{.{ .text = text }}, link); 223 } 224 225 // one span per line, inserted lines green and deleted lines red. the text 226 // box copies the text, so the spans only live until init. 227 fn addHunk(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), hunk: Hunk) !void { 228 var spans: std.ArrayList(wgt.Span) = .empty; 229 defer spans.deinit(allocator); 230 var start: usize = 0; 231 for (hunk.prefixes) |prefix| { 232 // each span keeps its trailing newline so the text box still breaks there 233 const end = if (std.mem.indexOfScalarPos(u8, hunk.text, start, '\n')) |nl| nl + 1 else hunk.text.len; 234 const style: wgt.Style = switch (prefix) { 235 '+' => .{ .fg = .{ .ansi = .green } }, 236 '-' => .{ .fg = .{ .ansi = .red } }, 237 else => .{}, 238 }; 239 try spans.append(allocator, .{ .text = hunk.text[start..end], .style = style }); 240 start = end; 241 } 242 try addSpans(allocator, box, spans.items, ""); 243 } 244 245 fn addSpans(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), spans: []const wgt.Span, link: []const u8) !void { 246 var tb = try wgt.TextBox.initSpans(allocator, spans, .{ .border_style = .single, .round_corners = true, .wrap_kind = .none }); 247 errdefer tb.deinit(allocator); 248 tb.getFocus().mode = .all; 249 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 250 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null, .flex = .shrink }); 251 } 252 253 pub const View = struct { 254 scroll: wgt.Scroll(ui.Widget), 255 256 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 257 var view: View = blk: { 258 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 259 errdefer box.deinit(allocator); 260 break :blk .{ .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }) }; 261 }; 262 errdefer view.deinit(allocator); 263 const box = view.inner(); 264 try data.appendWindow(allocator, session, box); 265 if (box.children.count() == 0) try addLink(allocator, box, "no changes", ""); 266 box.getFocus().child_id = box.children.keys()[0]; 267 return view; 268 } 269 270 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 271 self.scroll.deinit(allocator); 272 } 273 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 274 self.clearGrid(); 275 try self.scroll.build(allocator, constraint, root_focus); 276 } 277 pub fn clearGrid(self: *View) void { 278 self.scroll.clearGrid(); 279 } 280 pub fn getGrid(self: View) ?Grid { 281 return self.scroll.getGrid(); 282 } 283 pub fn getFocus(self: *View) *Focus { 284 return self.scroll.getFocus(); 285 } 286 pub fn atTop(self: *View) bool { 287 const box = self.inner(); 288 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 289 } 290 fn inner(self: *View) *wgt.Box(ui.Widget) { 291 return &self.scroll.child.box; 292 } 293 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 294 _ = allocator; 295 const sc = &self.scroll; 296 const box = self.inner(); 297 if (inp.rowDelta(key, @intCast(box.children.count()))) |delta| return ui.widget.moveRowFocus(box, sc, root_focus, delta); 298 switch (key) { 299 // scroll horizontally within the diff 300 .arrow_left => if (!ui.widget.moveInSelectedRow(box, root_focus, false) and sc.x > 0) { 301 sc.x -= 1; 302 sc.clampToContent(); 303 }, 304 .arrow_right => if (!ui.widget.moveInSelectedRow(box, root_focus, true)) { 305 sc.x += 1; 306 sc.clampToContent(); 307 }, 308 // Enter / a click on a "previous"/"next" row follow its "a:" link in 309 // the host (it reloads the adjacent window), so they don't reach here. 310 else => {}, 311 } 312 } 313 }; 314