summaryrefslogtreecommitdiff
path: root/src/term/terminal.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-17 00:32:48 -0300
committerGabriel Schneider <[email protected]>2026-11-17 12:15:01 -0300
commit1d382a3ed42788e82301d0ce2a16ebc35c79046c (patch)
tree2da33a886055944f17dc7e6c53cdc0aa49735c97 /src/term/terminal.zig
parent9c2fc736b9ccd7b7eea11e7f544ed540d2d2a6c2 (diff)
downloadpardes-1d382a3ed42788e82301d0ce2a16ebc35c79046c.tar.gz
pardes-1d382a3ed42788e82301d0ce2a16ebc35c79046c.zip
Term: prompt and commands through the interface: Term.prompt is the plain record of where the shell is in its command cycle (its phase, commands started, prompts drawn, lines declined, a continuation, the last command to finish and its status, input ready, input empty, the cursor at a prompt), Term.commandOutput what the newest command printed and Term.endOutputHere ends a command's output at the cursor; the pending command queue is the core's (Pane.pending_command, Pane.queuePendingCommand and Pane.releasePendingCommand, released at the shell's start or its prompt where Core.zig hears of them), and so are Pane.greeting, Pane.noteCommand, Kill's "the command pardes typed runs" (builtins.zig) and the newlines that keep a command's output below the edit copy (exec.zig); /pty/status, /pty/run and predicted echo read the record; promptInputReady, promptInputEmpty, commandRunning, armShellSpawn, shellSpawned, deinitPendingCommand and padOutputBelowEdits go; nothing else changes
Diffstat (limited to 'src/term/terminal.zig')
-rw-r--r--src/term/terminal.zig208
1 files changed, 47 insertions, 161 deletions
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);