diff options
Diffstat (limited to 'core/Pick.zig')
| -rw-r--r-- | core/Pick.zig | 180 |
1 files changed, 122 insertions, 58 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index 41a62201..c5b639cd 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -270,10 +270,9 @@ fn unroll(p: *Pardes, pk: *Pick, query: []const u8) void { s.rows = .{ .at = @intCast(@min(pk.at, std.math.maxInt(u16))) }; pk.unrolled = true; pk.shown = no_row; - // The whole window previews a theme: no pane dimmed (SPEC §3.6). + // The whole window previews a theme. if (pk.source == .themes) s.previewing = .full; if (p.paneBySerial(pk.landing)) |id| { - s.previewing.set(id); const pane = p.panes[id].?; pk.aside_row = pane.body.cur_row; pk.aside_col = pane.body.cur_col; @@ -606,78 +605,80 @@ fn rowAt(p: *Pardes, pk: *const Pick, arena: std.mem.Allocator, i: usize) !Row { pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !?TagState.Head { const pk = live(p) orelse return null; const s = &p.view().tag_state; - var text: std.ArrayList(u8) = .empty; - var spans: std.ArrayList(TagState.Span) = .empty; - try text.append(arena, ' '); - var cells: [4][2]u32 = undefined; - var ncells: usize = 0; + var b = try TagState.Builder.of(p, arena, pane); + const text = &b.text; // SPC's cell, then its keys': `␣ f`. - if (pk.keys.len > 0) for (0..pk.keys.len + 1) |k| { - const name = if (k == 0) "␣" else bindings.keyName(pk.keys.slice()[k - 1]); - cells[ncells] = .{ @intCast(text.items.len), @intCast(text.items.len + name.len) }; - ncells += 1; - try text.appendSlice(arena, name); - try text.append(arena, ' '); - }; + if (pk.keys.len > 0) try b.keyCells(.leader, pk.keys.slice()); try text.appendSlice(arena, pk.word.slice()); try text.append(arena, ' '); for (pk.refined[0..pk.nrefined]) |r| try text.print(arena, "{s} | ", .{r.query.slice()}); if (pk.spine == .edit) try text.print(arena, "{s} | Edit ", .{pk.query.slice()}); const caret: u32 = @intCast(text.items.len + s.field.caret); try text.appendSlice(arena, s.field.text()); + // The count at the row's right end (UX D3): `cursor/matches`, `0` with + // none, ` •M` marked. + const counted = text.items.len; const n = count(p, pk); - try text.print(arena, " {d}/{d}", .{ if (n == 0) 0 else s.rows.at + 1, n }); + if (n == 0) try text.append(arena, '0') else try text.print(arena, "{d}/{d}", .{ s.rows.at + 1, n }); if (pk.marks.items.len > 0) try text.print(arena, " •{d}", .{pk.marks.items.len}); if (pk.names.busy() or p.stream.busyFor(pk.id)) try text.appendSlice(arena, " ⋯"); // Themes and actions have no acts and no other sources: no second row. if (pk.source == .themes or pk.source == .actions) { - const hint: u32 = @intCast(text.items.len); - try text.appendSlice(arena, if (pk.source == .themes) " ⏎ wear it · esc" else " ⏎ or 1-9 apply it · esc"); - // The key cells first: the band's spines hang from them. - for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); - try spans.append(arena, .{ .lo = hint, .hi = @intCast(text.items.len), .style = .dim }); - return .{ .text = text.items, .spans = spans.items, .cells = @intCast(ncells), .caret = caret }; + const hints = text.items.len; + try b.styled(if (pk.source == .themes) "⏎ wear it · esc" else "⏎ or 1-9 apply it · esc", .dim); + try b.right(counted, hints); + var h = b.done(); + h.caret = caret; + return h; } - // The second row: the spines go on through it from the key cells, - // then the other sources' words, each switching to it. - try text.append(arena, '\n'); - const prefix = try pardes.tagline.prefixIn(p, arena, pane); - const width = pardes.tagline.tagWidth(p, panes.paneIdOf(p, pane) orelse 0); - const lead = File.displayWidth(p.settings.tab, prefix) % @max(1, width); - var col: usize = 0; - for (cells[0..ncells]) |c| { - const at = lead + File.displayWidth(p.settings.tab, text.items[0..c[0]]); - while (col < at) : (col += 1) try text.append(arena, ' '); - try spans.append(arena, .{ .lo = @intCast(text.items.len), .hi = @intCast(text.items.len + "│".len), .style = .rule }); - try text.appendSlice(arena, "│"); - col += 1; - } - const nspines: u8 = @intCast(spans.items.len); - try text.appendSlice(arena, if (ncells == 0) "" else " "); - for (acts) |act| if (offered(pk, act)) try text.print(arena, "{s} ", .{act.word}); - // The other sources' words, and the `|` before them, dim (UX D4). - const sources: u32 = @intCast(text.items.len); - try text.appendSlice(arena, "|"); - var first = false; - inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes and src != .actions) { - if (src != pk.source) { - if (!first) try text.append(arena, ' '); - first = false; - try text.appendSlice(arena, words.get(src).word); - } + try b.right(counted, text.items.len); + // The second row, one row (UX D2): under the spines from the key + // cells, from two cells right of the last, the acts, `|`, the other + // sources' words, each switching to it, the `|` and the sources dim + // (UX D4); what does not fit is dropped from the right (the sources + // first) and the row ends in a dim `…` (M-l and M-h reach them all). + try b.row(if (b.ncols > 0) b.cols[b.ncols - 1] + 2 else 0); + var offer: [acts.len + 1 + std.enums.values(Source).len][]const u8 = undefined; + var nw: usize = 0; + for (acts) |act| if (offered(pk, act)) { + offer[nw] = act.word; + nw += 1; }; - try spans.append(arena, .{ .lo = sources, .hi = @intCast(text.items.len), .style = .dim }); - for (cells[0..ncells]) |c| try spans.append(arena, .{ .lo = c[0], .hi = c[1], .style = .key }); - return .{ .text = text.items, .spans = spans.items, .cells = nspines, .caret = caret }; + const nacts = nw; + offer[nw] = "|"; + nw += 1; + inline for (comptime std.enums.values(Source)) |src| if (src != .list and src != .messages and src != .themes and src != .actions and src != pk.source) { + offer[nw] = words.get(src).word; + nw += 1; + }; + for (offer[0..nw], 0..) |word, i| { + const gap: usize = @intFromBool(i > 0); + const rest: usize = if (i + 1 < nw) 2 else 0; + if (b.col() + gap + File.displayWidth(p.settings.tab, word) + rest > b.width) { + try b.put(if (gap > 0) " " else ""); + try b.styled("…", .dim); + break; + } + try b.put(if (gap > 0) " " else ""); + if (i >= nacts) try b.styled(word, .dim) else try b.put(word); + } + var h = b.done(); + h.caret = caret; + return h; } -/// The band's rows: as many as the list will show, eleven at most, from -/// what the source has (not what the query keeps: typing never moves the -/// body). +/// The band's rows (UX D6): as many as the list will show, from what the +/// source has (not what the query keeps: typing never moves the body), +/// four at least, eleven at most, and 40 % of the host's column at most +/// (its panes' rows, which a borrow moves between them, never changes). pub fn bandRows(p: *const Pardes) u16 { const pk = live(p) orelse return 0; const have = if (!fuzzy(pk)) Band.Cursor.shown else pk.names.cands.items.len; - const rows: usize = @max(1, @min(Band.Cursor.shown, have)); + const v = p.view(); + const at = @import("layout.zig").findPane(p, TagState.hostOf(p) orelse return 0) orelse return 0; + var column: usize = 0; + for (v.cols[at.col].panes[0..v.cols[at.col].n]) |id| column += v.rects[id].h; + const rows: usize = @max(1, @min(Band.Cursor.shown, @max(4, have), column * 2 / 5)); // Its second step's rows too: the sources, the acts and what they act on. return @intCast(switch (pk.spine) { .sources => @max(rows, spine_rows.len), @@ -1751,7 +1752,10 @@ test "a picker from a held pane unrolls in a reader made for it: its rows in the defer frame.deinit(); const a = frame.allocator(); const tag = try tagOf(p, a, reader); - try std.testing.expect(std.mem.indexOf(u8, tag, " ␣ f Find pick 1/1\n") != null); + // Out of rest no path (UX D1): not the previewed file's, not Keep; + // the head starts the tag's text. + const first = tag[0..std.mem.indexOfScalar(u8, tag, '\n').?]; + try std.testing.expect(std.mem.startsWith(u8, first, "␣ f Find pick ") and std.mem.endsWith(u8, first, " 1/1")); try std.testing.expect(std.mem.indexOf(u8, tag, "│ Walk Look Split Newcol Grep Edit Copy Keep Drop | Grep Recent Panes Symbols All Jumplist") != null); // The body previews the row's file, after a frame of its own. p.update(.tick); @@ -1768,6 +1772,64 @@ test "a picker from a held pane unrolls in a reader made for it: its rows in the try std.testing.expectEqualStrings("pick", pk.query.slice()); } +test "a picker's act row is one row under the spines, two cells right of the last: what does not fit dropped from the right, the sources first, a dim `…` ending it" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 60, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "alpha.zig", "const alpha = 1;\n"); + defer p.gpa.free(a_path); + _ = try p.setTestFile("held\n"); + const asker = p.view().active; + Trail.keep(p.panes[asker].?); + p.stream.engine = .walk; + open(p, asker, .find, "", "", ""); + const reader = p.paneBySerial(live(p).?.landing).?; + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const a = frame.allocator(); + const tag = try tagOf(p, a, reader); + var rows = std.mem.splitScalar(u8, tag, '\n'); + try std.testing.expect(std.mem.startsWith(u8, rows.next().?, "␣ f Find ")); + const acts_row = rows.next().?; + try std.testing.expect(rows.next() == null); + try std.testing.expect(std.mem.startsWith(u8, acts_row, "│ │ Walk Look ")); + try std.testing.expect(std.mem.endsWith(u8, acts_row, " …")); + try std.testing.expect(File.displayWidth(p.settings.tab, acts_row) <= pardes.tagline.tagWidth(p, reader)); + const h = (try head(p, a, p.panes[reader].?)).?; + try std.testing.expect(h.spans[h.spans.len - 1].style == .dim); +} + +test "a picker's band is sized from its list at open, 4 rows at least, and typing never moves it" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 100, .rows = 40 }); + defer p.deinit(); + p.settings.jumps = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const a_path = try testFile(p, &tmp, "alpha.zig", "const alpha = 1;\n"); + defer p.gpa.free(a_path); + _ = try p.setTestFile("held\n"); + const asker = p.view().active; + Trail.keep(p.panes[asker].?); + // In the test's directory: its two files listed. + p.gpa.free(p.panes[asker].?.file.?.path); + p.panes[asker].?.file.?.path = try std.Io.Dir.path.join(p.gpa, &.{ std.Io.Dir.path.dirname(a_path).?, "held.txt" }); + p.stream.engine = .walk; + open(p, asker, .find, "", "", ""); + settle(p); + const reader = p.paneBySerial(live(p).?.landing).?; + p.sync(); + const rows = p.panes[reader].?.band_rows; + // One or two files listed: four rows, ground under them. + try std.testing.expectEqual(@as(u16, 4), rows); + for ("zzq") |c| p.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + settle(p); + p.sync(); + try std.testing.expectEqual(@as(usize, 0), count(p, live(p).?)); + try std.testing.expectEqual(rows, p.panes[reader].?.band_rows); +} + test "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and Backspace undoes it, M-h switches source keeping the query" { const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 140, .rows = 50 }); defer p.deinit(); @@ -1793,7 +1855,8 @@ test "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and try std.testing.expectEqual(@as(u16, 2), s.rows.at); var frame = std.heap.ArenaAllocator.init(std.testing.allocator); defer frame.deinit(); - try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), id), " Refs 3/4 •1\n") != null); + const refs = try tagOf(p, frame.allocator(), id); + try std.testing.expect(std.mem.indexOf(u8, refs, " Refs ") != null and std.mem.indexOf(u8, refs, " 3/4 •1\n") != null); p.update(.{ .key = .{ .cp = 'a', .alt = true } }); try std.testing.expectEqual(@as(usize, 4), pk.marks.items.len); p.update(.{ .key = .{ .cp = 'a', .alt = true } }); @@ -1811,7 +1874,8 @@ test "a picker's keys: Tab marks and moves, M-a all and none, C-SPC refines and settle(p); try std.testing.expectEqual(@as(usize, 1), count(p, pk)); _ = frame.reset(.retain_capacity); - try std.testing.expect(std.mem.indexOf(u8, try tagOf(p, frame.allocator(), id), " Refs alpha | w 1/1\n") != null); + const refined = try tagOf(p, frame.allocator(), id); + try std.testing.expect(std.mem.indexOf(u8, refined, " Refs alpha | w ") != null and std.mem.indexOf(u8, refined, " 1/1\n") != null); // Backspace twice: the `w` goes, then the refinement, its query back. p.update(.{ .key = .{ .cp = Key.backspace } }); p.update(.{ .key = .{ .cp = Key.backspace } }); |
