╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Diff.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
155 } 156 157 // render edits with right-aligned line numbers 158 fn renderHunk( 159 comptime repo_kind: rp.RepoKind, 160 comptime repo_opts: rp.RepoOpts(repo_kind), 161 arena: std.mem.Allocator, 162 hunk_iter: *df.HunkIterator(repo_kind, repo_opts), 163 hunk: *df.Hunk(repo_kind, repo_opts), 164 ) ![]const u8 { 165 var out: std.Io.Writer.Allocating = .init(arena); 166 defer out.deinit(); 167 var max_num: usize = 1; 168 for (hunk.edits.items) |edit| { 169 const n = editLineNum(edit) + 1; 170 if (n > max_num) max_num = n; 171 } 172 const width = std.fmt.count("{d}", .{max_num}); 173 for (hunk.edits.items, 0..) |edit, i| { 174 const text = switch (edit) { 175 .eql => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 176 .ins => |e| try hunk_iter.line_iter_b.get(e.new_line.num), 177 .del => |e| try hunk_iter.line_iter_a.get(e.old_line.num), 178 }; 179 const prefix: u8 = switch (edit) { 180 .eql => ' ', 181 .ins => '+', 182 .del => '-', 183 }; 184 if (i != 0) try out.writer.writeByte('\n'); 185 const num = editLineNum(edit) + 1; 186 try out.writer.splatByteAll(' ', width - std.fmt.count("{d}", .{num})); 187 try out.writer.print("{d} {c}{s}", .{ num, prefix, text }); 188 } 189 return out.toOwnedSlice(); 190 } 191 192 // the line number to show for an edit: the new-side number for kept and inserted 193 // lines, the old-side number for deletions (0-based in the diff machinery). 194 fn editLineNum(edit: df.Edit) usize { 195 return switch (edit) { 196 .eql => |e| e.new_line.num, 197 .ins => |e| e.new_line.num, 198 .del => |e| e.old_line.num, 199 }; 200 } 201 202 pub fn appendWindow(data: @This(), allocator: std.mem.Allocator, session: *ui.Session, box: *wgt.Box(ui.Widget)) !void { 203 if (data.path.len != 0) { 204 try addLink(allocator, box, data.path, ""); 205 try addLink(allocator, box, "← all files", try data.route.link(session.page_arena, 0, "")); 206 } 207 if (data.window.start > 0) try addLink(allocator, box, "← previous", try data.route.link(session.page_arena, data.window.start -| page_size, data.path)); 208 for (data.window.hunks) |hunk| { 209 if (data.path.len == 0) if (hunk.path) |path| try addLink(allocator, box, path, try data.route.link(session.page_arena, 0, path)); 210 if (hunk.text.len != 0) try addLink(allocator, box, hunk.text, ""); 211 } 212 if (data.window.has_more) try addLink(allocator, box, "next →", try data.route.link(session.page_arena, data.window.start + page_size, data.path)); 213 } 214 215 fn addLink(allocator: std.mem.Allocator, box: *wgt.Box(ui.Widget), text: []const u8, link: []const u8) !void { 216 var tb = try wgt.TextBox.init(allocator, text, .{ .border_style = .single, .rounded_corners = true, .wrap_kind = .none }); 217 errdefer tb.deinit(allocator); 218 tb.getFocus().mode = .all; 219 if (link.len != 0) tb.getFocus().kind = .{ .custom = link }; 220 try box.children.put(allocator, tb.getFocus().id, .{ .widget = .{ .text_box = tb }, .rect = null, .min_size = null, .flex = .shrink }); 221 } 222 223 pub const View = struct { 224 scroll: wgt.Scroll(ui.Widget), 225 session: *ui.Session, 226 227 pub fn initEmpty(allocator: std.mem.Allocator, session: *ui.Session) !View { 228 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 229 errdefer box.deinit(allocator); 230 return .{ 231 .scroll = try wgt.Scroll(ui.Widget).init(allocator, .{ .box = box }, .{ .direction = .both, .web_native = !session.is_terminal, .fill = true }), 232 .session = session, 233 }; 234 } 235 236 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 237 var view = try initEmpty(allocator, session); 238 errdefer view.deinit(allocator); 239 const box = view.inner(); 240 try data.appendWindow(allocator, session, box); 241 if (box.children.count() == 0) try addLink(allocator, box, "no changes", ""); 242 box.getFocus().child_id = box.children.keys()[0]; 243 return view; 244 } 245 246 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 247 self.scroll.deinit(allocator); 248 } 249 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 250 self.clearGrid(); 251 try self.scroll.build(allocator, constraint, root_focus); 252 } 253 pub fn clearGrid(self: *View) void { 254 self.scroll.clearGrid(); 255 } 256 pub fn getGrid(self: View) ?Grid { 257 return self.scroll.getGrid(); 258 } 259 pub fn getFocus(self: *View) *Focus { 260 return self.scroll.getFocus(); 261 } 262 pub fn atTop(self: *View) bool { 263 const box = self.inner(); 264 return self.scroll.y == 0 and (box.children.count() == 0 or box.getFocus().child_id == box.children.keys()[0]); 265 } 266 fn inner(self: *View) *wgt.Box(ui.Widget) { 267 return &self.scroll.child.box; 268 } 269 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 270 _ = allocator; 271 const sc = &self.scroll; 272 switch (key) { 273 // scroll horizontally within the diff 274 .arrow_left => { 275 if (sc.x > 0) { 276 sc.x -= 1; 277 self.scroll.clampToContent(); 278 } 279 }, 280 .arrow_right => { 281 sc.x += 1; 282 self.scroll.clampToContent(); 283 }, 284 .arrow_up => self.moveDiff(root_focus, -1), 285 .arrow_down => self.moveDiff(root_focus, 1), 286 .page_up => self.pageDiff(root_focus, -10), 287 .page_down => self.pageDiff(root_focus, 10), 288 .home => self.jumpDiff(root_focus, false), 289 .end => self.jumpDiff(root_focus, true), 290 // Enter / a click on a "previous"/"next" row follow its "a:" link in 291 // the host (it reloads the adjacent window), so they don't reach here. 292 .mouse => |mouse| switch (mouse.action) { 293 .scroll => |dir| self.moveDiff(root_focus, if (dir == .up) -1 else 1), 294 else => {}, 295 }, 296 else => {}, 297 } 298 } 299 300 // move one step in `delta` (+ down, - up). in the terminal, `delta` is a 301 // line count unless there is a new hunk visible (in which case it is a 302 // hunk count). on the web `delta` is always a hunk count. 303 fn moveDiff(self: *View, root_focus: *Focus, delta: isize) void { 304 const box = self.inner(); 305 const keys = box.children.keys(); 306 if (keys.len == 0) return; 307 const cur: usize = if (root_focus.grandchild_id) |g| (box.children.getIndex(g) orelse 0) else 0; 308 const target = @as(isize, @intCast(cur)) + delta; 309 const in_range = target >= 0 and target < @as(isize, @intCast(keys.len)); 310 311 if (self.session.is_terminal) { 312 if (in_range and self.hunkVisible(@intCast(target))) { 313 root_focus.setFocus(keys[@intCast(target)]); 314 return; 315 } 316 317 const sc = &self.scroll; 318 sc.y += delta * 5; // magnify because it's a line count 319 self.scroll.clampToContent(); 320 if (in_range and self.hunkVisible(@intCast(target))) { 321 root_focus.setFocus(keys[@intCast(target)]); 322 } 323 } else { 324 if (in_range) { 325 root_focus.setFocus(keys[@intCast(target)]); 326 } 327 } 328 } 329 330 // page a fixed number of lines, then focus the leading visible hunk 331 // (bottom-most when paging down, top-most when paging up). in the 332 // terminal, `delta` is a line count, and on the web it's a hunk count. 333 fn pageDiff(self: *View, root_focus: *Focus, delta: isize) void { 334 if (self.session.is_terminal) { 335 const sc = &self.scroll; 336 sc.y += delta * 5; // magnify because it's a line count 337 self.scroll.clampToContent(); 338 self.focusVisible(root_focus, delta > 0); 339 } else { 340 const box = self.inner(); 341 const keys = box.children.keys(); 342 if (keys.len == 0) return; 343 const cur: isize = if (root_focus.grandchild_id) |g| @intCast(box.children.getIndex(g) orelse 0) else 0; 344 const target: usize = @intCast(std.math.clamp(cur + delta, 0, @as(isize, @intCast(keys.len - 1)))); 345 root_focus.setFocus(keys[target]); 346 } 347 } 348 349 // jump to the first or last hunk. on the web the browser scrolls to the 350 // focused hunk; on the terminal pin the scroll to the top/bottom too. 351 fn jumpDiff(self: *View, root_focus: *Focus, to_end: bool) void { 352 const box = self.inner(); 353 const keys = box.children.keys(); 354 if (keys.len == 0) return; 355 if (self.session.is_terminal) { 356 const sc = &self.scroll; 357 sc.y = if (to_end) std.math.maxInt(isize) else 0; 358 self.scroll.clampToContent(); 359 } 360 root_focus.setFocus(if (to_end) keys[keys.len - 1] else keys[0]); 361 } 362 363 // whether hunk `index` is at least partly within the diff viewport, per the 364 // last build's layout rects (content space) and the current scroll offset. 365 fn hunkVisible(self: *View, index: usize) bool { 366 const box = self.inner(); 367 const sc = &self.scroll; 368 const vp = sc.grid orelse return false; 369 const r = box.children.values()[index].rect orelse return false; 370 const top = sc.y; 371 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 372 return (r.y + @as(isize, @intCast(r.size.height))) > top and r.y < bottom; 373 } 374 375 // focus the visible hunk at the scroll's leading edge. `prefer_last` picks 376 // the bottom-most visible (for downward motion), else the top-most. 377 fn focusVisible(self: *View, root_focus: *Focus, prefer_last: bool) void { 378 const box = self.inner(); 379 const sc = &self.scroll; 380 const vp = sc.grid orelse return; 381 const top = sc.y; 382 const bottom = sc.y + @as(isize, @intCast(vp.size.height - sc.bar_h)); 383 var chosen: ?usize = null; 384 for (box.children.keys(), box.children.values()) |id, *child| { 385 const r = child.rect orelse continue; // content-space layout rect 386 const r_top = r.y; 387 const r_bot = r.y + @as(isize, @intCast(r.size.height)); 388 if (r_bot <= top or r_top >= bottom) continue; // not visible 389 chosen = id; 390 if (!prefer_last) break; // first visible 391 } 392 if (chosen) |id| root_focus.setFocus(id); 393 } 394 }; 395