summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/Core.zig2
-rw-r--r--core/ninep/pty.zig81
-rw-r--r--core/panes.zig4
-rw-r--r--core/term/terminal.zig12
4 files changed, 91 insertions, 8 deletions
diff --git a/core/Core.zig b/core/Core.zig
index 2c6567ed..f5ed28bd 100644
--- a/core/Core.zig
+++ b/core/Core.zig
@@ -1070,6 +1070,7 @@ fn acknowledgeShellInner(p: *Pardes, id: Pane.Id, executable: []const u8, prompt
if (id.index() < MAX_PANES) if (p.panes[id.index()]) |pane| {
ctlfs.pty.shellGone(p, pane, true); // a respawn replaced whatever ran
pane.shell_failed = false;
+ pane.greeted = false;
pane.had_shell = true;
pane.fs.unmarked = !prompt_marks;
// The tag's Term word names the shell the host ran, and a click
@@ -2010,6 +2011,7 @@ pub fn sync(p: *Pardes) void {
p.emit(.{ .resize_pty = .{ .pane = .at(id), .cols = pane.cols, .rows = pane.rows } });
p.emitWrite(id, "ls\r");
pane.greet = false;
+ pane.greeted = true;
},
};
}
diff --git a/core/ninep/pty.zig b/core/ninep/pty.zig
index 43973491..2dd7f7f3 100644
--- a/core/ninep/pty.zig
+++ b/core/ninep/pty.zig
@@ -135,11 +135,12 @@ pub const status_len: u64 = 36;
pub fn readStatus(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
// Busy is what a run written now would be told: a command is running
- // or a line is typed at the prompt. Before the shell's first prompt a
- // run waits for it rather than answer busy, so that is not busy.
+ // or a line is typed at the prompt. Before the shell's first prompt, or
+ // the one after its greeting, a run waits for it rather than answer
+ // busy, so that is not busy.
const marks = pardes.Term.prompt(pane.terminal);
const busy = p.hostTtyTaken(id) or (!pane.fs.unmarked and (waitingRun(p, pane) != null or
- (marks.prompts > 0 and (marks.phase != .input or !marks.input_empty))));
+ (marks.prompts > 0 and !greeting(pane, marks) and (marks.phase != .input or !marks.input_empty))));
const out = p.fs.stage(p.gpa);
const size = winsize(pane);
out.print(p.gpa, "{d:>11} {d:>11} {d:>11}\n", .{ size[0], size[1], @intFromBool(busy) }) catch return Reply.fail(req.tag, E.NOMEM);
@@ -230,8 +231,8 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
answer(p, slot, "error {s}", .{if (pane.command_done) "command done; not a shell" else "a command runs here, not a shell"});
} else if (pf.unmarked) {
answer(p, slot, "error no prompt marks", .{});
- } else if (waitingRun(p, pane) != null or (marks.prompts > 0 and (marks.phase != .input or
- !marks.input_empty or p.hostTtyTaken(id))))
+ } else if (waitingRun(p, pane) != null or (marks.prompts > 0 and !greeting(pane, marks) and
+ (marks.phase != .input or !marks.input_empty or p.hostTtyTaken(id))))
{
// Something is running or someone has typed at the prompt: sending
// now would type into the middle of it. The phase is pardes's own
@@ -248,7 +249,8 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
answer(p, slot, "busy", .{});
} else {
// Sent now if the shell is at its prompt, or at its first prompt
- // when it has not drawn one yet (noteMarks).
+ // when it has not drawn one yet, or the one after its greeting
+ // (noteMarks).
slot.line = p.gpa.dupe(u8, line) catch return Reply.fail(req.tag, E.NOMEM);
slot.phase = .waiting;
pf.run = req.handle;
@@ -296,7 +298,7 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
pf.run = null;
return;
}
- if (marks.phase != .input or !marks.input_empty or p.hostTtyTaken(id)) return;
+ if (greeting(pane, marks) or marks.phase != .input or !marks.input_empty or p.hostTtyTaken(id)) return;
slot.want = marks.started +% 1;
slot.prompts = marks.prompts;
slot.declined = marks.declined;
@@ -361,6 +363,15 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
finish(p, slot, pane, done.status);
}
+/// The greeting typed at a new terminal's first prompt (Pane.greeted) is
+/// its shell's first command until the prompt after it: its echo not back
+/// yet, the prompt looks empty, and a line sent then would be typed behind
+/// the `ls`, its start taken for the run's (the run answered with what ls
+/// printed). `ls` always starts, so the wait ends.
+fn greeting(pane: *const Pane, marks: pardes.Term.Prompt) bool {
+ return pane.greeted and (marks.started == 0 or (marks.started == 1 and marks.phase != .input));
+}
+
/// The shell's marks as the session's emulator has them (taped), as a
/// run's step reads them: a screen-state replica's emulator is the last
/// delta's, which comes at a frame and can be a unit behind the one it
@@ -785,6 +796,62 @@ test "a run waiting for a new terminal's first prompt is the only line typed the
try testing.expectEqualStrings("true\r", sent[0..len]);
}
+test "a prompt redrawn under a run's line, the line taken, is the line run: never `error not run`" {
+ const gpa = testing.allocator;
+ const p = try withTerm(gpa);
+ defer p.deinit();
+ const node = Node.of(serialOf(p), .pty_run);
+ const out = struct {
+ fn put(pp: *Pardes, bytes: []const u8) void {
+ pp.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } });
+ while (pp.nextEffect()) |_| {}
+ }
+ };
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ // A window's first terminal: bash's first prompt, then the window's
+ // first size (its rows a row shorter), which readline answers by
+ // drawing its prompt again: with the run's line on it when it had read
+ // it, its Enter not yet. Then the line taken: its newline, and in the
+ // next read PS0's C (bash writes them apart): bytes as pty/data read
+ // them off a ReleaseSafe window session's start.
+ out.put(p, "\x1b]133;D;0;aid=pardes\x07" ++ prompt);
+ const h = call(p, .{ .tag = 1, .op = .open, .node = node }).reply.handle;
+ try testing.expectEqualStrings("echo $$\r", call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "echo $$\n" }).pty());
+ out.put(p, "\r\x1b[K\r" ++ prompt ++ "echo $$");
+ out.put(p, "\r\n\x1b[?2004l\r");
+ try testing.expectEqual(Status.again, call(p, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 }).reply.status);
+ out.put(p, "\x1b]133;C;aid=pardes\x074242\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt);
+ try testing.expectEqualStrings("exit 0\n4242\n", call(p, .{ .tag = 4, .op = .read, .node = node, .handle = h, .size = 64 }).bytes);
+}
+
+test "a run written just after a new terminal's greeting is typed is not answered with the greeting's output" {
+ const gpa = testing.allocator;
+ const p = try withTerm(gpa);
+ defer p.deinit();
+ p.panes[0].?.greet = true; // a Term's, in a window (resized)
+ p.update(.{ .resize = .{ .cols = 80, .rows = 24 } });
+ while (p.nextEffect()) |_| {}
+ const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ // The first prompt: the greeting typed at it, its echo not back yet.
+ p.update(.{ .output = .{ .pane = .at(0), .bytes = prompt } });
+ var greeted: [16]u8 = undefined;
+ var len: usize = 0;
+ while (p.nextEffect()) |e| if (e == .write) {
+ const b = e.write.bytes.slice();
+ @memcpy(greeted[len..][0..b.len], b);
+ len += b.len;
+ };
+ try testing.expectEqualStrings("ls\r", greeted[0..len]);
+ const node = Node.of(serialOf(p), .pty_run);
+ const h = call(p, .{ .tag = 1, .op = .open, .node = node }).reply.handle;
+ _ = call(p, .{ .tag = 2, .op = .write, .node = node, .handle = h, .data = "true\n" });
+ for ([_][]const u8{ "ls\r\n\x1b]133;C;aid=pardes\x07notes.txt\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt, "true\r\n\x1b]133;C;aid=pardes\x07\x1b]133;D;0;aid=pardes\x07" ++ prompt }) |bytes| {
+ p.update(.{ .output = .{ .pane = .at(0), .bytes = bytes } });
+ while (p.nextEffect()) |_| {}
+ }
+ try testing.expectEqualStrings("exit 0\n", call(p, .{ .tag = 3, .op = .read, .node = node, .handle = h, .size = 64 }).bytes);
+}
+
test "run answers how its command ended once the shell is back at a prompt" {
const gpa = testing.allocator;
const p = try withTerm(gpa);
diff --git a/core/panes.zig b/core/panes.zig
index 5bf6992a..4116c3e8 100644
--- a/core/panes.zig
+++ b/core/panes.zig
@@ -160,6 +160,10 @@ pub const Pane = struct {
cols: u16,
rows: u16,
greet: bool = false,
+ /// Its greeting was typed (Pardes.layout): that `ls` is its shell's
+ /// first command, and a run written until the prompt after it waits for
+ /// that prompt (ninep/pty.zig `greeting`), as for the first.
+ greeted: bool = false,
/// Its shell could not be started (Pardes.shellFailed): a run hears
/// `error shell gone` at once.
shell_failed: bool = false,
diff --git a/core/term/terminal.zig b/core/term/terminal.zig
index 45c01841..88eb1443 100644
--- a/core/term/terminal.zig
+++ b/core/term/terminal.zig
@@ -927,9 +927,19 @@ pub fn prompt(state: ?*State) Term.Prompt {
// Nothing further right counts: that is where fish draws a right
// prompt, and fish suggests nothing on an empty line.
const empty = ready and empty: {
+ var prompted = false;
const row = cursor.page_pin.cells(.all);
- for (row[0..@min(cursor.x + 1, row.len)]) |cell|
+ for (row[0..@min(cursor.x + 1, row.len)]) |cell| {
if (cell.semantic_content == .input and !cell.isEmpty()) break :empty false;
+ prompted = prompted or cell.semantic_content == .prompt;
+ }
+ // A row with no prompt on it, begun by a newline in the input: the
+ // line above was taken, its Enter echoed and the command's C still
+ // to come (bash writes them apart), so it is typed still.
+ if (!prompted and cursor.page_row.semantic_prompt == .prompt_continuation) if (cursor.page_pin.up(1)) |above| {
+ for (above.cells(.all)) |cell|
+ if (cell.semantic_content == .input and !cell.isEmpty()) break :empty false;
+ };
break :empty true;
};
return .{