summaryrefslogtreecommitdiff
path: root/core/Pipe.zig
diff options
context:
space:
mode:
Diffstat (limited to 'core/Pipe.zig')
-rw-r--r--core/Pipe.zig154
1 files changed, 131 insertions, 23 deletions
diff --git a/core/Pipe.zig b/core/Pipe.zig
index 000d1f74..1dc69d49 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();
@@ -88,14 +100,10 @@ pub const PendingPipe = struct {
}
};
+/// The notice band's pipe prompt answered (a tag's text, a header's).
pub fn submitPipe(p: *Pardes, id: usize) void {
const pane = p.panes[id] orelse return;
const t = panes.promptTarget(p, pane) orelse return;
- if (t.what == .body) {
- const f = pane.file orelse return;
- if (!panes.Output.fileTraits(f.output).saves) return;
- }
- const content = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(.at(id), "pipe", err);
const how = switch (pane.prompt) {
.pipe => |pipe| pipe,
else => return,
@@ -103,7 +111,39 @@ pub fn submitPipe(p: *Pardes, id: usize) void {
const armed = pane.promptText() orelse return;
const marker = pipeMarker(how);
if (!std.mem.startsWith(u8, armed, marker)) return;
- const command = armed[marker.len..];
+ submit(p, id, t, how, armed[marker.len..]);
+}
+
+/// 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;
+ if (!panes.Output.fileTraits(f.output).saves) return;
+ }
+ const content = edit.flatSurface(p.editor(), t) catch |err| return p.reportError(.at(id), "pipe", err);
if (command.len == 0) return;
var ranges: [panes.Text.max_selections]modal.Selection = undefined;
@@ -123,15 +163,20 @@ pub fn submitPipe(p: *Pardes, id: usize) void {
// `$` 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,
@@ -158,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;
}
@@ -241,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;
@@ -249,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;
@@ -275,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;
@@ -293,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
@@ -325,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" {
@@ -344,12 +452,11 @@ test "selection pipe prompt submits exact request and Escape cancels" {
pane.body.vsel = .{ .active = true, .row = 0, .col = 0, .explicit = true };
p.update(.{ .key = .{ .cp = '|' } });
- try std.testing.expect(pane.focus == .body and pane.prompt == .pipe);
- try std.testing.expectEqualStrings(config.pipe_marker, pane.promptText().?);
+ try std.testing.expect(pane.focus == .body and p.view().tag_state.kind == .prompt);
try std.testing.expect(nextPipeEffect(p) == null);
p.update(.{ .key = .{ .cp = 't', .text = "tr a-z A-Z" } });
p.update(.{ .key = .{ .cp = Key.enter } });
- try std.testing.expect(pane.focus == .body and pane.prompt != .pipe and pane.input.own == null);
+ try std.testing.expect(pane.focus == .body and p.view().tag_state.kind == .rest);
const id = nextPipeEffect(p) orelse return error.MissingPipeEffect;
const request = p.pipe.pipeRequest(id) orelse return error.MissingPipeRequest;
try std.testing.expectEqualStrings("tr a-z A-Z", request.command);
@@ -361,9 +468,10 @@ test "selection pipe prompt submits exact request and Escape cancels" {
p.update(.{ .key = .{ .cp = '|' } });
p.update(.{ .key = .{ .cp = 'c', .text = "cat" } });
p.update(.{ .key = .{ .cp = Key.escape } });
- try std.testing.expect(pane.focus == .body and pane.prompt != .pipe);
+ 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" {
@@ -404,7 +512,7 @@ test "$ keeps the selections the command exits 0 on, and a real shell gives that
};
pane.body.setRanges(config.tab_width, pane.file.?.content, &ranges, &.{}, 1, true);
p.update(.{ .key = .{ .cp = '$' } });
- try std.testing.expectEqualStrings(config.pipe_marker_keep, pane.promptText().?);
+ try std.testing.expectEqual(pardes.Prompt.Op.keep_pipe, p.view().tag_state.prompt.op);
p.update(.{ .key = .{ .cp = 'g', .text = "grep -v b" } });
p.update(.{ .key = .{ .cp = Key.enter } });
const id = nextPipeEffect(p) orelse return error.MissingPipeEffect;
@@ -663,10 +771,10 @@ test "selection pipe binding is file-normal-only" {
pane.file.?.output = .{ .from = .search };
p.update(.{ .key = .{ .cp = '|' } });
- try std.testing.expect(pane.prompt != .pipe);
+ try std.testing.expect(p.view().tag_state.kind != .prompt);
pane.file.?.output = null;
pane.body.mode = .insert;
p.update(.{ .key = .{ .cp = '|', .text = "|" } });
- try std.testing.expect(pane.prompt != .pipe);
+ try std.testing.expect(p.view().tag_state.kind != .prompt);
try std.testing.expectEqualSlices(u8, "|x", pane.file.?.content);
}