diff options
| author | Gabriel Schneider <[email protected]> | 2026-11-17 00:32:48 -0300 |
|---|---|---|
| committer | Gabriel Schneider <[email protected]> | 2026-11-17 12:15:01 -0300 |
| commit | 1d382a3ed42788e82301d0ce2a16ebc35c79046c (patch) | |
| tree | 2da33a886055944f17dc7e6c53cdc0aa49735c97 /src/panes.zig | |
| parent | 9c2fc736b9ccd7b7eea11e7f544ed540d2d2a6c2 (diff) | |
| download | pardes-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/panes.zig')
| -rw-r--r-- | src/panes.zig | 76 |
1 files changed, 73 insertions, 3 deletions
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; |
