summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/Core.zig21
-rw-r--r--src/Term.zig45
-rw-r--r--src/builtins.zig7
-rw-r--r--src/exec.zig19
-rw-r--r--src/ninep/pty.zig26
-rw-r--r--src/panes.zig76
-rw-r--r--src/tape.zig2
-rw-r--r--src/term/echo.zig2
-rw-r--r--src/term/terminal.zig208
9 files changed, 218 insertions, 188 deletions
diff --git a/src/Core.zig b/src/Core.zig
index 700ab7ab..55848aa9 100644
--- a/src/Core.zig
+++ b/src/Core.zig
@@ -993,7 +993,16 @@ pub fn acknowledgeShell(p: *Pardes, id: usize, executable: []const u8, prompt_ma
} else |_| {}
}
};
- if (id < MAX_PANES) pardes.panes.terminal.shellSpawned(p, id, prompt_marks);
+ // The lines queued for it go when it can take them: at its first
+ // prompt, or now when it draws no marks to wait for.
+ if (id < MAX_PANES) if (p.panes[id]) |pane| if (pane.isTerminal() and pane.pending_command.wait == .spawn) {
+ if (prompt_marks) {
+ pane.pending_command.wait = .input;
+ } else {
+ pane.greet = false;
+ pane.releasePendingCommand(p, id, true);
+ }
+ };
if (p.settings.shell.effective.set(executable))
p.settings.shell.pending = false;
}
@@ -1874,15 +1883,19 @@ pub fn sync(p: *Pardes) void {
// A directory pane's entries follow its width, as acme's
// columns do (Dir.fit).
if (pane.file) |file| if (file.listing != null and r.h > pane.tag_rows) pardes.panes.Dir.fit(p, pane, pane.cols);
- pardes.panes.terminal.releasePendingCommandIfReady(p, id, pane);
+ if (pane.pending_command.wait == .input) pane.releasePendingCommand(p, id, tape.value(.prompt, struct {
+ fn ready(at: *const Pane) bool {
+ return pardes.Term.prompt(at.terminal).input_ready;
+ }
+ }.ready, .{pane}));
// Which bands a pane shows is the step's, as its geometry is:
// what is drawn under them, and where a click lands, read it.
Messages.collectNotices(p, pane, r, id);
// The greeting `ls` of a new terminal in a directory is typed at
// its shell's FIRST prompt, or never: typed later it would land
// in whatever runs then, or in the middle of a line someone
- // typed (panes.terminal.greeting).
- if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (tape.value(.prompt, pardes.panes.terminal.greeting, .{pane})) {
+ // typed (Pane.greeting).
+ if (pane.greet and pane.isTerminal() and p.resize_count > 0) switch (tape.value(.prompt, Pane.greeting, .{pane})) {
.wait => {},
.never => pane.greet = false,
.now => {
diff --git a/src/Term.zig b/src/Term.zig
index 504aef7a..ba0a2307 100644
--- a/src/Term.zig
+++ b/src/Term.zig
@@ -121,3 +121,48 @@ pub const Modes = struct {
/// It has the alternate screen (1049).
alternate: bool = false,
};
+
+// ---- prompt and commands ----
+
+/// Where its shell is in its command cycle (`Prompt`).
+pub const prompt = terminal.prompt;
+/// What the newest finished command printed (`CommandOutput`), read from
+/// at most so many rows above its end.
+pub const commandOutput = terminal.commandOutput;
+/// The shell exited in the middle of a command: its output ends at the
+/// cursor.
+pub const endOutputHere = terminal.endOutputHere;
+
+/// Where a shell is in its command cycle, as pardes's own OSC 133 marks
+/// tell it (marks carrying its `aid` alone: a nested shell's pass it by),
+/// and what is at its cursor. All zero without a terminal.
+pub const Prompt = struct {
+ phase: enum { none, prompt, input, running } = .none,
+ /// Commands started (C at the input phase), prompts drawn (B), and D
+ /// marks outside a running command (a line the shell handled without
+ /// starting it).
+ started: u32 = 0,
+ prompts: u32 = 0,
+ declined: u32 = 0,
+ /// The newest prompt continues an unfinished line (bash's PS2).
+ continuation: bool = false,
+ /// The newest command to end: which one, and its status if given.
+ finished: ?struct { seq: u32, status: ?i32 } = null,
+ /// The cursor is at a prompt's input, on either screen.
+ input_ready: bool = false,
+ /// ...and nothing is typed on its line up to the cursor.
+ input_empty: bool = false,
+ /// The cursor is on a prompt's row or at its input, on the primary
+ /// screen.
+ at_prompt: bool = false,
+};
+
+/// What a command printed. `text` is the caller's.
+pub const CommandOutput = struct {
+ text: [:0]const u8,
+ /// Whether `text` starts where the command did. It does not when that
+ /// start is not there to read: cut off by the rows asked for, scrolled
+ /// out of the history or erased from it (ED 3), erased under it while
+ /// it ran, or never marked (a C or D on the alternate screen).
+ whole: bool,
+};
diff --git a/src/builtins.zig b/src/builtins.zig
index 85bb6e44..5a1c3061 100644
--- a/src/builtins.zig
+++ b/src/builtins.zig
@@ -428,7 +428,12 @@ pub const Kill = struct {
for (c.p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
const command: ?[]const u8 = if (pane.command) |line| (if (pane.command_done) null else line) else null;
- if (command == null and !panes.terminal.commandRunning(pane)) continue;
+ // The command pardes last typed here, running now.
+ const typed_runs = if (pane.sent_command) |sent| typed: {
+ const at = panes.Term.prompt(pane.terminal);
+ break :typed at.phase == .running and at.started == sent.seq;
+ } else false;
+ if (command == null and !typed_runs) continue;
running = true;
const first = if (command) |line| first: {
const said = std.mem.trim(u8, line, " \t");
diff --git a/src/exec.zig b/src/exec.zig
index d7fb19d8..23c624bd 100644
--- a/src/exec.zig
+++ b/src/exec.zig
@@ -669,7 +669,8 @@ pub fn takesCommandLine(p: *const Pardes, id: usize) bool {
// idle, EMPTY prompt is typed at; otherwise a new pane runs the line.
// (Text in pardes's own edit buffer is not the shell's yet: an exec of
// it, or of anything, types only what it runs.)
- if (panes.terminal.promptInputReady(pane) and !panes.terminal.promptInputEmpty(pane)) return false;
+ const at = panes.Term.prompt(pane.terminal);
+ if (at.input_ready and !at.input_empty) return false;
return !p.hostTtyTaken(id);
}
@@ -710,9 +711,19 @@ pub fn executeFrom(p: *Pardes, id: usize, txt: []const u8, from_body: bool) ?usi
// command pane in the session's directory, never typed into whichever
// terminal has the keyboard.
if (p.exec_header or !takesCommandLine(p, id)) return runCommand(p, id, cmd);
- panes.terminal.padOutputBelowEdits(p, id);
- panes.terminal.noteCommand(pane, cmd);
- if (panes.terminal.queuePendingCommand(pane, cmd) catch |err| {
+ // At a prompt with typed text below it: the output area padded with
+ // newlines so the command's output does not overwrite the buffer.
+ if (pane.ovl) |o| if (pane.isTerminal()) if (pane.terminal) |state| if (panes.Term.prompt(state).at_prompt) {
+ // the buffer's LAST surface row: its lines may outnumber the shell
+ // rows it covers, and it is the bottom one output must clear
+ const max_row = o.row + @as(i32, @intCast(modal.lineCount(o.text))) - 1;
+ const grid = panes.Term.grid(state);
+ const cursor_abs = pane.surfRow(@as(i32, @intCast(grid.offset)) + @as(i32, grid.cursor_y));
+ const pad = std.math.clamp(max_row - cursor_abs, 0, @as(i32, pane.rows));
+ for (0..@intCast(pad)) |_| p.emitWrite(id, "\r");
+ };
+ pane.noteCommand(cmd);
+ if (pane.queuePendingCommand(cmd) catch |err| {
p.reportError(id, "queue command", err);
return id;
}) return id;
diff --git a/src/ninep/pty.zig b/src/ninep/pty.zig
index 8e71a2c9..bd090dfc 100644
--- a/src/ninep/pty.zig
+++ b/src/ninep/pty.zig
@@ -137,9 +137,9 @@ 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.
- const marks = if (pane.terminal) |state| &state.stream.handler else null;
+ const marks = pardes.Term.prompt(pane.terminal);
const busy = p.hostTtyTaken(id) or (!pane.fs.unmarked and (waitingRun(p, pane) != null or
- (if (marks) |m| m.prompts > 0 and (m.phase != .input or !pardes.panes.terminal.promptInputEmpty(pane)) else false)));
+ (marks.prompts > 0 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);
@@ -219,8 +219,8 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
if (std.mem.trim(u8, line, " \t").len == 0) return tree.failText(req.tag, E.INVAL, e_bad_line);
for (line) |c| if (c < ' ' and c != '\t') return tree.failText(req.tag, E.INVAL, e_bad_line);
const pf = &pane.fs;
- const state = pane.terminal orelse return tree.failText(req.tag, E.INVAL, e_bad_line);
- const marks = &state.stream.handler;
+ if (pane.terminal == null) return tree.failText(req.tag, E.INVAL, e_bad_line);
+ const marks = pardes.Term.prompt(pane.terminal);
if (pane.shell_failed) {
// Its shell never started: no prompt is coming to wait for.
answer(p, slot, "error shell gone", .{});
@@ -230,7 +230,7 @@ pub fn writeRun(p: *Pardes, req: Req, id: usize, pane: *Pane) Reply {
} 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
- !pardes.panes.terminal.promptInputEmpty(pane) or p.hostTtyTaken(id))))
+ !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
@@ -285,17 +285,17 @@ pub fn readRun(p: *Pardes, req: Req) Reply {
/// for the prompt means whoever reads the answer can send the next command
/// at once instead of finding the shell still drawing it.
pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
- const state = pane.terminal orelse return;
+ if (pane.terminal == null) return;
const pf = &pane.fs;
const slot = waitingRun(p, pane) orelse return;
- const marks = &state.stream.handler;
+ const marks = pardes.Term.prompt(pane.terminal);
if (slot.phase == .waiting) {
if (pf.unmarked) {
answer(p, slot, "error no prompt marks", .{});
pf.run = null;
return;
}
- if (marks.phase != .input or !pardes.panes.terminal.promptInputEmpty(pane) or p.hostTtyTaken(id)) return;
+ if (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;
@@ -305,7 +305,7 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
if (pardes.Term.modes(pane.terminal).alternate) pane.feedOutput(p, "\x1b[?1049l");
p.emitWrite(id, slot.line);
p.emitWrite(id, "\r");
- pardes.panes.terminal.noteCommand(pane, slot.line);
+ pane.noteCommand(slot.line);
// The run is the shell's first command: a greeting `ls` typed at the
// same prompt would run after it, and its C would be taken for ours.
pane.greet = false;
@@ -322,7 +322,7 @@ pub fn noteMarks(p: *Pardes, id: usize, pane: *Pane) void {
// typed into an empty prompt, so throw it away with Ctrl-C, which
// drops the whole pending command, and answer at the prompt after
// that, so the next line finds the shell clear.
- const empty = pardes.panes.terminal.promptInputEmpty(pane);
+ const empty = marks.input_empty;
switch (slot.cleared) {
.no => {
if (marks.continuation) {
@@ -398,9 +398,9 @@ fn finish(p: *Pardes, slot: *Run, pane: *Pane, status_code: ?i32) void {
pub const Printed = struct { text: []const u8, whole: bool };
-/// What pane `pane`'s command printed (terminal.commandOutput), in `arena`.
+/// What pane `pane`'s command printed (Term.commandOutput), in `arena`.
fn printedBy(pane: *Pane, arena: std.mem.Allocator) ?Printed {
- const out = pardes.panes.terminal.commandOutput(pane, arena, output_rows) catch return null;
+ const out = pardes.Term.commandOutput(pane.terminal, arena, output_rows) catch return null;
return .{ .text = out.text, .whole = out.whole };
}
@@ -413,7 +413,7 @@ pub fn shellExited(p: *Pardes, pane: *Pane, status: ?u8) void {
const code = status orelse return;
if (waitingRun(p, pane)) |slot| if (slot.phase == .sent) {
pane.fs.run = null;
- pardes.panes.terminal.endOutputHere(pane);
+ if (pane.terminal) |state| pardes.Term.endOutputHere(state);
finish(p, slot, pane, code);
};
var buf: [4]u8 = undefined;
diff --git a/src/panes.zig b/src/panes.zig
index ed2f3767..4f9ac6fc 100644
--- a/src/panes.zig
+++ b/src/panes.zig
@@ -146,7 +146,12 @@ pub const Pane = struct {
/// A shell has run in it (Pardes.acknowledgeShell): a Term whose first
/// shell fails to start closes, one started again keeps its pane.
had_shell: bool = false,
- pending_command: terminal.PendingCommand = .{},
+ /// What pardes typed for a shell not ready for it yet: queued until it
+ /// is started (`spawn`) and then at its prompt (`input`).
+ pending_command: struct {
+ bytes: []u8 = &.{},
+ wait: enum { none, spawn, input } = .none,
+ } = .{},
/// The last command line pardes typed into this terminal (an exec, a
/// middle click, a pty/run): its first word, which Kill matches, and
/// the shell's command count its start (C mark) reaches.
@@ -971,6 +976,69 @@ pub const Pane = struct {
return @intCast(@min(pane.rows - shown, (at + 1) -| shown));
}
+ /// Whether a new terminal's greeting may be typed now: only at the
+ /// shell's first prompt (pardes's own marks: no command started; a
+ /// prompt drawn again for a resize is the same first prompt), with
+ /// nothing typed at it and no program on the alternate screen. Any
+ /// later moment is `.never`, for good: something has run or been typed
+ /// first, and an `ls` then would go into it. A shell pardes cannot mark
+ /// has no first prompt to find, and is never greeted.
+ pub fn greeting(pane: *const Pane) enum { wait, now, never } {
+ if (pane.fs.unmarked) return .never;
+ // Text typed into the edit buffer that stands in for the prompt row.
+ if (pane.ovl) |o| if (o.text.len > 0) return .never;
+ const state = pane.terminal orelse return .wait;
+ const at = Term.prompt(state);
+ // (Not `declined`: bash's first prompt comes after a D of its own.)
+ if (at.started > 0) return .never;
+ // At a prompt's input (the emulator's semantic marks, pardes's or
+ // the shell's own), before any command pardes's marks saw run.
+ if (!at.input_ready) return .wait;
+ return if (at.input_empty) .now else .never;
+ }
+
+ /// Remembers a line pardes types into this terminal as the command it
+ /// started, for Kill: it runs from the shell's next C mark to its D.
+ /// ponytail: of several lines queued for a shell not up yet, only the
+ /// last is remembered, as the first of them.
+ pub fn noteCommand(pane: *Pane, line: []const u8) void {
+ const state = pane.terminal orelse return;
+ const said = std.mem.trim(u8, line, " \t");
+ const word = said[0 .. std.mem.indexOfAny(u8, said, " \t") orelse said.len];
+ var sent: @typeInfo(@FieldType(Pane, "sent_command")).optional.child = .{ .seq = Term.prompt(state).started +% 1 };
+ sent.len = @intCast(@min(word.len, sent.word.len));
+ @memcpy(sent.word[0..sent.len], word[0..sent.len]);
+ pane.sent_command = sent;
+ }
+
+ /// A line for a shell not ready for it (`pending_command`), queued
+ /// after the ones before it; false when the shell is ready now.
+ pub fn queuePendingCommand(pane: *Pane, command: []const u8) !bool {
+ if (pane.pending_command.wait == .none) return false;
+ const old_len = pane.pending_command.bytes.len;
+ const new_len = try std.math.add(usize, old_len, try std.math.add(usize, command.len, 1));
+ const bytes = if (old_len == 0)
+ try pane.gpa.alloc(u8, new_len)
+ else
+ try pane.gpa.realloc(pane.pending_command.bytes, new_len);
+ @memcpy(bytes[old_len..][0..command.len], command);
+ bytes[new_len - 1] = '\r';
+ pane.pending_command.bytes = bytes;
+ pane.greet = false;
+ return true;
+ }
+
+ /// The lines queued for its shell, written now that it is `ready`.
+ pub fn releasePendingCommand(pane: *Pane, p: *Pardes, id: usize, ready: bool) void {
+ if (!ready) return;
+ const bytes = pane.pending_command.bytes;
+ pane.pending_command = .{};
+ if (bytes.len > 0) {
+ p.emitWrite(id, bytes);
+ pane.gpa.free(bytes);
+ }
+ }
+
/// `run <serial> <word>` or `exit <serial> <N|?>` in the log.
/// The whole line, one record as `msg` carries its text: its control
/// characters, a newline too, become spaces (events.pushLog). A new pane is
@@ -1130,7 +1198,8 @@ pub fn teardownPane(p: *Pardes, pane: *Pane) void {
}
p.shell_rows.dropPane(pane);
for (0..pane.pointer_selections.len) |slot| pane.clearPointerSelection(slot);
- panes.terminal.deinitPendingCommand(pane);
+ if (pane.pending_command.bytes.len > 0) pane.gpa.free(pane.pending_command.bytes);
+ pane.pending_command = .{};
if (pane.command) |line| p.gpa.free(line);
if (pane.shell) |bin| p.gpa.free(bin);
@import("Kind.zig").close(p, pane);
@@ -1194,7 +1263,8 @@ pub fn newShell(p: *Pardes, id: usize, cwd: []const u8) !*Pane {
// shell saying it is there is no rename (`new N /` then `rename`);
// out of memory for it, the shell names it when it says.
if (cwd.len > 0) pane.setOwnedCwd(cwd) catch {};
- panes.terminal.armShellSpawn(pane);
+ std.debug.assert(pane.pending_command.bytes.len == 0);
+ pane.pending_command.wait = .spawn;
p.installPane(id, pane);
p.emitSpawn(id, pane.serial, cwd);
return pane;
diff --git a/src/tape.zig b/src/tape.zig
index 998643f2..9c3730f0 100644
--- a/src/tape.zig
+++ b/src/tape.zig
@@ -137,7 +137,7 @@ pub const results = .{
pardes.filesystem.OsReadWrite, anyerror!pardes.filesystem.OsStat,
anyerror![]const u8, anyerror![]u8,
anyerror!void, pardes.Term.Told,
- ?pardes.ctlfs.pty.Printed, @typeInfo(@TypeOf(terminal.greeting)).@"fn".return_type.?,
+ ?pardes.ctlfs.pty.Printed, @typeInfo(@TypeOf(pardes.panes.Pane.greeting)).@"fn".return_type.?,
[]pardes.Stream.Batch,
// What only the authority holds (`authority`): a closed pane's record,
// Recent's entry for a closed jump, the closed panes' names.
diff --git a/src/term/echo.zig b/src/term/echo.zig
index d069fe36..9ea13111 100644
--- a/src/term/echo.zig
+++ b/src/term/echo.zig
@@ -533,7 +533,7 @@ fn blank(cp: u21) bool {
/// pardes's marks is trusted only once its echo is confirmed.
fn atPrompt(pane: *const Pane) bool {
const state = pane.terminal orelse return false;
- return state.vt.screens.active_key == .primary and state.stream.handler.phase == .input and terminal.promptInputReady(pane);
+ return state.vt.screens.active_key == .primary and state.stream.handler.phase == .input and terminal.prompt(state).input_ready;
}
/// The terminal view `view`'s keys go to, in tty mode: none when they go
diff --git a/src/term/terminal.zig b/src/term/terminal.zig
index 02ae4b3c..2fe84d0e 100644
--- a/src/term/terminal.zig
+++ b/src/term/terminal.zig
@@ -419,85 +419,6 @@ fn asGhostRgb(rgb: [3]u8) GColor.RGB {
return .{ .r = rgb[0], .g = rgb[1], .b = rgb[2] };
}
-pub const PendingCommand = struct {
- bytes: []u8 = &.{},
- wait: enum { none, spawn, input } = .none,
-};
-
-pub fn armShellSpawn(pane: *Pane) void {
- std.debug.assert(pane.pending_command.bytes.len == 0);
- pane.pending_command.wait = .spawn;
-}
-
-/// Remembers a line pardes types into this terminal as the command it
-/// started, for Kill: it runs from the shell's next C mark to its D.
-/// ponytail: of several lines queued for a shell not up yet, only the last
-/// is remembered, as the first of them.
-pub fn noteCommand(pane: *Pane, line: []const u8) void {
- const state = pane.terminal orelse return;
- const word = std.mem.trim(u8, line, " \t")[0 .. std.mem.indexOfAny(u8, std.mem.trim(u8, line, " \t"), " \t") orelse std.mem.trim(u8, line, " \t").len];
- var sent: @typeInfo(@FieldType(Pane, "sent_command")).optional.child = .{ .seq = state.stream.handler.started +% 1 };
- sent.len = @intCast(@min(word.len, sent.word.len));
- @memcpy(sent.word[0..sent.len], word[0..sent.len]);
- pane.sent_command = sent;
-}
-
-/// Whether the command pardes last typed here is running now.
-pub fn commandRunning(pane: *const Pane) bool {
- const state = pane.terminal orelse return false;
- const sent = pane.sent_command orelse return false;
- const marks = &state.stream.handler;
- return marks.phase == .running and marks.started == sent.seq;
-}
-
-pub fn queuePendingCommand(pane: *Pane, command: []const u8) !bool {
- if (pane.pending_command.wait == .none) return false;
- const old_len = pane.pending_command.bytes.len;
- const new_len = try std.math.add(usize, old_len, try std.math.add(usize, command.len, 1));
- const bytes = if (old_len == 0)
- try pane.gpa.alloc(u8, new_len)
- else
- try pane.gpa.realloc(pane.pending_command.bytes, new_len);
- @memcpy(bytes[old_len..][0..command.len], command);
- bytes[new_len - 1] = '\r';
- pane.pending_command.bytes = bytes;
- pane.greet = false;
- return true;
-}
-
-pub fn shellSpawned(p: *Pardes, id: usize, prompt_marks: bool) void {
- const pane = p.panes[id] orelse return;
- if (!pane.isTerminal() or pane.pending_command.wait != .spawn) return;
- if (prompt_marks) {
- pane.pending_command.wait = .input;
- releasePendingCommand(p, id, pane, false);
- } else {
- pane.greet = false;
- releasePendingCommand(p, id, pane, true);
- }
-}
-
-pub fn releasePendingCommandIfReady(p: *Pardes, id: usize, pane: *Pane) void {
- if (pane.pending_command.wait == .input)
- releasePendingCommand(p, id, pane, pardes.tape.value(.prompt, promptInputReady, .{pane}));
-}
-
-fn releasePendingCommand(p: *Pardes, id: usize, pane: *Pane, ready: bool) void {
- if (!ready) return;
- const bytes = pane.pending_command.bytes;
- pane.pending_command = .{};
- if (bytes.len > 0) {
- p.emitWrite(id, bytes);
- pane.gpa.free(bytes);
- }
-}
-
-pub fn deinitPendingCommand(pane: *Pane) void {
- if (pane.pending_command.bytes.len > 0)
- pane.gpa.free(pane.pending_command.bytes);
- pane.pending_command = .{};
-}
-
/// Where a kitty client puts its temporary files: `$TMPDIR`, else /tmp
/// (which ghostty accepts whatever this says). The environment block lives
/// as long as the process and pardes never edits it (ChildEnv.exportVar).
@@ -898,66 +819,64 @@ pub fn feed(state: *State, bytes: []const u8, track: []const i32, rows: []i32, e
return .{ .rows = rows[0..track.len], .erased = erased[0..track.len], .cleared = cleared, .alive = alive != null, .failed = failed };
}
-/// Whether a new terminal's greeting may be typed now: only at the shell's
-/// first prompt (pardes's own marks: no command started; a prompt drawn
-/// again for a resize is the same first prompt), with nothing
-/// typed at it and no program on the alternate screen. Any later moment is `.never`, for good: something has run or
-/// been typed first, and an `ls` then would go into it. A shell pardes
-/// cannot mark has no first prompt to find, and is never greeted.
-pub fn greeting(pane: *const Pane) enum { wait, now, never } {
- if (pane.fs.unmarked) return .never;
- // Text typed into the edit buffer that stands in for the prompt row.
- if (pane.ovl) |o| if (o.text.len > 0) return .never;
- const state = pane.terminal orelse return .wait;
- const marks = &state.stream.handler;
- // (Not `declined`: bash's first prompt comes after a D of its own.)
- if (marks.started > 0) return .never;
- // At a prompt's input (ghostty's semantic marks, pardes's or the
- // shell's own), before any command pardes's marks saw run.
- if (!promptInputReady(pane)) return .wait;
- return if (promptInputEmpty(pane)) .now else .never;
-}
-
-/// At a prompt's input: on the alternate screen too, where a program killed
-/// there (htop, `sig KILL`) left the emulator and the shell then drew its
-/// prompt, marks and all. A full-screen program draws no prompt marks.
-pub fn promptInputReady(pane: *const Pane) bool {
- const state = pane.terminal orelse return false;
- return state.vt.screens.active.cursor.semantic_content == .input;
+/// Where the shell is in its command cycle (`Term.Prompt`); all zero
+/// without an emulator.
+pub fn prompt(state: ?*State) Term.Prompt {
+ const s = state orelse return .{};
+ const marks = &s.stream.handler;
+ const cursor = &s.vt.screens.active.cursor;
+ // At a prompt's input: on the alternate screen too, where a program
+ // killed there (htop, `sig KILL`) left the emulator and the shell then
+ // drew its prompt, marks and all. A full-screen program draws no
+ // prompt marks.
+ const ready = cursor.semantic_content == .input;
+ // Nothing typed on its line: no input left of the cursor and none
+ // under it (text typed and then Ctrl-A'd starts at the cursor).
+ // 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: {
+ const row = cursor.page_pin.cells(.all);
+ for (row[0..@min(cursor.x + 1, row.len)]) |cell|
+ if (cell.semantic_content == .input and !cell.isEmpty()) break :empty false;
+ break :empty true;
+ };
+ return .{
+ .phase = switch (marks.phase) {
+ .none => .none,
+ .prompt => .prompt,
+ .input => .input,
+ .running => .running,
+ },
+ .started = marks.started,
+ .prompts = marks.prompts,
+ .declined = marks.declined,
+ .continuation = marks.continuation,
+ .finished = if (marks.finished) |f| .{ .seq = f.seq, .status = f.status } else null,
+ .input_ready = ready,
+ .input_empty = empty,
+ .at_prompt = s.vt.cursorIsAtPrompt(),
+ };
}
-pub const CommandOutput = struct {
- /// Owned by the caller.
- text: [:0]const u8,
- /// Whether `text` starts where the command did. It does not when that
- /// start is not there to read: cut off by `max_rows`, scrolled out of
- /// the history or erased from it by ED 3 (ghostty flags the pin
- /// `garbage`; an erased end leaves no text at all), erased under it
- /// while it ran (`MarkedHandler.lost`), or never pinned (a C or D that
- /// came on the alternate screen).
- whole: bool,
-};
-
/// What the newest finished command printed: the screen text between its
/// C and D marks, read from at most `max_rows` rows above its end, so a
/// command that printed the whole history costs no more than its tail.
/// The shell exited in the middle of a command (its `exit 3`): no end mark
/// will come, so the command's output ends where the cursor is.
-pub fn endOutputHere(pane: *Pane) void {
- const state = pane.terminal orelse return;
+pub fn endOutputHere(state: *State) void {
const marks = &state.stream.handler;
if (marks.phase == .running and marks.out_end == null) marks.pinCursor(&marks.out_end, true);
}
-pub fn commandOutput(pane: *Pane, gpa: std.mem.Allocator, max_rows: usize) !CommandOutput {
+pub fn commandOutput(state: ?*State, gpa: std.mem.Allocator, max_rows: usize) !Term.CommandOutput {
// Unless there is text to read, whether the nothing it printed is the
// whole of it.
const whole = printed: {
- const state = pane.terminal orelse break :printed true;
- const marks = &state.stream.handler;
+ const s = state orelse break :printed true;
+ const marks = &s.stream.handler;
const start = marks.out_start orelse break :printed false;
const end = marks.out_end orelse break :printed false;
- const screen = state.vt.screens.get(.primary) orelse break :printed false;
+ const screen = s.vt.screens.get(.primary) orelse break :printed false;
if (end.garbage) break :printed false;
var whole = !marks.lost and !start.garbage;
// Nothing printed: the whole of it.
@@ -982,19 +901,6 @@ pub fn commandOutput(pane: *Pane, gpa: std.mem.Allocator, max_rows: usize) !Comm
return .{ .text = try gpa.dupeSentinel(u8, "", 0), .whole = whole };
}
-/// At a prompt with nothing typed on its line: no input left of the
-/// cursor and none under it (text typed and then Ctrl-A'd starts at the
-/// cursor). Nothing further right counts: that is where fish draws a
-/// right prompt, and fish suggests nothing on an empty line.
-pub fn promptInputEmpty(pane: *const Pane) bool {
- if (!promptInputReady(pane)) return false;
- const cursor = &pane.terminal.?.vt.screens.active.cursor;
- const row = cursor.page_pin.cells(.all);
- for (row[0..@min(cursor.x + 1, row.len)]) |cell|
- if (cell.semantic_content == .input and !cell.isEmpty()) return false;
- return true;
-}
-
/// One key for its program, as the program's keyboard protocol and modes
/// ask for it, written to `out`.
pub fn key(state: *State, k: Key, out: *std.Io.Writer) !void {
@@ -2008,26 +1914,6 @@ fn ghostColor(p: *Pardes, color: ghostty_vt.Style.Color, is_bg: bool) pardes.Col
};
}
-/// executing at a prompt with typed text below it: pad the output area
-/// with newlines so the command's output doesn't overwrite the buffer
-pub fn padOutputBelowEdits(p: *Pardes, id: usize) void {
- // Nothing to pad away from: with no emulator there is no prompt and no
- // child whose output could land on top of the edit buffer.
- const pane = p.panes[id] orelse return;
- const o = pane.ovl orelse return;
- if (!pane.isTerminal()) return;
- const state = pane.terminal orelse return;
- if (!state.vt.cursorIsAtPrompt()) return;
- // the buffer's LAST surface row: its lines may outnumber the shell
- // rows it covers, and it is the bottom one output must clear
- const max_row = o.row + @as(i32, @intCast(modal.lineCount(o.text))) - 1;
- const goff: i32 = @intCast(state.vt.screens.active.pages.scrollbar().offset);
- const cursor_abs = pane.surfRow(goff + @as(i32, @intCast(state.vt.screens.active.cursor.y)));
- const pad = std.math.clamp(max_row - cursor_abs, 0, @as(i32, pane.rows));
- var i: i32 = 0;
- while (i < pad) : (i += 1) p.emitWrite(id, "\r");
-}
-
pub fn pushUndo(p: *Pardes, pane: *Pane) error{OutOfMemory}!void {
return pane.body.remember(p.gpa, pane.ovl);
}
@@ -2202,7 +2088,7 @@ test "document construction allocates only the pane and has inert terminal defau
try std.testing.expectEqual(@as(usize, @sizeOf(Pane)), failing.allocated_bytes);
try std.testing.expectEqual(Term.Grid{}, grid(pane.terminal));
try std.testing.expectEqual(Term.Modes{}, modes(pane.terminal));
- try std.testing.expect(!promptInputReady(pane));
+ try std.testing.expectEqual(Term.Prompt{}, prompt(pane.terminal));
pane.scrollBy(100);
pane.followOutput();
pane.resizeGrid(80, 24);
@@ -2865,9 +2751,9 @@ test "a finished command's output erased by a later ED 3 is not called whole, an
const p = try Pardes.init(gpa, .{ .tty_only = true, .cols = 40, .rows = 6 });
defer p.deinit();
const pane = p.panes[0].?;
- const prompt = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
- p.update(.{ .output = .{ .pane = 0, .bytes = prompt ++ "\r\n\x1b]133;C;aid=pardes\x07printed\r\n\x1b]133;D;0;aid=pardes\x07" ++ prompt } });
- const before = try commandOutput(pane, gpa, 100);
+ const marked = "\x1b]133;A;cl=line;aid=pardes\x07$ \x1b]133;B;aid=pardes\x07";
+ p.update(.{ .output = .{ .pane = 0, .bytes = marked ++ "\r\n\x1b]133;C;aid=pardes\x07printed\r\n\x1b]133;D;0;aid=pardes\x07" ++ marked } });
+ const before = try commandOutput(pane.terminal, gpa, 100);
defer gpa.free(before.text);
try std.testing.expect(before.whole);
try std.testing.expect(std.mem.startsWith(u8, before.text, "printed"));
@@ -2876,7 +2762,7 @@ test "a finished command's output erased by a later ED 3 is not called whole, an
// where another program's text would be read as its output.
p.update(.{ .output = .{ .pane = 0, .bytes = &lineBreaks(20) } });
p.update(.{ .output = .{ .pane = 0, .bytes = "\x1b[3J" } });
- const after = try commandOutput(pane, gpa, 100);
+ const after = try commandOutput(pane.terminal, gpa, 100);
defer gpa.free(after.text);
try std.testing.expect(!after.whole);
try std.testing.expectEqualStrings("", after.text);