╭ Refs.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │http│ ssh │ │ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
1 const std = @import("std"); 2 const evt = @import("../../event.zig"); 3 const ui = @import("../../ui.zig"); 4 const xit = @import("xit"); 5 const rp = xit.repo; 6 const xitui = xit.xitui; 7 const wgt = xitui.widget; 8 const layout = xitui.layout; 9 const Key = xitui.input.Key; 10 const Grid = xitui.grid.Grid; 11 const Focus = xitui.focus.Focus; 12 const inp = @import("../input.zig"); 13 14 // how many refs one window of a column shows. 15 pub const page_size = 20; 16 17 // "owner/name", needed to build the columns' window-navigation links. 18 identity: []const u8, 19 // only one column paginates at a time: `after` is the window start of `kind`'s 20 // column; the other column always starts at 0. 21 kind: ui.RoutablePage.RefKind, 22 after: usize, 23 // the current window of each column, plus the next window's start (null at the 24 // end) so the view can decide whether to show a "next →" row. 25 branches: []const []const u8, 26 tags: []const []const u8, 27 branches_next_after: ?usize, 28 tags_next_after: ?usize, 29 // the columns' headers, in the half-height SubTitle font. 30 branches_label: ui.SubTitle, 31 tags_label: ui.SubTitle, 32 33 const Self = @This(); 34 35 const Window = struct { names: []const []const u8, next_after: ?usize }; 36 37 pub fn init( 38 arena: *std.heap.ArenaAllocator, 39 session: *ui.Session, 40 source_maybe: ?ui.RepoSource, 41 identity: []const u8, 42 kind: ui.RoutablePage.RefKind, 43 after: usize, 44 ) !Self { 45 const aa = arena.allocator(); 46 // the offset only applies to its own column; the other stays at 0. 47 const branches_after: usize = if (kind == .branch) after else 0; 48 const tags_after: usize = if (kind == .tag) after else 0; 49 const branches_label = try ui.SubTitle.init(arena, "branches"); 50 const tags_label = try ui.SubTitle.init(arena, "tags"); 51 const empty: Self = .{ 52 .identity = try aa.dupe(u8, identity), 53 .kind = kind, 54 .after = after, 55 .branches = &.{}, 56 .tags = &.{}, 57 .branches_next_after = null, 58 .tags_next_after = null, 59 .branches_label = branches_label, 60 .tags_label = tags_label, 61 }; 62 63 // no filesystem (wasm) or nowhere to look: empty lists. the wasm path never 64 // calls init anyway — it rebuilds from the serialized snapshot. 65 const io = session.io orelse return empty; 66 const source = source_maybe orelse return empty; 67 68 // open with the arena's backing allocator (transient; the ref names are 69 // duped into the page arena so they outlive the repo handle). 70 const gpa = arena.child_allocator; 71 const branches, const tags = switch (source.repo_kind) { 72 inline else => |repo_kind| blk: { 73 var any_repo = rp.AnyRepo(repo_kind, .{}).open(io, gpa, .{ .path = source.path }) catch return empty; 74 defer any_repo.deinit(io, gpa); 75 76 switch (any_repo) { 77 inline else => |*repo| { 78 // xit seeks each iterator straight to its window; the git 79 // backend can't seek, so it starts at the beginning and 80 // collectWindow discards the entries before the window. 81 const can_seek = repo_kind == .xit; 82 83 var branch_iter = repo.listBranches(io, gpa, if (can_seek) .{ .index = branches_after } else .beginning) catch return empty; 84 defer branch_iter.deinit(io); 85 const b = try collectWindow(io, aa, &branch_iter, branches_after, if (can_seek) 0 else branches_after); 86 87 var tag_iter = repo.listTags(io, gpa, if (can_seek) .{ .index = tags_after } else .beginning) catch return empty; 88 defer tag_iter.deinit(io); 89 const t = try collectWindow(io, aa, &tag_iter, tags_after, if (can_seek) 0 else tags_after); 90 break :blk .{ b, t }; 91 }, 92 } 93 }, 94 }; 95 96 return .{ 97 .identity = empty.identity, 98 .kind = kind, 99 .after = after, 100 .branches = branches.names, 101 .tags = tags.names, 102 .branches_next_after = branches.next_after, 103 .tags_next_after = tags.next_after, 104 .branches_label = branches_label, 105 .tags_label = tags_label, 106 }; 107 } 108 109 // window a ref iterator without materializing the whole list. `skip` entries 110 // are discarded first, for backends whose iterators can't seek. 111 fn collectWindow(io: std.Io, aa: std.mem.Allocator, iter: anytype, after: usize, skip: usize) !Window { 112 for (0..skip) |_| { 113 if (try iter.next(io) == null) break; 114 } 115 var names = try std.ArrayListUnmanaged([]const u8).initCapacity(aa, page_size); 116 while (names.items.len < page_size) { 117 const ref = try iter.next(io) orelse break; 118 names.appendAssumeCapacity(try aa.dupe(u8, ref.name)); 119 } 120 const has_more = (try iter.next(io)) != null; 121 return .{ .names = names.items, .next_after = if (has_more) after + page_size else null }; 122 } 123 124 pub const View = struct { 125 // a horizontal box of two vertical columns. each column is a fixed, 126 // centered, non-focusable label above a Scroll of focusable rows, so the 127 // label stays visible while the rows scroll. focus tracks a single selected 128 // row across both columns: the box's focus path points at the active column, 129 // that column's at its scroll, and the scroll's (its rows') at the row. 130 box: wgt.Box(ui.Widget), 131 data: *const Self, 132 133 const left_col = 0; 134 const right_col = 1; 135 136 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 137 var box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 138 errdefer box.deinit(allocator); 139 140 // the offset only applies to its own column; the other starts at 0. 141 const branches_after: usize = if (data.kind == .branch) data.after else 0; 142 const tags_after: usize = if (data.kind == .tag) data.after else 0; 143 try addColumn(allocator, &box, session, data.identity, .branch, &data.branches_label, data.branches, branches_after, data.branches_next_after); 144 try addColumn(allocator, &box, session, data.identity, .tag, &data.tags_label, data.tags, tags_after, data.tags_next_after); 145 146 var self = View{ .box = box, .data = data }; 147 // select the first row of the first column that has one. 148 for (self.box.children.keys(), self.box.children.values()) |id, *child| { 149 if (child.widget.box.getFocus().child_id != null) { 150 self.box.getFocus().child_id = id; 151 break; 152 } 153 } 154 return self; 155 } 156 157 fn addColumn( 158 allocator: std.mem.Allocator, 159 box: *wgt.Box(ui.Widget), 160 session: *ui.Session, 161 identity: []const u8, 162 kind: ui.RoutablePage.RefKind, 163 label: *const ui.SubTitle, 164 names: []const []const u8, 165 after: usize, 166 next_after: ?usize, 167 ) !void { 168 var column = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 169 errdefer column.deinit(allocator); 170 171 // a fixed, non-focusable header in the SubTitle font, nudged off the 172 // left edge by a one-column space. it stays visible while the rows 173 // scroll below, and declares its height (2 rows) as a min so the (fill) 174 // scroll reserves room for it rather than consuming the whole column. 175 { 176 var header_box = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .horiz }); 177 errdefer header_box.deinit(allocator); 178 { 179 var space = try wgt.Text(ui.Widget).init(allocator, " "); 180 errdefer space.deinit(allocator); 181 try header_box.children.put(allocator, space.getFocus().id, .{ .widget = .{ .text = space }, .rect = null, .min_size = null }); 182 } 183 { 184 var header = try ui.SubTitle.View.init(allocator, label); 185 errdefer header.deinit(allocator); 186 try header_box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .sub_title = header }, .rect = null, .min_size = null }); 187 } 188 try column.children.put(allocator, header_box.getFocus().id, .{ .widget = .{ .box = header_box }, .rect = null, .min_size = .{ .width = null, .height = 2 } }); 189 } 190 191 // a long ref list overflows the page height; the rows scroll on their 192 // own (the label above stays put) so they lay out under a fixed height. 193 { 194 var scroll = blk: { 195 var rows = try wgt.Box(ui.Widget).init(allocator, .{ .border_style = null, .direction = .vert }); 196 errdefer rows.deinit(allocator); 197 198 // window-navigation rows bracket the names: "← previous" off the 199 // first window, "next →" when more remain. each is a full reload. 200 if (after > 0) try addRow(allocator, &rows, "← previous", try windowLink(session.page_arena, identity, kind, after -| page_size)); 201 // each ref name links to the files tab at that ref's root. 202 for (names) |name| try addRow(allocator, &rows, name, try refLink(session.page_arena, identity, kind, name)); 203 if (next_after) |na| try addRow(allocator, &rows, "next →", try windowLink(session.page_arena, identity, kind, na)); 204 205 if (rows.children.count() > 0) rows.getFocus().child_id = rows.children.keys()[0]; 206 207 // fill the column (rows top-aligned, scroll bar pinned to the 208 // edge) rather than shrinking to the widest row. 209 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .box = rows }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 210 }; 211 errdefer scroll.deinit(allocator); 212 213 // the column's focus path skips the label and points at the scroll. 214 if (scroll.getFocus().child_id != null) column.getFocus().child_id = scroll.getFocus().id; 215 try column.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 216 } 217 218 try box.children.put(allocator, column.getFocus().id, .{ .widget = .{ .box = column }, .rect = null, .min_size = null }); 219 } 220 221 // [0] = the fixed label, [1] = the scrollable rows. 222 fn columnScroll(column: *wgt.Box(ui.Widget)) *wgt.Scroll(ui.Widget) { 223 return &column.children.values()[1].widget.scroll; 224 } 225 226 // a focusable row in a column. `link`, when present, is the row's `a:` 227 // navigation kind (the window-navigation rows); ref names pass null. 228 fn addRow(allocator: std.mem.Allocator, col: *wgt.Box(ui.Widget), label: []const u8, link: ?[]const u8) !void { 229 var row = try wgt.TextBox(ui.Widget).init(allocator, label, .{ .border_style = .hidden, .rounded_corners = true, .wrap_kind = .none }); 230 errdefer row.deinit(allocator); 231 row.getFocus().focusable = true; 232 if (link) |l| row.getFocus().kind = .{ .custom = l }; 233 try col.children.put(allocator, row.getFocus().id, .{ .widget = .{ .text_box = row }, .rect = null, .min_size = null }); 234 } 235 236 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 237 self.box.deinit(allocator); 238 } 239 240 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 241 self.clearGrid(); 242 243 // the selected row (in the active column) shows a single border; the 244 // focused TextBox upgrades it to a double border itself. 245 const active_col_id = self.box.getFocus().child_id; 246 for (self.box.children.values()) |*child| { 247 const column = &child.widget.box; 248 const col = &columnScroll(column).child.box; 249 const active = column.getFocus().id == active_col_id; 250 for (col.children.keys(), col.children.values()) |id, *row| { 251 switch (row.widget) { 252 .text_box => |*tb| tb.options.border_style = if (active and col.getFocus().child_id == id) .single else .hidden, 253 else => {}, 254 } 255 } 256 } 257 258 // split the available width evenly between the two columns when it's 259 // known; otherwise let them size to their content. each column's Scroll 260 // has `fill = true`, so it stretches across the column (rows/scroll bar) 261 // rather than hugging the widest row. 262 if (constraint.max_size.width) |w| { 263 const half = w / 2; 264 for (self.box.children.values()) |*child| { 265 child.min_size = .{ .width = half, .height = null }; 266 child.max_size = .{ .width = half, .height = null }; 267 } 268 } else { 269 for (self.box.children.values()) |*child| { 270 child.min_size = null; 271 child.max_size = null; 272 } 273 } 274 275 // clear the incoming min height; each column's Scroll fills the viewport 276 // height itself (via `fill`), keeping its bar pinned to the edge. 277 try self.box.build(allocator, .{ 278 .min_size = .{ .width = null, .height = null }, 279 .max_size = constraint.max_size, 280 }, root_focus); 281 } 282 283 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 284 _ = allocator; 285 // up/down (and the scroll wheel) move within the active column a row; 286 // page up/down and home/end jump; left/right switch columns, keeping the 287 // same row offset. the parent (Repo) intercepts up at the top row to move 288 // focus to the header. 289 if (inp.rowDelta(key, self.columnRowCount())) |delta| { 290 const scroll = self.activeScroll() orelse return; 291 ui.moveRowFocus(&scroll.child.box, scroll, root_focus, delta); 292 return; 293 } 294 switch (key) { 295 .arrow_left => try self.switchColumn(root_focus, left_col), 296 .arrow_right => try self.switchColumn(root_focus, right_col), 297 else => {}, 298 } 299 } 300 301 // the active column's row count, used as a clamp-to-end delta for home/end. 302 fn columnRowCount(self: *View) isize { 303 const col = self.activeColumn() orelse return 0; 304 return @intCast(col.children.count()); 305 } 306 307 fn activeScroll(self: *View) ?*wgt.Scroll(ui.Widget) { 308 const id = self.box.getFocus().child_id orelse return null; 309 const index = self.box.children.getIndex(id) orelse return null; 310 return columnScroll(&self.box.children.values()[index].widget.box); 311 } 312 313 fn activeColumn(self: *View) ?*wgt.Box(ui.Widget) { 314 return &(self.activeScroll() orelse return null).child.box; 315 } 316 317 fn switchColumn(self: *View, root_focus: *Focus, target: usize) !void { 318 if (target >= self.box.children.count()) return; 319 const cur_col = self.activeColumn() orelse return; 320 const target_scroll = columnScroll(&self.box.children.values()[target].widget.box); 321 const target_col = &target_scroll.child.box; 322 // an empty column has nothing to select; ignore. 323 if (target_col.children.count() == 0) return; 324 // keep the same row offset, clamped to the target column's last row. 325 const cur_id = cur_col.getFocus().child_id orelse return; 326 const cur = cur_col.children.getIndex(cur_id) orelse return; 327 const last = target_col.children.count() - 1; 328 const next = @min(cur, last); 329 root_focus.setFocus(target_col.children.keys()[next]); 330 if (target_col.children.values()[next].rect) |rect| target_scroll.scrollToRect(rect); 331 } 332 333 pub fn clearGrid(self: *View) void { 334 self.box.clearGrid(); 335 } 336 337 pub fn getGrid(self: View) ?Grid { 338 return self.box.getGrid(); 339 } 340 341 pub fn getFocus(self: *View) *Focus { 342 return self.box.getFocus(); 343 } 344 345 // the selected row's offset within the active column (0 = first row), so the 346 // parent knows when up should escape to the header. 347 pub fn getSelectedIndex(self: View) ?usize { 348 var copy = self; 349 const col = copy.activeColumn() orelse return null; 350 const cur_id = col.getFocus().child_id orelse return null; 351 return col.children.getIndex(cur_id); 352 } 353 }; 354 355 // the "a:" link to `kind`'s column paginated to `after` within `identity` 356 // ("owner/name"). the other column resets to its first window. 357 fn windowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, after: usize) ![]const u8 { 358 const route = ui.RoutablePage.repoRefsRoute(identity, kind, after) orelse return error.RouteTooLong; 359 const url = try route.urlAlloc(page_arena); 360 return std.fmt.allocPrint(page_arena.allocator(), "a:{s}", .{url}); 361 } 362 363 // the "a:" link to the files tab at ref `name` (a branch or tag) within 364 // `identity` ("owner/name"), at its root directory. the name is percent-encoded 365 // since it can contain a '/'. 366 fn refLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8) ![]const u8 { 367 const aa = page_arena.allocator(); 368 const ref_or_oid: ui.RoutablePage.RefOrOid = switch (kind) { 369 .branch => .branch, 370 .tag => .tag, 371 }; 372 const value = try ui.urlEncodeRef(aa, name); 373 const route = ui.RoutablePage.repoFilesRoute(identity, ref_or_oid, value, "", 0) orelse return error.RouteTooLong; 374 const url = try route.urlAlloc(page_arena); 375 return std.fmt.allocPrint(aa, "a:{s}", .{url}); 376 } 377