summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/Prompt.zig85
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).?);
+}