summaryrefslogtreecommitdiff
path: root/core/Pipe.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/Pipe.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/Pipe.zig')
-rw-r--r--core/Pipe.zig122
1 files changed, 112 insertions, 10 deletions
diff --git a/core/Pipe.zig b/core/Pipe.zig
index 70d551d0..d0413c08 100644
--- a/core/Pipe.zig
+++ b/core/Pipe.zig
@@ -18,12 +18,17 @@ const Pipe = @This();
/// A pipe's run, an Edit's and its writes, out with the host: errands of
/// the session that performs them, not a dump's (core/walk.zig).
-pub const replica = .{ .authority = .{ "wait", "edit_run", "writes" } };
+pub const replica = .{ .authority = .{ "wait", "preview", "edit_run", "writes" } };
/// Bumped for every request, so a late answer to an older one is ignored.
seq: u32 = 0,
/// The request in flight, and what it needs to apply its answer.
wait: ?PendingPipe = null,
+/// A pipe prompt's live run (Prompt.zig, pick-ui SPEC §7.4): each
+/// selection through the command as typed, each run's outcome its own
+/// (`framed`), drawn as ghost lines and counted; Enter applies the ones
+/// that worked from it.
+preview: ?PendingPipe = null,
/// An Edit waiting on its `<`, `|` and `>` commands, which share the ids.
edit_run: ?*edit_cmd.Pending = null,
/// What an Edit's `w` took, for the `edit_write` effect to write.
@@ -31,6 +36,7 @@ writes: std.ArrayList(edit_cmd.Write) = .empty,
pub fn deinit(pipe: *Pipe, gpa: std.mem.Allocator) void {
if (pipe.wait) |*wait| wait.deinit();
+ if (pipe.preview) |*running| running.deinit();
if (pipe.edit_run) |pending| pending.deinit(gpa);
for (pipe.writes.items) |*w| w.deinit(gpa);
pipe.writes.deinit(gpa);
@@ -72,6 +78,12 @@ pub const PendingPipe = struct {
explicit: bool,
how: modal.Normal.PipeBehavior,
nranges: u16,
+ /// A live run's: the command as typed, and once answered each run's
+ /// output, stderr and exit status (`framed`).
+ typed: []u8 = &.{},
+ outs: [][]const u8 = &.{},
+ errs: [][]const u8 = &.{},
+ codes: []u8 = &.{},
pub fn deinit(wait: *PendingPipe) void {
wait.arena.deinit();
@@ -105,6 +117,27 @@ pub fn submitPipe(p: *Pardes, id: usize) void {
/// Pipe text `t` of pane `id` through `command`, `how` says: one run per
/// selection, off the loop.
pub fn submit(p: *Pardes, id: usize, t: *Text, how: modal.Normal.PipeBehavior, command: []const u8) void {
+ send(p, id, t, how, command, false);
+}
+
+/// The same run, live under a pipe prompt (`preview`): its outcome per
+/// selection, the text left alone.
+pub fn live(p: *Pardes, id: usize, t: *Text, how: modal.Normal.PipeBehavior, command: []const u8) void {
+ send(p, id, t, how, command, true);
+}
+
+/// The live run let go: its answer, when it comes, is no one's.
+pub fn dropLive(p: *Pardes) void {
+ if (p.pipe.preview) |*old| old.deinit();
+ p.pipe.preview = null;
+}
+
+/// A live run's command: its stdout as it is, then its stderr and its exit
+/// status after a record separator each, so that each selection's run says
+/// how it went where the runner's answer is all or nothing (as `$` does).
+const framed = "exec 3>&1; e=$( {{ (\n{s}\n) 1>&3; }} 2>&1 ); s=$?; printf '\\036%s\\036%s' \"$e\" \"$s\"";
+
+fn send(p: *Pardes, id: usize, t: *Text, how: modal.Normal.PipeBehavior, command: []const u8, preview: bool) void {
const pane = p.panes[id] orelse return;
if (t.what == .body) {
const f = pane.file orelse return;
@@ -130,15 +163,20 @@ pub fn submit(p: *Pardes, id: usize, t: *Text, how: modal.Normal.PipeBehavior, c
// `$` wants a verdict per selection where the runner's is all or
// nothing: the command runs in a subshell whose exit status becomes the
// output, so the runner always succeeds and the answer says which kept
- const command_copy = (if (how == .keep)
+ const command_copy = (if (preview)
+ std.fmt.allocPrint(arena, framed, .{command})
+ else if (how == .keep)
std.fmt.allocPrint(arena, "(\n{s}\n) >/dev/null; echo $?", .{command})
else
arena.dupe(u8, command)) catch return arena_state.deinit();
+ const typed: []u8 = if (preview) arena.dupe(u8, command) catch return arena_state.deinit() else &.{};
const cwd_copy = arena.dupe(u8, Pardes.paneDir(pane)) catch return arena_state.deinit();
p.pipe.seq +%= 1;
- if (p.pipe.wait) |*old| old.deinit();
- p.pipe.wait = .{
+ const slot = if (preview) &p.pipe.preview else &p.pipe.wait;
+ if (slot.*) |*old| old.deinit();
+ slot.* = .{
+ .typed = typed,
.id = p.pipe.seq,
.pane = .at(id),
.serial = pane.serial,
@@ -165,6 +203,9 @@ pub fn pipeRequest(pipe: *const Pipe, id: u32) ?selection_pipe.Request {
if (pipe.wait) |*wait| {
if (wait.id == id) return wait.request();
}
+ if (pipe.preview) |*live_run| {
+ if (live_run.id == id) return live_run.request();
+ }
if (pipe.edit_run) |pending| if (pending.id == id) return pending.request;
return null;
}
@@ -248,6 +289,7 @@ pub fn pipeResponse(
failure: ?selection_pipe.Failure,
) void {
if (p.pipe.edit_run) |pending| if (pending.id == id) return edit_cmd.finish(p, success, outputs, failure);
+ if (p.pipe.preview) |*live_run| if (live_run.id == id) return answered(live_run, success, outputs, failure);
// A SUPERSEDED OR UNKNOWN id is the one silence worth keeping: it is
// the answer to a question nobody is still asking.
if (p.pipe.wait == null or p.pipe.wait.?.id != id) return;
@@ -256,6 +298,60 @@ pub fn pipeResponse(
defer wait.deinit();
if (!success or outputs.len != wait.inputs.len) return pipeFailed(p, &wait, failure);
+ apply(p, &wait, outputs, null);
+}
+
+/// A live run answered: each run's stdout, stderr and status from what
+/// `framed` made of it; a run the runner itself failed (no shell, too
+/// much output) says so, as each of them.
+fn answered(live_run: *PendingPipe, success: bool, outputs: []const []const u8, failure: ?selection_pipe.Failure) void {
+ const a = live_run.arena.allocator();
+ const n = live_run.inputs.len;
+ const outs = a.alloc([]const u8, n) catch return;
+ const errs = a.alloc([]const u8, n) catch return;
+ const codes = a.alloc(u8, n) catch return;
+ for (outs, errs, codes, 0..) |*out, *err, *code, i| {
+ out.* = "";
+ err.* = "";
+ code.* = 255;
+ if (!success or outputs.len != n) {
+ if (failure) |f| {
+ code.* = if (f.kind == .exit) f.code else 255;
+ err.* = a.dupe(u8, f.stderr) catch "";
+ }
+ continue;
+ }
+ const o = outputs[i];
+ const last = std.mem.lastIndexOfScalar(u8, o, 0x1e) orelse continue;
+ const mid = std.mem.lastIndexOfScalar(u8, o[0..last], 0x1e) orelse continue;
+ out.* = a.dupe(u8, o[0..mid]) catch return;
+ err.* = a.dupe(u8, o[mid + 1 .. last]) catch return;
+ code.* = std.fmt.parseInt(u8, std.mem.trim(u8, o[last + 1 ..], " \n"), 10) catch 255;
+ }
+ live_run.outs = outs;
+ live_run.errs = errs;
+ live_run.codes = codes;
+}
+
+/// Enter in a pipe prompt whose live run answered for the text as it is:
+/// the outputs of the runs that worked applied from it, the selections of
+/// the ones that failed left as they are. False when it has nothing to
+/// apply from (the caller runs the command).
+pub fn applyLive(p: *Pardes, serial: u32, typed: []const u8) bool {
+ var live_run = p.pipe.preview orelse return false;
+ p.pipe.preview = null;
+ defer live_run.deinit();
+ const pane = p.panes[live_run.pane.index()] orelse return false;
+ if (pane.serial != serial or live_run.codes.len == 0 or !std.mem.eql(u8, live_run.typed, typed)) return false;
+ if (pane.file == null or pane.file.?.revision != live_run.revision) return false;
+ apply(p, &live_run, live_run.outs, live_run.codes);
+ return true;
+}
+
+/// `wait`'s outputs applied to the text it piped: each range's cut with its
+/// output in place, `$`'s kept; with `codes`, a run that failed leaves its
+/// range as it was (`$`: a run's status is its verdict).
+fn apply(p: *Pardes, wait: *const PendingPipe, outputs: []const []const u8, codes: ?[]const u8) void {
const pane = p.panes[wait.pane.index()] orelse return;
if (pane.serial != wait.serial) return;
@@ -282,7 +378,8 @@ pub fn pipeResponse(
var m: usize = 0;
var pri: ?usize = null;
for (0..wait.nranges) |i| {
- if (!std.mem.eql(u8, std.mem.trim(u8, outputs[i], " \n"), "0")) continue;
+ const kept_here = if (codes) |c| c[i] == 0 else std.mem.eql(u8, std.mem.trim(u8, outputs[i], " \n"), "0");
+ if (!kept_here) continue;
if (i == wait.primary) pri = m;
kept[m] = wait.ranges[i];
m += 1;
@@ -300,8 +397,9 @@ pub fn pipeResponse(
// answer from elsewhere past them is said, the text left as it was.
var said: [128]u8 = undefined;
for (0..n) |i| {
- const cut = pipeCut(&wait, content, i) orelse return;
- const output = pipeOutput(&wait, outputs, content, i);
+ const cut = pipeCut(wait, content, i) orelse return;
+ const failed = if (codes) |c| c[if (wait.how.pipes()) i else 0] != 0 else false;
+ const output = if (failed) content[cut.lo..cut.hi] else pipeOutput(wait, outputs, content, i);
if (output.len > selection_pipe.max_stdout_bytes)
return p.setMessage(wait.pane, std.fmt.bufPrint(&said, "pipe: an output of {d} bytes, past {d}: the text is left as it was", .{ output.len, selection_pipe.max_stdout_bytes }) catch unreachable); // two numbers fit 128
total_output += output.len; // each under 1 MiB, at most max_selections of them
@@ -332,12 +430,15 @@ pub fn pipeResponse(
t.show(p.settings.tab);
}
+/// The pipe request the keys made last: a known filter typed in a body's
+/// prompt runs live at each key before Enter runs it (Prompt.zig).
fn nextPipeEffect(p: *Pardes) ?u32 {
+ var last: ?u32 = null;
while (p.nextEffect()) |effect| switch (effect) {
- .pipe => |request| return request.id,
+ .pipe => |asked| last = asked.id,
else => {},
};
- return null;
+ return last;
}
test "selection pipe prompt submits exact request and Escape cancels" {
@@ -369,7 +470,8 @@ test "selection pipe prompt submits exact request and Escape cancels" {
p.update(.{ .key = .{ .cp = Key.escape } });
try std.testing.expect(pane.focus == .body and p.view().tag_state.kind == .rest);
try std.testing.expectEqualSlices(u8, before, pane.file.?.content);
- try std.testing.expect(nextPipeEffect(p) == null);
+ // `cat`, a known filter, ran live as it was typed: Esc drops it.
+ try std.testing.expect(p.pipe.preview == null);
}
test "| in a tag pipes the tag's own text, and undo there brings it back" {