summaryrefslogtreecommitdiff
path: root/core/Prompt.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-24 23:01:19 -0300
committerGabriel Schneider <[email protected]>2026-11-24 23:01:19 -0300
commitb40ce1fcea232b99f28462c54cffc9fdd78abbad (patch)
tree56cf607744ab84b0516609909963351c14c1a32f /core/Prompt.zig
parent3da01ac676283c6d20ca6f2c0321b9dfe8cc59cd (diff)
downloadpardes-b40ce1fcea232b99f28462c54cffc9fdd78abbad.tar.gz
pardes-b40ce1fcea232b99f28462c54cffc9fdd78abbad.zip
Prompt: a pipe prompt runs a known filter live, a run a selection (walk UI, pick-ui SPEC.md §7.4, item 4 of line E): `| A-| ! A-! $` from a body run the command as typed at each key when its first word is a known filter (`sort uniq tr cut sed awk jq column fmt fold rev head tail wc grep nl tac cat`), else the count says `<cmd> isn't a known filter · C-p runs it` and C-p runs it live all the same; the live run is a pipe request of its own, off the loop (Pipe.preview, out with the host as `wait` is; a later key's supersedes it, its answer then no one's), its command framed so that each selection's run says its stdout, its stderr and its status (Pipe.framed: sh, the runner's all-or-nothing answer kept as it is); counted `2 lines → 2 · exit 0`, `2 runs · 1 ok · 1 exit 2 <its stderr's first line>`, `keeps 4 of 6` (`$`), `⋯` while it runs; the outputs drawn in the body as ghost lines under the lines they replace (Prompt.ghosts, renderBody's last step: the replaced lines on the diff's deleted ground, each output on the added ground, `!`'s before its selection, `A-!`'s after, the rows below pushed down; drawn, never written: R3), `A-|`'s runs a band step, each `#1 exit 0` and its output; Enter over runs that failed is refused once (` — ⏎ again applies the 1 that worked`), again applies what the live run made, the selections whose runs failed left as they were (Pipe.applyLive: pipeResponse's apply split out, a run's status its verdict), and nothing when every run failed (said, the prompt open); a command not run live, and `A-|` (its point is what it does), runs on Enter as before, one request; wire 39; THE FAULTS THE 40,000-SEED FUZZ MET (each in no code of this item, each a damaged dump's): Core.PendingWrite's `off` past its bytes (dropPendingWrite's count below zero), now a `.place`, the total saturating; the prompt histories' lines undefined past a damaged count (walk.hash read a length of 0xAAAA), now empty by default; TEST FIRST: Prompt.zig "a pipe prompt runs a known filter live, a run a selection ..." and "a live run's framing says each run's output, stderr and status in a real shell" (shell-test); Pipe.zig's tests take the request the keys made last (a known filter ran live before Enter ran it); golden pipe (the count of the live run); core-test 1018/1185 (167 skipped: the framing test wants a shell), shell-test 1232/1235, pane-test 182/184, behaviour-test 274/277 (the failures A's, as on item 1), process-test A's six and none of mine, term-test 17/17, kitty-test 45/45; the fuzz at 40,000 join and 1,779 image seeds: pass; the soak, 5 clients: diverged 0, unequal 0 at rtt 30/80/207, quiet checks 590/0 484/0 700/0, the corpus 22 scenes the same, exit 0
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Diffstat (limited to 'core/Prompt.zig')
-rw-r--r--core/Prompt.zig344
1 files changed, 337 insertions, 7 deletions
diff --git a/core/Prompt.zig b/core/Prompt.zig
index 1c0b1d31..2f42443b 100644
--- a/core/Prompt.zig
+++ b/core/Prompt.zig
@@ -36,6 +36,7 @@ const Places = @import("Places.zig");
const Trail = @import("Trail.zig");
const colors = @import("colors.zig");
const Surface = @import("surface.zig").Surface;
+const Cell = @import("surface.zig").Cell;
const File = panes.File;
const Pardes = pardes.Pardes;
const Pane = panes.Pane;
@@ -68,6 +69,10 @@ counted: u32 = std.math.maxInt(u32),
/// The pane, by serial, whose sam prompt Enter took last with no key since:
/// the next one extends its recipe (0: none).
chain: u32 = 0,
+/// A pipe's: Ctrl-p ran a command that is no known filter live; Enter was
+/// refused once over runs that failed.
+forced: bool = false,
+refused: bool = false,
/// The session's prompt histories: the lines Enter took, newest last.
pub const History = struct {
@@ -80,7 +85,9 @@ pub const Ring = struct {
pub const replica = .{ .len = .{.{ "lines", "n" }} };
/// The lines a history keeps; past it the oldest goes.
pub const max = 16;
- lines: [max]Kind.Buf(Field.max) = undefined,
+ /// Empty, not undefined: a damaged image's count past the lines it
+ /// holds reads empty lines, never undefined bytes.
+ lines: [max]Kind.Buf(Field.max) = @splat(.{}),
n: u8 = 0,
/// `line` the newest, an older copy of it gone.
@@ -224,6 +231,7 @@ pub fn open(p: *Pardes, pane: *Pane, op: Op) void {
s.field.clear();
s.rows = .{};
s.prompt = .{ .op = op, .scope = scope, .chain = s.prompt.chain };
+ if (behavior(op) != null) Pipe.dropLive(p);
}
/// A key the tag at rest takes (keys.handleKey): any but one that opens a
@@ -263,8 +271,14 @@ pub fn key(p: *Pardes, k: Key) void {
s.prompt.history = !s.prompt.history;
return;
}
+ // A command no known filter: Ctrl-p runs it live all the same.
+ if (ctrl and k.cp == 'p' and behavior(s.prompt.op) != null) {
+ s.prompt.forced = true;
+ return live(p, pane);
+ }
if (!s.field.key(k)) return;
s.prompt.recall = 0;
+ s.prompt.forced = false;
live(p, pane);
}
@@ -275,6 +289,13 @@ pub fn key(p: *Pardes, k: Key) void {
fn live(p: *Pardes, pane: *Pane) void {
const s = &p.view().tag_state;
const pat = s.field.text();
+ if (behavior(s.prompt.op)) |how| {
+ // A pipe: run live per selection when it is a known filter (or
+ // Ctrl-p said so), off the loop; Enter is asked again.
+ s.prompt.refused = false;
+ if (pat.len == 0 or !(s.prompt.forced or pure(pat))) return Pipe.dropLive(p);
+ return Pipe.live(p, p.view().active, &pane.body, how, pat);
+ }
if (mode(s.prompt.op) == null and !searches(s.prompt.op)) return;
s.prompt.bad = pat.len > 0 and if (regexp.Regex.compile(pat)) |_| false else |_| true;
if (s.prompt.bad) return;
@@ -329,6 +350,7 @@ fn seekBack(p: *Pardes, pane: *Pane) void {
fn commit(p: *Pardes, id: usize, pane: *Pane) void {
const s = &p.view().tag_state;
const op = s.prompt.op;
+ if (behavior(op) != null and refuses(p, id, pane)) return;
const line = s.field.text();
// The field is the view's: what is kept of it is copied before rest.
var kept: [Field.max]u8 = undefined;
@@ -359,6 +381,11 @@ fn commit(p: *Pardes, id: usize, pane: *Pane) void {
p.registers.put(p.gpa, '|', typed, 0, 1, true) catch |err| return p.reportError(.at(id), "prompt", err);
// `! A-! $` apply, and end the recipe: no Edit line says them.
if (op == .pipe or op == .pipe_to) link(p, pane, op, typed) else s.prompt.chain = 0;
+ // What the live run made, applied (the runs that failed leaving
+ // their selections); else, or `A-|` (its point is what it does),
+ // the command run now.
+ if (op != .pipe_to and Pipe.applyLive(p, pane.serial, typed)) return;
+ Pipe.dropLive(p);
return Pipe.submit(p, id, &pane.body, how, typed);
}
}
@@ -403,6 +430,48 @@ fn asList(p: *Pardes, id: usize, pane: *Pane) void {
panes.Query.open(p, id, .text, typed, true);
}
+/// The filters a pipe prompt runs live as it is typed: commands that
+/// only read their input and write their output (SPEC §7.4).
+const filters = [_][]const u8{ "sort", "uniq", "tr", "cut", "sed", "awk", "jq", "column", "fmt", "fold", "rev", "head", "tail", "wc", "grep", "nl", "tac", "cat" };
+
+fn pure(line: []const u8) bool {
+ const name = commandName(line);
+ for (filters) |f| if (std.mem.eql(u8, f, name)) return true;
+ return false;
+}
+
+fn commandName(line: []const u8) []const u8 {
+ var words = std.mem.tokenizeAny(u8, line, " \t");
+ return words.next() orelse "";
+}
+
+/// The pipe prompt's live run, answered, for its field and `pane`'s text
+/// as they are.
+fn answered(p: *const Pardes, pane: *const Pane, line: []const u8) ?*const Pipe.PendingPipe {
+ const live_run = if (p.pipe.preview) |*l| l else return null;
+ if (live_run.serial != pane.serial or live_run.codes.len == 0 or !std.mem.eql(u8, live_run.typed, line)) return null;
+ if (pane.file == null or pane.file.?.revision != live_run.revision) return null;
+ return live_run;
+}
+
+/// Enter over runs that failed: refused once, the count saying what a
+/// second Enter applies; refused for good when every run failed.
+fn refuses(p: *Pardes, id: usize, pane: *Pane) bool {
+ const s = &p.view().tag_state;
+ if (s.prompt.op == .keep_pipe or s.prompt.op == .pipe_to) return false;
+ const live_run = answered(p, pane, s.field.text()) orelse return false;
+ var failed: usize = 0;
+ for (live_run.codes) |c| failed += @intFromBool(c != 0);
+ if (failed == 0) return false;
+ if (failed == live_run.codes.len) {
+ p.setMessage(.at(id), "every run failed: nothing to apply");
+ return true;
+ }
+ if (s.prompt.refused) return false;
+ s.prompt.refused = true;
+ return true;
+}
+
/// Esc: the prompt goes and the body is as it was before it.
fn cancel(p: *Pardes, pane: *Pane) void {
const op = p.view().tag_state.prompt.op;
@@ -413,6 +482,7 @@ fn cancel(p: *Pardes, pane: *Pane) void {
/// The live effect of prompt `op` on `pane`'s body taken back.
fn undo(p: *Pardes, pane: *Pane, op: Op) void {
+ if (behavior(op) != null) Pipe.dropLive(p);
if (mode(op)) |m| _ = normal.applySelRegex(p, pane, &pane.body, "", m);
pane.nsel_snap = 0;
if (!searches(op)) return;
@@ -761,6 +831,87 @@ fn plainWord(w: *std.Io.Writer, op: Op, line: []const u8) !?[]const u8 {
return if (op == .search or op == .search_back) "Look" else "Exec";
}
+/// A pipe prompt's live run in pane `pane`'s body, its rows painted in `s`
+/// (joinBody's): each replaced selection's lines on the diff's deleted
+/// ground, its run's output after them as ghost lines on the added ground
+/// (`!` before them, `A-!` after, the text kept), the rows below pushed
+/// down and the last ones off the view. Drawn, never written (the
+/// coordinator's R3); a run that failed shows nothing.
+pub fn ghosts(p: *Pardes, s: *Surface, arena: std.mem.Allocator, pane: *Pane, r: pardes.Rect, body_h: u16) !void {
+ const ts = &p.view().tag_state;
+ if (ts.kind != .prompt or ts.host != pane.serial) return;
+ const how = behavior(ts.prompt.op) orelse return;
+ if (how == .ignore or how == .keep) return;
+ const live_run = answered(p, pane, ts.field.text()) orelse return;
+ const f = &pane.file.?;
+ const tab = p.settings.tab;
+ const index = try File.lineIndex(p.gpa, f);
+ const y0 = p.bodyTop(pane, r);
+ const rows: usize = @min(body_h, s.rows -| y0);
+ const first: usize = pane.context_rows;
+ if (rows <= first) return;
+ const w: usize = s.cols;
+ const old = try arena.dupe(Cell, s.cells[(y0 + first) * w .. (y0 + rows) * w]);
+ const grounds = colors.Grounds.of(p.theme());
+ const ink = p.theme().fg orelse p.theme().tag_fg;
+ const x0 = r.x + config.GUTTER + File.gutterWidth(pane);
+ const width = r.w -| (x0 - r.x);
+ const ranges = live_run.ranges[0..live_run.nranges];
+ var out: usize = first;
+ var k: usize = 0;
+ var vr: usize = first;
+ while (vr < rows and out < rows) : (vr += 1) {
+ const line: usize = @intCast(@max(0, pane.wrapAt(tab, @intCast(vr)).line));
+ const opens = vr == first or pane.wrapAt(tab, @intCast(vr - 1)).line != line;
+ const closes = vr + 1 >= rows or pane.wrapAt(tab, @intCast(vr + 1)).line != line;
+ while (k < ranges.len and lineAt(index, @max(ranges[k].anchor, ranges[k].head) -| 1) < line) k += 1;
+ // `!`: its output before the selection's first line.
+ var j = k;
+ if (how == .insert and opens) while (j < ranges.len and lineAt(index, @min(ranges[j].anchor, ranges[j].head)) == line) : (j += 1) {
+ out = ghostRows(s, live_run, j, out, rows, y0, x0, width, grounds.add.row, ink);
+ };
+ if (out >= rows) break;
+ @memcpy(s.cells[(y0 + out) * w ..][0..w], old[(vr - first) * w ..][0..w]);
+ j = k;
+ while (j < ranges.len and lineAt(index, @min(ranges[j].anchor, ranges[j].head)) <= line) : (j += 1) {
+ if (how == .replace and outcome(live_run, j) == 0) s.tint(x0, @intCast(y0 + out), width, grounds.del.row, null);
+ }
+ out += 1;
+ // `|` and `A-!`: after the selection's last line.
+ if (how != .insert and closes) while (k < ranges.len and lineAt(index, @max(ranges[k].anchor, ranges[k].head) -| 1) == line) : (k += 1) {
+ out = ghostRows(s, live_run, k, out, rows, y0, x0, width, grounds.add.row, ink);
+ };
+ }
+ // What was pushed past the view leaves blank rows behind it, none.
+}
+
+fn lineAt(index: []const usize, at: usize) usize {
+ return Places.lineOf(index, at);
+}
+
+/// Run `i`'s exit status (`!` and `A-!` run once for them all).
+fn outcome(live_run: *const Pipe.PendingPipe, i: usize) u8 {
+ return live_run.codes[if (live_run.how.pipes()) i else 0];
+}
+
+/// Range `i`'s output as ghost rows from body row `out`, the rows it
+/// reaches; none for a run that failed.
+fn ghostRows(s: *Surface, live_run: *const Pipe.PendingPipe, i: usize, out_: usize, rows: usize, y0: usize, x0: u16, width: u16, ground: [3]u8, ink: [3]u8) usize {
+ if (outcome(live_run, i) != 0) return out_;
+ var out = out_;
+ const text = live_run.outs[if (live_run.how.pipes()) i else 0];
+ var lines = std.mem.splitScalar(u8, if (std.mem.endsWith(u8, text, "\n")) text[0 .. text.len - 1] else text, '\n');
+ while (lines.next()) |l| {
+ if (out >= rows) break;
+ const y: u16 = @intCast(y0 + out);
+ s.fill(0, y, @intCast(s.cols), 1, .{});
+ s.fill(x0, y, width, 1, .{ .bg = .{ .rgb = ground } });
+ _ = s.print(x0, y, width, l, .{ .fg = .{ .rgb = ink }, .bg = .{ .rgb = ground } });
+ out += 1;
+ }
+ return out;
+}
+
/// The x of the blank after pane `pane`'s line numbers.
fn gutterX(pane: *const Pane, r: pardes.Rect) u16 {
return r.x + config.GUTTER + (File.gutterWidth(pane) -| File.laneCells(pane) -| pane.layers.heat -| 1);
@@ -869,6 +1020,8 @@ pub fn head(p: *Pardes, arena: std.mem.Allocator, pane: *Pane) !TagState.Head {
try text.appendSlice(arena, " ");
try styled(arena, &text, &spans, regexMessage(line), .err);
try text.print(arena, " · the {d} from before shown, dimmed", .{@as(usize, pane.body.nsel) + 1});
+ } else if (behavior(pr.op) != null) {
+ try pipeCount(p, arena, &text, &spans, pane, pr, line);
} else try count(arena, &text, pane, pr);
}
try text.appendSlice(arena, " ");
@@ -911,6 +1064,43 @@ fn count(arena: std.mem.Allocator, text: *std.ArrayList(u8), pane: *Pane, pr: *c
}
}
+/// A pipe prompt's count: `6 lines → 5 · exit 0`, `6 runs · 5 ok · 1 exit 2
+/// <its stderr's first line>`, `keeps 4 of 6`, `⋯` while it runs, or why
+/// it does not run live.
+fn pipeCount(p: *const Pardes, arena: std.mem.Allocator, text: *std.ArrayList(u8), spans: *std.ArrayList(TagState.Span), pane: *const Pane, pr: *const Prompt, line: []const u8) !void {
+ if (!pr.forced and !pure(line)) {
+ try text.appendSlice(arena, " ");
+ try styled(arena, text, spans, commandName(line), .err);
+ return text.appendSlice(arena, " isn't a known filter · C-p runs it");
+ }
+ const live_run = answered(p, pane, line) orelse return text.appendSlice(arena, " ⋯");
+ const n = live_run.codes.len;
+ var ok: usize = 0;
+ var bad: ?usize = null;
+ for (live_run.codes, 0..) |c, i| {
+ if (c == 0) ok += 1 else if (bad == null) bad = i;
+ }
+ if (pr.op == .keep_pipe) return text.print(arena, " keeps {d} of {d}", .{ ok, n });
+ if (bad) |i| {
+ const err = live_run.errs[i];
+ try text.print(arena, " {d} run{s} · {d} ok · {d} exit {d} ", .{ n, plural(n), ok, n - ok, live_run.codes[i] });
+ try styled(arena, text, spans, err[0 .. std.mem.indexOfScalar(u8, err, '\n') orelse err.len], .err);
+ if (pr.refused and ok > 0) try text.print(arena, " — ⏎ again applies the {d} that worked", .{ok});
+ return;
+ }
+ if (pr.op == .pipe_to) return text.print(arena, " {d} run{s} · output below, the text stays", .{ n, plural(n) });
+ var in: usize = 0;
+ for (live_run.inputs) |input| in += lineCount(input);
+ var out: usize = 0;
+ for (live_run.outs) |o| out += lineCount(o);
+ try text.print(arena, " {d} line{s} → {d} · exit 0", .{ in, plural(in), out });
+}
+
+fn lineCount(s: []const u8) usize {
+ if (s.len == 0) return 0;
+ return std.mem.count(u8, s, "\n") + @intFromBool(s[s.len - 1] != '\n');
+}
+
fn plural(n: usize) []const u8 {
return if (n == 1) "" else "s";
}
@@ -919,7 +1109,21 @@ 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 == .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);
if (!s.prompt.history) return 0;
+ return historyRows(p);
+}
+
+/// `A-|`'s runs as rows: each `#1 exit 0`, then its output's lines.
+fn runRows(l: *const Pipe.PendingPipe) u16 {
+ var n: usize = 0;
+ for (l.outs) |o| n += 1 + lineCount(o);
+ return @intCast(@min(Band.Cursor.shown, n));
+}
+
+fn historyRows(p: *Pardes) u16 {
+ const s = &p.view().tag_state;
var n: u16 = 0;
const r = ring(p, s.prompt.op);
for (r.lines[0..r.n]) |l| {
@@ -932,7 +1136,30 @@ pub fn takes(p: *Pardes) u16 {
/// holding the field, the one Up recalled on.
pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.Step) ![]Band.Step {
const s = &p.view().tag_state;
- if (!s.prompt.history) return out[0..0];
+ var made: usize = 0;
+ 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| {
+ var rows: std.ArrayList(Band.Row) = .empty;
+ for (l.outs, l.codes, 0..) |o, c, i| {
+ if (rows.items.len >= Band.Cursor.shown) break;
+ const said = try std.fmt.allocPrint(arena, "#{d} exit {d}", .{ i + 1, c });
+ try rows.append(arena, .{ .text = said, .key = @intCast(said.len) });
+ var lines = std.mem.splitScalar(u8, if (c == 0) o else l.errs[i], '\n');
+ while (lines.next()) |line| {
+ if (rows.items.len >= Band.Cursor.shown) break;
+ if (line.len > 0) try rows.append(arena, .{ .text = line });
+ }
+ }
+ out[made] = .{ .rows = rows.items, .doc = true };
+ made += 1;
+ };
+ if (!s.prompt.history) return out[0..made];
+ return out[0 .. made + try historyStep(p, arena, &out[made])];
+}
+
+/// The history step (Ctrl-r): its lines newest first, those holding the
+/// field, the one Up recalled on.
+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();
@@ -943,8 +1170,8 @@ pub fn steps(p: *Pardes, arena: std.mem.Allocator, out: *[Band.max_steps]Band.St
try rows.append(arena, .{ .text = l, .on = back == s.prompt.recall, .index = 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[0] = .{ .rows = rows.items };
- return out[0..1];
+ out.* = .{ .rows = rows.items };
+ return 1;
}
fn typeKeys(p: *Pardes, keys: []const u8) void {
@@ -1025,7 +1252,7 @@ test "Enter keeps what s found, the pattern in / and in the history Up brings ba
typeKeys(p, "|");
try std.testing.expectEqual(Pane.Prompt.none, pane.prompt);
p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } });
- try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⏎ replace"));
+ try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace"));
p.update(.{ .key = .{ .cp = Key.enter } });
try std.testing.expectEqualStrings("tr a-z A-Z", p.registers.value('|', 0).?);
var asked: ?u32 = null;
@@ -1035,10 +1262,10 @@ test "Enter keeps what s found, the pattern in / and in the history Up brings ba
const request = p.pipe.pipeRequest(asked orelse return error.MissingPipeEffect).?;
try std.testing.expectEqualStrings("tr a-z A-Z", request.command);
try std.testing.expectEqualSlices(u8, "self.b", request.inputs[0]);
- // Esc in a pipe's prompt runs nothing.
+ // Esc in a pipe's prompt applies nothing: the live run dropped.
typeKeys(p, "|cat");
p.update(.{ .key = .{ .cp = Key.escape } });
- while (p.nextEffect()) |effect| try std.testing.expect(effect != .pipe);
+ try std.testing.expect(p.pipe.preview == null);
}
const dirty = "dirty one\nclean\ndirty two dirty\nclean\ndirty three\ndirty four dirty\nclean\ndirty five\n";
@@ -1181,3 +1408,106 @@ test "sam prompts one after another make one recipe word, the Edit line it is; a
Trail.execStep(p, 0, 0);
try std.testing.expect(p.pipe.edit_run != null);
}
+
+/// The pipe request the last key made, its id.
+fn lastPipe(p: *Pardes) ?u32 {
+ var id: ?u32 = null;
+ while (p.nextEffect()) |effect| if (effect == .pipe) {
+ id = effect.pipe.id;
+ };
+ return id;
+}
+
+test "a pipe prompt runs a known filter live, a run a selection: the outputs ghost lines under the lines they replace, counted; one no filter waits for C-p; Enter over runs that failed is refused once, then applies the ones that worked; when every run failed nothing" {
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 200, .rows = 30 });
+ defer p.deinit();
+ var frame = std.heap.ArenaAllocator.init(std.testing.allocator);
+ defer frame.deinit();
+ while (p.nextEffect()) |_| {}
+ const pane = try p.setTestFile("b\na\nc\n");
+ typeKeys(p, "%s[ab]");
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ typeKeys(p, "|");
+ p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } });
+ try std.testing.expect(has(try tagOf(p), " | tr a-z A-Z = Exec Edit |tr a-z A-Z ⋯ ⏎ replace"));
+ const first = lastPipe(p) orelse return error.MissingLiveRun;
+ const asked = p.pipe.pipeRequest(first).?;
+ try std.testing.expectEqual(@as(usize, 2), asked.inputs.len);
+ try std.testing.expect(has(asked.command, "tr a-z A-Z") and has(asked.command, "\\036"));
+ p.update(.{ .pipe_resp = .{ .id = first, .success = true, .outputs = &.{ "B\x1e\x1e0", "A\x1e\x1e0" } } });
+ try std.testing.expect(has(try tagOf(p), " 2 lines → 2 · exit 0 ⏎ replace"));
+ // The body: each output a ghost line under the line it replaces, the
+ // rows below pushed down; the text as it was.
+ p.sync();
+ const s = try p.render(frame.allocator());
+ const r = p.view().rects[0];
+ const x = r.x + config.GUTTER + File.gutterWidth(pane);
+ const y = p.bodyTop(pane, r);
+ for ([_][]const u8{ "b", "B", "a", "A", "c" }, 0..) |want, row| try std.testing.expectEqualStrings(want, s.at(x, y + @as(u16, @intCast(row))).grapheme());
+ try std.testing.expectEqual(pardes.Color{ .rgb = colors.Grounds.of(p.theme()).del.row }, s.at(x, y).style.bg);
+ try std.testing.expectEqual(pardes.Color{ .rgb = colors.Grounds.of(p.theme()).add.row }, s.at(x, y + 1).style.bg);
+ try std.testing.expectEqualStrings("b\na\nc\n", pane.file.?.content);
+ // Enter: what the live run made, applied.
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqualStrings("B\nA\nc\n", pane.file.?.content);
+ try std.testing.expect(lastPipe(p) == null);
+ // No known filter: not run until C-p.
+ typeKeys(p, "|");
+ p.update(.{ .key = .{ .cp = 'r', .text = "rot13" } });
+ try std.testing.expect(has(try tagOf(p), " rot13 isn't a known filter · C-p runs it"));
+ try std.testing.expect(lastPipe(p) == null);
+ p.update(.{ .key = .{ .cp = 'p', .ctrl = true } });
+ const second = lastPipe(p) orelse return error.MissingLiveRun;
+ p.update(.{ .pipe_resp = .{ .id = second, .success = true, .outputs = &.{ "\x1eoops: no\x1e2", "X\x1e\x1e0" } } });
+ try std.testing.expect(has(try tagOf(p), " 2 runs · 1 ok · 1 exit 2 oops: no "));
+ // Enter refused once, said; again, the one that worked applied.
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqual(TagState.Kind.prompt, p.view().tag_state.kind);
+ try std.testing.expect(has(try tagOf(p), "oops: no — ⏎ again applies the 1 that worked"));
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqual(TagState.Kind.rest, p.view().tag_state.kind);
+ try std.testing.expectEqualStrings("B\nX\nc\n", pane.file.?.content);
+ // Every run failed: Enter applies nothing, and says so.
+ typeKeys(p, "|sort");
+ const third = lastPipe(p) orelse return error.MissingLiveRun;
+ p.update(.{ .pipe_resp = .{ .id = third, .success = true, .outputs = &.{ "\x1e\x1e1", "\x1e\x1e1" } } });
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ try std.testing.expectEqual(TagState.Kind.prompt, p.view().tag_state.kind);
+ try std.testing.expect(has(pane.msg[0..pane.msg_len], "every run failed"));
+ p.update(.{ .key = .{ .cp = Key.escape } });
+ try std.testing.expectEqualStrings("B\nX\nc\n", pane.file.?.content);
+ try std.testing.expect(p.pipe.preview == null);
+}
+
+test "a live run's framing says each run's output, stderr and status in a real shell" {
+ if (pardes.Term.ops == null) return error.SkipZigTest; // a shell
+ const p = try Pardes.init(std.testing.allocator, .{ .tty_only = true, .cols = 120, .rows = 30 });
+ defer p.deinit();
+ while (p.nextEffect()) |_| {}
+ _ = try p.setTestFile("one\ntwo\n");
+ typeKeys(p, "%s\\w+");
+ p.update(.{ .key = .{ .cp = Key.enter } });
+ typeKeys(p, "|");
+ p.update(.{ .key = .{ .cp = 'g', .text = "grep o" } });
+ p.update(.{ .key = .{ .cp = 'p', .ctrl = true } });
+ const id = lastPipe(p) orelse return error.MissingLiveRun;
+ const job = try @import("selection_pipe.zig").Job.copy(std.testing.allocator, p.pipe.pipeRequest(id).?);
+ defer job.deinit(std.testing.allocator);
+ var response = @import("selection_pipe.zig").runJob(std.testing.allocator, std.testing.io, job);
+ defer response.deinit(std.testing.allocator);
+ try std.testing.expect(response.success);
+ p.update(.{ .pipe_resp = .{ .id = id, .success = true, .outputs = response.outputs } });
+ const live_run = p.pipe.preview.?;
+ try std.testing.expectEqualSlices(u8, &.{ 0, 0 }, live_run.codes);
+ try std.testing.expectEqualStrings("one\n", live_run.outs[0]);
+ p.update(.{ .key = .{ .cp = Key.backspace } });
+ p.update(.{ .key = .{ .cp = 'x', .text = "x" } });
+ const again = lastPipe(p) orelse return error.MissingLiveRun;
+ const job2 = try @import("selection_pipe.zig").Job.copy(std.testing.allocator, p.pipe.pipeRequest(again).?);
+ defer job2.deinit(std.testing.allocator);
+ var response2 = @import("selection_pipe.zig").runJob(std.testing.allocator, std.testing.io, job2);
+ defer response2.deinit(std.testing.allocator);
+ p.update(.{ .pipe_resp = .{ .id = again, .success = true, .outputs = response2.outputs } });
+ // `grep x` finds nothing: exit 1 for both, an empty stderr.
+ try std.testing.expectEqualSlices(u8, &.{ 1, 1 }, p.pipe.preview.?.codes);
+}