diff options
Diffstat (limited to 'core')
| -rw-r--r-- | core/Pick.zig | 141 | ||||
| -rw-r--r-- | core/Prompt.zig | 95 | ||||
| -rw-r--r-- | core/keys.zig | 1 |
3 files changed, 190 insertions, 47 deletions
diff --git a/core/Pick.zig b/core/Pick.zig index 0c6b1c68..1d332f25 100644 --- a/core/Pick.zig +++ b/core/Pick.zig @@ -140,9 +140,12 @@ marks: std.ArrayList(u32) = .empty, refined: [max_refine]Refined = undefined, nrefined: u8 = 0, /// The band's second step: the sources to switch to (M-h), the acts on -/// the picked rows (M-l, M-Enter); `edit`: the field is the act Edit's -/// command, the query kept in `query`. -spine: enum(u8) { rows, sources, acts, edit } = .rows, +/// the picked rows (M-l, M-Enter), its source's earlier queries (C-r); +/// `edit`: the field is the act Edit's command, the query kept in `query`. +spine: enum(u8) { rows, sources, acts, edit, history } = .rows, +/// The line of its source's history the cursor of it unrolled is on +/// (Prompt.historyAt: 0 the newest holding the field). +recall: u8 = 0, /// The act step's cursor, among the acts offered. act_at: u8 = 0, /// The places Walk queued (C-i steps them in the landing pane): each a @@ -805,6 +808,7 @@ pub fn bandRows(p: *const Pardes) u16 { for (acts) |act| n += @intFromBool(offered(pk, act)); break :acts @max(rows, n); }, + .history => @max(rows, pardes.Prompt.historyRows(p.prompt_history.pick.getPtrConst(pk.source), p.view().tag_state.field.text())), .rows, .edit => rows, }); } @@ -814,7 +818,8 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator) ![]Band.Step { const pk = live(p) orelse return &.{}; const s = &p.view().tag_state; const n = count(p, pk); - const out = try arena.alloc(Band.Step, if (pk.spine == .sources or pk.spine == .acts or pk.source == .lists) 2 else 1); + const out = try arena.alloc(Band.Step, if (pk.spine == .sources or pk.spine == .acts or pk.spine == .history or pk.source == .lists) 2 else 1); + if (pk.spine == .history) _ = try pardes.Prompt.historyStep(arena, p.prompt_history.pick.getPtrConst(pk.source), s.field.text(), pk.recall, &out[1]); if (pk.spine == .acts) out[1] = try actStep(p, pk, arena); if (pk.source == .lists) out[1] = try listedStep(p, pk, arena); if (pk.spine == .sources) { @@ -1050,7 +1055,10 @@ pub fn rollUp(p: *Pardes) void { const pk = live(p) orelse return; const s = &p.view().tag_state; if (pk.spine != .edit) pk.query.set(s.field.text()); + // Its query its source's history's (UX L2's C-r): Lists keeps none. + if (pk.source != .lists) p.prompt_history.pick.getPtr(pk.source).add(pk.query.slice()); pk.spine = .rows; + pk.recall = 0; pk.at = s.rows.at; pk.unrolled = false; pk.walking = null; @@ -1216,6 +1224,29 @@ pub fn key(p: *Pardes, k: Key) void { return; } if (pk.spine == .acts) return actKey(p, pk, k); + // Its source's earlier queries unrolled (C-r, UX L2): Down and Up + // move their cursor, Enter puts the one under it in the field, C-r + // and Esc (closeHistory) roll them up; anything else is the field's, + // which filters both. + if (pk.spine == .history) { + const r = p.prompt_history.pick.getPtr(pk.source); + if (pardes.Prompt.historyKey(r, s.field.text(), &pk.recall, k)) return; + if (plain and k.cp == Key.enter) { + const at = pardes.Prompt.historyAt(r, s.field.text(), pk.recall); + pk.spine = .rows; + pk.recall = 0; + if (at == 0) return; + s.field.set(r.get(at).?); + return filter(p, pk); + } + if (ctrl and k.cp == 'r') { + pk.spine = .rows; + return; + } + pk.recall = 0; + if (s.field.key(k)) filter(p, pk); + return; + } // Themes: Enter wears the one under the cursor; actions: Enter, or an // action's digit with the field empty, applies it; the rows and the // field. @@ -1273,6 +1304,11 @@ pub fn key(p: *Pardes, k: Key) void { } if (ctrl and (k.cp == ' ' or k.cp == '@')) return refine(p, pk, false); if (ctrl and k.cp == 'q') return keep(p, pk); + if (ctrl and k.cp == 'r') { + pk.spine = .history; + pk.recall = 0; + return; + } if (alt and k.cp == 'p') { pk.previews = !pk.previews; pk.shown = no_row; @@ -1288,6 +1324,16 @@ pub fn key(p: *Pardes, k: Key) void { if (s.field.key(k)) filter(p, pk); } +/// Esc with the live picker's history unrolled (keys.tagStateKey): that +/// rolled up alone, the picker as it was; false with none. +pub fn closeHistory(p: *Pardes) bool { + const pk = live(p) orelse return false; + if (pk.spine != .history) return false; + pk.spine = .rows; + pk.recall = 0; + return true; +} + /// The source spine (M-h): a key a source, the query kept. const spine_rows = [_]struct { key: u8, word: []const u8, source: ?Source }{ .{ .key = 'f', .word = "Find", .source = .find }, @@ -3150,3 +3196,90 @@ test "Lists (UX L'3): a picker of the kept pickers, newest first, each its list try std.testing.expectEqualStrings("J 1/2", (try rowAt(p, live(p).?, a, 0)).text); p.update(.{ .key = .{ .cp = Key.escape } }); } + +test "Ctrl-r in a picker unrolls its source's earlier queries (UX L2's last line): recorded as a picker lands and as it is left with its field typed; the band's rows filtered by the field, Enter puts one in the field, Esc closes the band; each source its own; Lists none; as many as a prompt's history keeps" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 160, .rows = 50 }); + defer p.deinit(); + p.settings.jumps = .walk; + p.stream.engine = .walk; + var tmp = std.testing.tmpDir(.{}); + defer tmp.cleanup(); + const w_path = try testFile(p, &tmp, "words.zig", "in_word\n"); + defer p.gpa.free(w_path); + const base = try p.setTestFile("text\n"); + p.gpa.free(base.file.?.path); + base.file.?.path = try testFile(p, &tmp, "main.zig", "text\n"); + var frame = std.heap.ArenaAllocator.init(std.testing.allocator); + defer frame.deinit(); + const History = struct { + /// The history band's rows, joined by `|`; "" with none shown. + fn rows(pp: *Pardes, a: std.mem.Allocator) ![]const u8 { + const band = try steps(pp, a); + if (band.len < 2) return ""; + var out: std.ArrayList(u8) = .empty; + for (band[1].rows, 0..) |row, i| try out.print(a, "{s}{s}", .{ if (i > 0) "|" else "", row.text }); + return out.items; + } + }; + const ctrl_r: Key = .{ .cp = 'r', .ctrl = true }; + const Type = struct { + fn keys(pp: *Pardes, text: []const u8) void { + for (text) |c| pp.update(.{ .key = .{ .cp = c, .text = &[_]u8{c} } }); + } + }; + // SPC f f words, Enter: `words` is Find's. + Type.keys(p, " ffwords"); + settle(p); + p.update(.{ .key = .{ .cp = Key.enter } }); + p.sync(); + try std.testing.expect(live(p) == null); + // A Grep left with its field typed: `in_w` is Grep's. + Type.keys(p, " fgin_w"); + p.update(.{ .key = .{ .cp = Key.escape } }); + // SPC f f, Ctrl-r: Find's alone. + Type.keys(p, " ff"); + p.update(.{ .key = ctrl_r }); + try std.testing.expectEqualStrings("words", try History.rows(p, frame.allocator())); + // Esc closes the band, back to the picker; Ctrl-r again, Enter: the + // line in the field, the rows filtered by it. + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expect(live(p) != null); + try std.testing.expectEqualStrings("", try History.rows(p, frame.allocator())); + p.update(.{ .key = ctrl_r }); + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expect(live(p) != null); + try std.testing.expectEqualStrings("words", p.view().tag_state.field.text()); + try std.testing.expectEqualStrings("", try History.rows(p, frame.allocator())); + settle(p); + try std.testing.expectEqual(@as(usize, 1), count(p, live(p).?)); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Grep's: its own line. + Type.keys(p, " fg"); + p.update(.{ .key = ctrl_r }); + try std.testing.expectEqualStrings("in_w", try History.rows(p, frame.allocator())); + p.update(.{ .key = .{ .cp = Key.escape } }); + p.update(.{ .key = .{ .cp = Key.escape } }); + // Lists keeps none: left with its field typed, nothing recorded. + Type.keys(p, " flzz"); + p.update(.{ .key = .{ .cp = Key.escape } }); + try std.testing.expectEqual(@as(u8, 0), p.prompt_history.pick.get(.lists).n); + // The bound: a prompt's history's, the oldest gone, newest first; the + // rows filtered by the field. + for (0..pardes.Prompt.Ring.max + 1) |i| { + Type.keys(p, " ff"); + var q: [8]u8 = undefined; + Type.keys(p, try std.fmt.bufPrint(&q, "q{d}", .{i})); + p.update(.{ .key = .{ .cp = Key.escape } }); + } + const find = p.prompt_history.pick.get(.find); + try std.testing.expectEqual(@as(u8, pardes.Prompt.Ring.max), find.n); + try std.testing.expectEqualStrings("q16", find.get(1).?); + try std.testing.expectEqualStrings("q1", find.get(pardes.Prompt.Ring.max).?); + Type.keys(p, " ffq1"); + p.update(.{ .key = ctrl_r }); + const shown = try History.rows(p, frame.allocator()); + try std.testing.expect(std.mem.startsWith(u8, shown, "q16|q15|q14|q13|q12|q11|q10|q1")); + try std.testing.expect(std.mem.indexOf(u8, shown, "words") == null); + p.update(.{ .key = .{ .cp = Key.escape } }); + p.update(.{ .key = .{ .cp = Key.escape } }); +} diff --git a/core/Prompt.zig b/core/Prompt.zig index afe9f78e..b76b2d9a 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -78,11 +78,14 @@ refused: bool = false, /// `:`'s (Command.zig): what its rows are of, and where Tab writes. command: Command = .{}, -/// The session's prompt histories: the lines Enter took, newest last. +/// The session's prompt histories: the lines Enter took, newest last; and +/// each picker source's queries, as its picker landed or was left typed +/// (Pick.zig, UX L2). pub const History = struct { search: Ring = .{}, pipe: Ring = .{}, command: Ring = .{}, + pick: std.EnumArray(pardes.Pick.Source, Ring) = .initFill(.{}), }; pub const Ring = struct { @@ -262,25 +265,9 @@ pub fn key(p: *Pardes, k: Key) void { // Up and C-p move its cursor to the next line it shows, older or newer, // the field kept as typed; Enter takes the line under it. if (s.prompt.history) { - const down = (plain and k.cp == Key.down) or (ctrl and k.cp == 'n'); - const up = (plain and k.cp == Key.up) or (ctrl and k.cp == 'p'); const r = ring(p, s.prompt.op); - const at = historyAt(p); - if (down or up) { - var newer: u8 = 0; - var older: u8 = 0; - var rows: usize = 0; - var back: u8 = 1; - while (back <= r.n and rows < Band.Cursor.shown) : (back += 1) { - if (std.mem.indexOf(u8, r.get(back).?, s.field.text()) == null) continue; - rows += 1; - if (back < at) newer = back; - if (back > at and older == 0) older = back; - } - const to = if (down) older else newer; - if (to != 0) s.prompt.recall = to; - return; - } + if (historyKey(r, s.field.text(), &s.prompt.recall, k)) return; + const at = historyAt(r, s.field.text(), s.prompt.recall); if (plain and k.cp == Key.enter and at != 0) { s.field.set(r.get(at).?); live(p, pane); @@ -1261,11 +1248,12 @@ fn plural(n: usize) []const u8 { /// lines the field is in, eleven at most. pub fn takes(p: *Pardes) u16 { const s = &p.view().tag_state; - if (s.prompt.op == .command) return @max(Command.takes(p), if (s.prompt.history) historyRows(p) else 0); + const rows = historyRows(ring(p, s.prompt.op), s.field.text()); + if (s.prompt.op == .command) return @max(Command.takes(p), if (s.prompt.history) rows else 0); if (s.prompt.op == .pipe_to) if (p.panes[TagState.hostOf(p) orelse return 0]) |pane| if (answered(p, pane, s.field.text())) |l| - return @max(runRows(l), if (s.prompt.history) historyRows(p) else 0); + return @max(runRows(l), if (s.prompt.history) rows else 0); if (!s.prompt.history) return 0; - return historyRows(p); + return rows; } /// `A-|`'s runs as rows: each `#1 exit 0`, then its output's lines. @@ -1275,12 +1263,12 @@ fn runRows(l: *const Pipe.PendingPipe) u16 { return @intCast(@min(Band.Cursor.shown, n)); } -fn historyRows(p: *Pardes) u16 { - const s = &p.view().tag_state; +/// The rows history `r` unrolled takes: its lines holding `q`, one at +/// least, eleven at most. +pub fn historyRows(r: *const Ring, q: []const u8) u16 { var n: u16 = 0; - const r = ring(p, s.prompt.op); for (r.lines[0..r.n]) |l| { - if (std.mem.indexOf(u8, l.slice(), s.field.text()) != null) n += 1; + if (std.mem.indexOf(u8, l.slice(), q) != null) n += 1; } return @max(1, @min(Band.Cursor.shown, n)); } @@ -1292,8 +1280,9 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.St var made: usize = 0; // `:`'s history before its rows and their doc, which takes the width // left after the others. + const r = ring(p, s.prompt.op); if (s.prompt.op == .command) { - if (s.prompt.history) made = try historyStep(p, arena, &out[0]); + if (s.prompt.history) made = try historyStep(arena, r, s.field.text(), s.prompt.recall, &out[0]); return out[0 .. made + try Command.steps(p, arena, out[made..])]; } if (s.prompt.op == .pipe_to) if (p.panes[TagState.hostOf(p) orelse return out[0..0]]) |pane| if (answered(p, pane, s.field.text())) |l| { @@ -1312,35 +1301,55 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.St made += 1; }; if (!s.prompt.history) return out[0..made]; - return out[0 .. made + try historyStep(p, arena, &out[made])]; + return out[0 .. made + try historyStep(arena, r, s.field.text(), s.prompt.recall, &out[made])]; } -/// The line of its history the cursor of the history unrolled is on: the -/// one the keys moved to (`recall`), else the newest holding the field; -/// 0 with none. -fn historyAt(p: *Pardes) u8 { - const s = &p.view().tag_state; - if (s.prompt.recall != 0) return s.prompt.recall; - const r = ring(p, s.prompt.op); +/// The line of history `r` the cursor of it unrolled is on: the one the +/// keys moved to (`recall`), else the newest holding the field `q`; 0 +/// with none. +pub fn historyAt(r: *const Ring, q: []const u8, recall: u8) u8 { + if (recall != 0) return recall; var back: u8 = 1; while (back <= r.n) : (back += 1) { - if (std.mem.indexOf(u8, r.get(back).?, s.field.text()) != null) return back; + if (std.mem.indexOf(u8, r.get(back).?, q) != null) return back; } return 0; } +/// Down and Ctrl-n, Up and Ctrl-p in history `r` unrolled (UX D5: a list +/// like any other): its cursor `recall` to the next line it shows, older +/// or newer, the field `q` kept as typed; true when the key was one. +pub fn historyKey(r: *const Ring, q: []const u8, recall: *u8, k: Key) bool { + const plain = !k.ctrl and !k.alt; + const ctrl = k.ctrl and !k.alt; + const down = (plain and k.cp == Key.down) or (ctrl and k.cp == 'n'); + const up = (plain and k.cp == Key.up) or (ctrl and k.cp == 'p'); + if (!down and !up) return false; + const at = historyAt(r, q, recall.*); + var newer: u8 = 0; + var older: u8 = 0; + var rows: usize = 0; + var back: u8 = 1; + while (back <= r.n and rows < Band.Cursor.shown) : (back += 1) { + if (std.mem.indexOf(u8, r.get(back).?, q) == null) continue; + rows += 1; + if (back < at) newer = back; + if (back > at and older == 0) older = back; + } + const to = if (down) older else newer; + if (to != 0) recall.* = to; + return true; +} + /// A history step's row's index: its line's place in the ring with this /// bit, which a click tells from the other steps' rows (Command.point). pub const history_row: u16 = 0x8000; -/// The history step (Ctrl-r): its lines newest first, those holding the -/// field, its cursor on one. -fn historyStep(p: *Pardes, arena: std.mem.Allocator, out: *Band.Step) !usize { - const s = &p.view().tag_state; - const r = ring(p, s.prompt.op); +/// History `r`'s step (Ctrl-r): its lines newest first, those holding the +/// field `q`, its cursor on one. +pub fn historyStep(arena: std.mem.Allocator, r: *const Ring, q: []const u8, recall: u8, out: *Band.Step) !usize { var rows: std.ArrayList(Band.Row) = .empty; - const q = s.field.text(); - const at = historyAt(p); + const at = historyAt(r, q, recall); var back: u8 = 1; while (back <= r.n and rows.items.len < Band.Cursor.shown) : (back += 1) { const l = r.get(back).?; diff --git a/core/keys.zig b/core/keys.zig index ea25c2f7..803b2b3c 100644 --- a/core/keys.zig +++ b/core/keys.zig @@ -586,6 +586,7 @@ fn tagStateKey(p: *Pardes, key: Key) void { const v = p.view(); const s = &v.tag_state; if (s.kind == .prompt) return Prompt.key(p, key); + if (s.kind == .pick and hit(key, config.escape) and @import("Pick.zig").closeHistory(p)) return; if (TagState.hostOf(p) == null or hit(key, config.escape)) return TagState.toRest(p); switch (s.kind) { .rest, .prompt => unreachable, |
