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