diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-25 14:19:43 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-25 14:27:26 -0300 |
| commit | 4062e54f04ec6489a6624141df6620d89fe8de27 (patch) | |
| tree | 069bee79dc2bac21e3117e20991e80a8ff5f2563 /core/Prompt.zig | |
| parent | 600f2d071a1e78fa9d3af529395efcf803544fb5 (diff) | |
| download | pardes-4062e54f04ec6489a6624141df6620d89fe8de27.tar.gz pardes-4062e54f04ec6489a6624141df6620d89fe8de27.zip | |
prompt: C-r's history has the band's cursor like any list (UX D5): `▶` and the selection on its newest line holding the field, Down and C-n, Up and C-p move it to the next line it shows with the field kept as typed, Enter takes the line under it: S2's 07c showed the rows `pub` and `self` with no cursor row (both sizes); THE CAUSE (core/Prompt.zig historyStep): a row was `on` only when it was the line Up had recalled into the field (`recall`), so with the field as typed no row was, and Up and Down, the only keys that moved anything, rewrote the field and with it the filter the rows are made by; THE FIX: `recall` is the line the keys are on, in the field when Up or Down brought it with the history rolled up (as before), under the history's cursor when it is unrolled (historyAt: that line, else the newest holding the field); unrolled, Down/C-n and Up/C-p set it to the next older or newer line among the rows shown, the field untouched, and Enter puts the line under the cursor in the field, runs it live and goes on as Enter does (commit, or `:`'s run); no line holding the field (`no line holds it`): no cursor, Enter takes the field; no new state (the dump and the digest as they were); DECISIONS: C-p moves the cursor while the history is unrolled, so a pipe prompt's C-p (run a command that is no known filter) wants C-r first; the cursor reaches the 11 rows shown (a ring keeps 16 lines); TEST FIRST: Prompt.zig "C-r's history has the band's cursor like any list ..." fails on the base at its first cursor check (no row on); a user hit it at every C-r
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_016Y9MNBFhdevVu48bvL9kCE
Diffstat (limited to 'core/Prompt.zig')
| -rw-r--r-- | core/Prompt.zig | 85 |
1 files changed, 80 insertions, 5 deletions
diff --git a/core/Prompt.zig b/core/Prompt.zig index 0a288a79..706ff54d 100644 --- a/core/Prompt.zig +++ b/core/Prompt.zig @@ -48,8 +48,9 @@ const hit = pardes.hit; pub const Op = enum(u8) { search, search_back, select, split, keep, remove, pipe, pipe_to, insert, append, keep_pipe, command }; op: Op = .select, -/// Up and Down's place in its history: 0 the field as typed, n the n-th -/// newest line. +/// The line of its history the keys are on: 0 the field as typed, n the +/// n-th newest line, in the field when Up or Down brought it, under the +/// cursor of the history unrolled (`historyAt`). recall: u8 = 0, /// Ctrl-r: its history unrolled under the tag, filtered by the field. history: bool = false, @@ -257,6 +258,34 @@ pub fn key(p: *Pardes, k: Key) void { const plain = !k.ctrl and !k.alt; const ctrl = k.ctrl and !k.alt; if (hit(k, config.escape)) return cancel(p, pane); + // Its history unrolled is a list like any other (UX D5): Down and C-n, + // 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 (plain and k.cp == Key.enter and at != 0) { + s.field.set(r.get(at).?); + live(p, pane); + } + } if (s.prompt.op == .command and Command.key(p, id, pane, k)) return; if (plain and k.cp == Key.enter) return commit(p, id, pane); if (k.alt and !k.ctrl and k.cp == Key.enter) return asList(p, id, pane); @@ -1236,7 +1265,7 @@ fn historyRows(p: *Pardes) u16 { } /// The prompt's steps in its band: its history, newest first, the lines -/// holding the field, the one Up recalled on. +/// holding the field, its cursor on one. pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.Step) ![]Band.Step { const s = &p.view().tag_state; var made: usize = 0; @@ -1265,22 +1294,37 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.St return out[0 .. made + try historyStep(p, arena, &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); + var back: u8 = 1; + while (back <= r.n) : (back += 1) { + if (std.mem.indexOf(u8, r.get(back).?, s.field.text()) != null) return back; + } + return 0; +} + /// 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, the one Up recalled on. +/// 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); var rows: std.ArrayList(Band.Row) = .empty; const q = s.field.text(); + const at = historyAt(p); var back: u8 = 1; while (back <= r.n and rows.items.len < Band.Cursor.shown) : (back += 1) { const l = r.get(back).?; if (std.mem.indexOf(u8, l, q) == null) continue; - try rows.append(arena, .{ .text = l, .on = back == s.prompt.recall, .index = history_row | back }); + try rows.append(arena, .{ .text = l, .on = back == at, .index = history_row | back }); } if (rows.items.len == 0) try rows.append(arena, .{ .text = if (r.n == 0) "no history yet" else "no line holds it", .dim = true }); out.* = .{ .rows = rows.items }; @@ -1728,3 +1772,34 @@ test "a search's count keeps ` · N lines` after it in the dim ink, and drops it typeKeys(p, "/dirty"); try std.testing.expect(has(try tagOf(p), "1/7 \u{21bb} \u{b7} 5 lines \u{23ce} land")); } + +test "C-r's history has the band's cursor like any list (UX D5): `▶` on its newest line, Down and C-n, Up and C-p move it, Enter takes the line under it" { + const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 }); + defer p.deinit(); + while (p.nextEffect()) |_| {} + _ = try p.setTestFile("self.a = 1;\npub fn b() void {}\nself.c = 2;\n"); + typeKeys(p, "/self"); + p.update(.{ .key = .{ .cp = Key.enter } }); + typeKeys(p, "/pub"); + p.update(.{ .key = .{ .cp = Key.enter } }); + typeKeys(p, "/"); + p.update(.{ .key = .{ .cp = 'r', .ctrl = true } }); + var out: [Band.max_steps]Band.Step = undefined; + const a = p.scratch.allocator(); + var rows = (try steps(p, a, &out))[0].rows; + try std.testing.expectEqual(@as(usize, 2), rows.len); + try std.testing.expectEqualStrings("pub", rows[0].text); + try std.testing.expect(rows[0].on and !rows[1].on); + for ([_]Key{ .{ .cp = 'n', .ctrl = true }, .{ .cp = Key.up }, .{ .cp = Key.down }, .{ .cp = 'p', .ctrl = true }, .{ .cp = 'n', .ctrl = true } }, [_]bool{ false, true, false, true, false }) |k, first| { + p.update(.{ .key = k }); + rows = (try steps(p, a, &out))[0].rows; + try std.testing.expect(rows[0].on == first and rows[1].on == !first); + // The field as typed: the rows stay those it filters. + try std.testing.expectEqualStrings("", p.view().tag_state.field.text()); + } + // Enter: the line under the cursor, `self`, searched for and kept. + p.update(.{ .key = .{ .cp = Key.enter } }); + try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind); + try std.testing.expectEqualStrings("self", p.registers.value('/', 0).?); + try std.testing.expectEqualStrings("self", p.prompt_history.search.get(1).?); +} |
