summaryrefslogtreecommitdiff
path: root/core/Prompt.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Prompt.zig')
-rw-r--r--core/Prompt.zig95
1 files changed, 52 insertions, 43 deletions
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).?;