╭─────────────╮ │scroll to top│ ╰─────────────╯ ╭ Refs.zig ─────────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯
75 // the window root only applies to its own column; a ref name arrives 76 // url-encoded, and the iterator seeks to the first name at or after it. 77 const decoded = std.Uri.percentDecodeInPlace(try aa.dupe(u8, from)); 78 for ( 79 [_]ui.RoutablePage.RefKind{ .branch, .tag }, 80 [_]*Column{ &result.branches, &result.tags }, 81 ) |col_kind, column| { 82 const col_from: []const u8 = if (kind == col_kind) decoded else ""; 83 // an unreadable listing leaves the column empty. the names are sorted, 84 // so those with the prefix are contiguous from the prefix itself. 85 var iter = listRefs(repo_kind, repo_opts, repo, io, gpa, col_kind, iterStart(laterKey(prefix, col_from))) catch continue; 86 defer iter.deinit(); 87 var names = try std.ArrayListUnmanaged([]const u8).initCapacity(aa, page_size); 88 while (try iter.next()) |ref| { 89 if (!std.mem.startsWith(u8, ref.name, prefix)) break; 90 if (names.items.len == page_size) { 91 column.next = try aa.dupe(u8, ref.name); 92 break; 93 } 94 names.appendAssumeCapacity(try aa.dupe(u8, ref.name)); 95 } 96 column.names = names.items; 97 if (col_from.len != 0) { 98 var prev_iter = listRefs(repo_kind, repo_opts, repo, io, gpa, col_kind, iterStart(prefix)) catch continue; 99 defer prev_iter.deinit(); 100 column.prev = try prevRoot(aa, &prev_iter, prefix, col_from); 101 } 102 } 103 return result; 104 } 105 106 fn listRefs( 107 comptime repo_kind: rp.RepoKind, 108 comptime repo_opts: rp.RepoOpts(repo_kind), 109 repo: *rp.Repo(repo_kind, repo_opts), 110 io: std.Io, 111 gpa: std.mem.Allocator, 112 kind: ui.RoutablePage.RefKind, 113 start: xit.ref.RefIteratorStart, 114 ) !xit.ref.RefIterator(repo_kind, repo_opts) { 115 return switch (kind) { 116 .branch => repo.listBranches(io, gpa, start), 117 .tag => repo.listTags(io, gpa, start), 118 }; 119 } 120 121 fn iterStart(from: []const u8) xit.ref.RefIteratorStart { 122 return if (from.len == 0) .beginning else .{ .key = from }; 123 } 124 125 // the later of two ref names, so a window never starts before the prefix 126 fn laterKey(a: []const u8, b: []const u8) []const u8 { 127 return if (std.mem.lessThan(u8, a, b)) b else a; 128 } 129 130 // the root of the window before the one starting at `from` (decoded) among the 131 // names with `prefix`: the ref a page before it ("" = the first window), or null 132 // when nothing precedes it. `iter` starts at the prefix. 133 fn prevRoot(aa: std.mem.Allocator, iter: anytype, prefix: []const u8, from: []const u8) !?[]const u8 { 134 var ring: [page_size][]const u8 = undefined; 135 var count: usize = 0; 136 while (try iter.next()) |ref| { 137 if (!std.mem.startsWith(u8, ref.name, prefix) or !std.mem.lessThan(u8, ref.name, from)) break; 138 ring[count % page_size] = try aa.dupe(u8, ref.name); 139 count += 1; 140 } 141 if (count == 0) return null; 142 if (count <= page_size) return ""; 143 return ring[count % page_size]; 144 } 145 146 pub const View = struct { 147 // a vertical box: the search sub-header above a horizontal box of two 148 // columns. each column is a fixed label above a 149 // Scroll of a WordFlow, so the names wrap across the column and scroll 150 // beneath the label. focus points at the header or the active column and, 151 // in its flow, the selected name. 152 box: wgt.Box(ui.Widget), 153 data: *const Self, 154 session: *ui.Session, 155 156 const header_index = 0; 157 const columns_index = 1; 158 const left_col = 0; 159 const right_col = 1; 160 161 pub fn init(allocator: std.mem.Allocator, data: *const Self, session: *ui.Session) !View { 162 var outer = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 163 errdefer outer.deinit(allocator); 164 165 // both backends list refs in sorted order, so a prefix is just a seek 166 { 167 var header_scroll = try ui.widget.HeaderScroll.init(allocator, session); 168 errdefer header_scroll.deinit(allocator); 169 const header = header_scroll.row(); 170 { 171 var search_box = try ui.widget.SearchBox.init(allocator, session, " search ", "search", data.search); 172 errdefer search_box.deinit(allocator); 173 header.getFocus().child_id = search_box.getFocus().id; 174 try header.children.put(allocator, search_box.getFocus().id, .{ .widget = .{ .search_box = search_box }, .rect = null, .min_size = ui.widget.SearchBox.min_size }); 175 } 176 // the spacer keeps the box at its own width 177 { 178 var spacer = try ui.widget.Spacer.init(allocator); 179 errdefer spacer.deinit(allocator); 180 try header.children.put(allocator, spacer.getFocus().id, .{ .widget = .{ .spacer = spacer }, .rect = null, .min_size = null }); 181 } 182 // a row taller than the box, leaving a blank line above the columns 183 try outer.children.put(allocator, header_scroll.getFocus().id, .{ .widget = .{ .header_scroll = header_scroll }, .rect = null, .min_size = .{ .width = null, .height = 4 } }); 184 } 185 186 // with both columns empty, the empty message stands in for them 187 if (data.isEmpty()) { 188 var center = try ui.widget.initEmpty(allocator); 189 errdefer center.deinit(allocator); 190 try outer.children.put(allocator, center.getFocus().id, .{ .widget = .{ .center = center }, .rect = null, .min_size = null }); 191 } else { 192 var split = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 193 errdefer split.deinit(allocator); 194 195 const search = data.search orelse ""; 196 try addColumn(allocator, &split, session, data.identity, search, .branch, &data.branches); 197 try addColumn(allocator, &split, session, data.identity, search, .tag, &data.tags); 198 199 // select the first name of the first column that has one 200 for (split.children.keys(), split.children.values()) |id, *child| { 201 if (child.widget.box.getFocus().child_id != null) { 202 split.getFocus().child_id = id; 203 break; 204 } 205 } 206 try outer.children.put(allocator, split.getFocus().id, .{ .widget = .{ .box = split }, .rect = null, .min_size = null }); 207 } 208 209 // search results start in the box so the term can be refined right away 210 outer.getFocus().child_id = outer.children.keys()[if (data.search != null) header_index else columns_index]; 211 return .{ .box = outer, .data = data, .session = session }; 212 } 213 214 fn addColumn( 215 allocator: std.mem.Allocator, 216 box: *wgt.Box(ui.Widget), 217 session: *ui.Session, 218 identity: []const u8, 219 search: []const u8, 220 kind: ui.RoutablePage.RefKind, 221 data: *const Column, 222 ) !void { 223 var column = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .vert }); 224 errdefer column.deinit(allocator); 225 226 // a fixed, non-focusable header in the SubTitle font, nudged off the 227 // left edge by a one-column space. it declares its height (2 rows plus 228 // a blank one beneath) as a min so the (fill) scroll reserves room for it. 229 { 230 var header_box = try wgt.Box(ui.Widget).init(allocator, .{ .border = null, .direction = .horiz }); 231 errdefer header_box.deinit(allocator); 232 { 233 var space = try wgt.Text.init(allocator, " "); 234 errdefer space.deinit(allocator); 235 try header_box.children.put(allocator, space.getFocus().id, .{ .widget = .{ .text = space }, .rect = null, .min_size = null }); 236 } 237 { 238 var header = try ui.SubTitle.View.init(allocator, &data.label); 239 errdefer header.deinit(allocator); 240 try header_box.children.put(allocator, header.getFocus().id, .{ .widget = .{ .sub_title = header }, .rect = null, .min_size = null }); 241 } 242 try column.children.put(allocator, header_box.getFocus().id, .{ .widget = .{ .box = header_box }, .rect = null, .min_size = .{ .width = null, .height = 3 } }); 243 } 244 245 // each name links to the files tab at that ref's root. window 246 // navigation brackets them: "← previous" off the first window, "next →" 247 // when more remain, each a full reload. 248 { 249 var items: std.ArrayList(ui.widget.WordFlow.Item) = .empty; 250 defer items.deinit(allocator); 251 if (data.prev) |p| try items.append(allocator, .{ .text = "← previous", .link = try windowLink(session.page_arena, identity, kind, p, search) }); 252 for (data.names) |name| try items.append(allocator, .{ .text = name, .link = try refLink(session.page_arena, identity, kind, name) }); 253 if (data.next) |n| try items.append(allocator, .{ .text = "next →", .link = try windowLink(session.page_arena, identity, kind, n, search) }); 254 255 var scroll = blk: { 256 var flow = try ui.widget.WordFlow.init(allocator); 257 errdefer flow.deinit(allocator); 258 try flow.setItems(allocator, items.items); 259 break :blk try wgt.Scroll(ui.Widget).init(allocator, .{ .word_flow = flow }, .{ .direction = .vert, .web_native = !session.is_terminal, .fill = true }); 260 }; 261 errdefer scroll.deinit(allocator); 262 263 // the column's focus path skips the label and points at the flow 264 if (scroll.getFocus().child_id != null) column.getFocus().child_id = scroll.getFocus().id; 265 try column.children.put(allocator, scroll.getFocus().id, .{ .widget = .{ .scroll = scroll }, .rect = null, .min_size = null }); 266 } 267 268 try box.children.put(allocator, column.getFocus().id, .{ .widget = .{ .box = column }, .rect = null, .min_size = null }); 269 } 270 271 pub fn deinit(self: *View, allocator: std.mem.Allocator) void { 272 self.box.deinit(allocator); 273 } 274 275 pub fn build(self: *View, allocator: std.mem.Allocator, constraint: layout.Constraint, root_focus: *Focus) !void { 276 self.clearGrid(); 277 278 // split the available width evenly between the two columns when it's 279 // known; otherwise let them size to their content. 280 if (!self.data.isEmpty()) { 281 if (constraint.max_size.width) |w| { 282 const half = w / 2; 283 for (self.columns().children.values()) |*child| { 284 child.min_size = .{ .width = half, .height = null }; 285 child.max_size = .{ .width = half, .height = null }; 286 } 287 } else { 288 for (self.columns().children.values()) |*child| { 289 child.min_size = null; 290 child.max_size = null; 291 } 292 } 293 } 294 295 // clear the incoming min height; each column's Scroll fills the viewport 296 // height itself (via `fill`), keeping its bar pinned to the edge. 297 try self.box.build(allocator, .{ 298 .min_size = .{ .width = null, .height = null }, 299 .max_size = constraint.max_size, 300 }, root_focus); 301 } 302 303 pub fn input(self: *View, allocator: std.mem.Allocator, key: Key, root_focus: *Focus) !void { 304 if (self.headerActive()) { 305 const search_box = self.searchBox(); 306 if (inp.vertDirection(key) == .down) { 307 // the empty message has nothing to focus 308 if (!self.data.isEmpty()) root_focus.setFocus(self.columns().getFocus().id); 309 return; 310 } 311 if (key == .enter) { 312 const text = try search_box.text(allocator); 313 defer allocator.free(text); 314 return self.submit(text); 315 } 316 return search_box.input(allocator, key, root_focus); 317 } 318 // arrows and the scroll wheel move the selection within the active 319 // flow. left from a row's first name and right from its last cross to 320 // the other column's same row. up from a first row reaches the search 321 // box. 322 const index = self.activeIndex() orelse return; 323 const flow = self.flowAt(index); 324 const cur = flow.indexOfFocusId(flow.focus.child_id orelse return) orelse return; 325 // a flow last laid out at zero width has names without positions 326 if (cur >= flow.rects.items.len) return; 327 const count = flow.text_boxes.items.len; 328 const cur_y = flow.rects.items[cur].y; 329 switch (inp.vertDirection(key)) { 330 .up => { 331 if (flow.rowStep(cur, false)) |i| self.focusName(index, i, root_focus) else _ = self.focusHeader(root_focus); 332 return; 333 }, 334 .down => { 335 if (flow.rowStep(cur, true)) |i| self.focusName(index, i, root_focus); 336 return; 337 }, 338 .none => {}, 339 } 340 switch (key) { 341 .arrow_left => if (index == right_col and rowStart(flow, cur)) { 342 self.crossTo(left_col, cur_y, root_focus); 343 } else if (cur > 0) self.focusName(index, cur - 1, root_focus), 344 .arrow_right => if (index == left_col and rowEnd(flow, cur)) { 345 self.crossTo(right_col, cur_y, root_focus); 346 } else if (cur + 1 < count) self.focusName(index, cur + 1, root_focus), 347 .home => self.focusName(index, 0, root_focus), 348 .end => self.focusName(index, count - 1, root_focus), 349 else => {}, 350 } 351 } 352 353 // both lists narrowed to `text`, each from its first window. an empty box 354 // on an unsearched page has nothing to clear. 355 fn submit(self: *View, text: []const u8) !void { 356 if (text.len == 0 and self.data.search == null) return; 357 const route = ui.RoutablePage.repoRefsRoute(self.data.identity, .branch, "") orelse return; 358 try self.session.navigate(route.withSearch(text) orelse return); 359 } 360 361 fn searchBox(self: *View) *ui.widget.SearchBox { 362 return &self.box.children.values()[header_index].widget.header_scroll.row().children.values()[0].widget.search_box; 363 } 364 365 fn headerActive(self: *View) bool { 366 return self.box.getFocus().child_id == self.box.children.keys()[header_index]; 367 } 368 369 fn columns(self: *View) *wgt.Box(ui.Widget) { 370 return &self.box.children.values()[columns_index].widget.box; 371 } 372 373 fn activeIndex(self: *View) ?usize { 374 const cols = self.columns(); 375 const id = cols.getFocus().child_id orelse return null; 376 return cols.children.getIndex(id); 377 } 378 379 // [0] = the fixed label, [1] = the scrolling flow 380 fn scrollAt(self: *View, index: usize) *wgt.Scroll(ui.Widget) { 381 return &self.columns().children.values()[index].widget.box.children.values()[1].widget.scroll; 382 } 383 384 fn flowAt(self: *View, index: usize) *ui.widget.WordFlow { 385 return &self.scrollAt(index).child.word_flow; 386 } 387 388 fn rowStart(flow: *ui.widget.WordFlow, item: usize) bool { 389 const rects = flow.rects.items; 390 return item == 0 or rects[item - 1].y != rects[item].y; 391 } 392 393 fn rowEnd(flow: *ui.widget.WordFlow, item: usize) bool { 394 const rects = flow.rects.items; 395 return item + 1 == rects.len or rects[item + 1].y != rects[item].y; 396 } 397 398 // land on the other column's row at or above the current one: its first 399 // name when moving right, its last when moving left. names are laid out in 400 // order, so that row ends at the last name no lower than the current one. 401 fn crossTo(self: *View, target: usize, from_y: isize, root_focus: *Focus) void { 402 // the other column may have no names at all 403 const rects = self.flowAt(target).rects.items; 404 if (rects.len == 0) return; 405 var last: usize = 0; 406 for (rects, 0..) |rect, i| { 407 if (rect.y <= from_y) last = i; 408 } 409 var first = last; 410 while (first > 0 and rects[first - 1].y == rects[last].y) first -= 1; 411 self.focusName(target, if (target == right_col) first else last, root_focus); 412 } 413 414 fn focusName(self: *View, index: usize, item: usize, root_focus: *Focus) void { 415 const flow = self.flowAt(index); 416 root_focus.setFocus(flow.text_boxes.items[item].getFocus().id); 417 // the browser scrolls natively; the terminal brings the name into view 418 if (self.session.is_terminal and item < flow.rects.items.len) self.scrollAt(index).scrollToRect(flow.rects.items[item]); 419 } 420 421 pub fn clearGrid(self: *View) void { 422 self.box.clearGrid(); 423 } 424 425 pub fn getGrid(self: View) ?Grid { 426 return self.box.getGrid(); 427 } 428 429 pub fn getFocus(self: *View) *Focus { 430 return self.box.getFocus(); 431 } 432 433 // up leaves the tab from the search box 434 pub fn atTop(self: *View) bool { 435 return self.headerActive(); 436 } 437 438 pub fn focusHeader(self: *View, root_focus: *Focus) bool { 439 root_focus.setFocus(self.searchBox().getFocus().id); 440 return true; 441 } 442 }; 443 444 // the "a:" link to `kind`'s column windowed from ref `name` (raw; "" = the 445 // first window) within `identity` ("owner:name"). the other column resets to 446 // its first window. 447 fn windowLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8, search: []const u8) ![]const u8 { 448 const encoded = try ui.urlEncodeRef(page_arena.allocator(), name); 449 const window = ui.RoutablePage.repoRefsRoute(identity, kind, encoded) orelse return error.RouteTooLong; 450 const route = window.withSearch(search) orelse return error.RouteTooLong; 451 const url = try route.toUrl(page_arena); 452 return page_arena.allocator().print("a:{s}", .{url}); 453 } 454 455 // the "a:" link to the files tab at ref `name` (a branch or tag) within 456 // `identity` ("owner:name"), at its root directory. the name is percent-encoded 457 // since it can contain a '/'. 458 fn refLink(page_arena: *std.heap.ArenaAllocator, identity: []const u8, kind: ui.RoutablePage.RefKind, name: []const u8) ![]const u8 { 459 const aa = page_arena.allocator(); 460 const ref_or_oid: ui.RoutablePage.RefOrOid = switch (kind) { 461 .branch => .branch, 462 .tag => .tag, 463 }; 464 const value = try ui.urlEncodeRef(aa, name); 465 const route = ui.RoutablePage.repoFilesRoute(identity, ref_or_oid, value, "", 0) orelse return error.RouteTooLong; 466 const url = try route.toUrl(page_arena); 467 return aa.print("a:{s}", .{url}); 468 } 469