Esc

Pty

moduleextensionexperimentalSource
  • macOS: Everything works on macOS
  • Linux: Everything works on Linux
  • Windows: Everything works on Windows
  • Wasm: Everything works on Wasm
·since 0.36.1

#import "Extensions/Pty";

Nonblocking pseudoterminal sessions for macOS and Linux.

Procedures#

open_sessionexperimentalsince 0.36.1Source
(shell: string, command: string = "", cols: int = 80, rows: int = 24) -> (Session, bool)

Starts shell as an interactive shell, or with -c command when command is nonempty. The child inherits the environment with TERM=xterm-256color. Call before starting other threads. Returns false on setup failure. Windows returns unsupported without importing POSIX.

session, ok := open_session("/bin/sh", "printf hello", 80, 24);
if ok close_session(*session);
↗
read_sessionexperimentalsince 0.36.1Source
(session: *Session, buffer: [] u8) -> int

Reads available output: positive byte count, zero for would-block, -1 for EOF/error. The caller owns buffer. poll_exit separately reports the child's exit status.

write_sessionexperimentalsince 0.36.1Source
(session: *Session, text: string) -> int

Writes as much input as fits without blocking. Returns bytes written (zero on would-block). Keep the unwritten suffix and retry next frame.

resize_sessionexperimentalsince 0.36.1Source
(session: *Session, cols: int, rows: int) -> bool

Changes the PTY size. The kernel sends SIGWINCH to its foreground process group.

poll_exitexperimentalsince 0.36.1Source
(session: *Session) -> bool

Reaps the child without blocking. status is the native wait status, not an exit code.

close_sessionexperimentalsince 0.36.1Source
(session: *Session)

Closes the master, terminates a remaining child and reaps it. Safe on an empty session.

Types#

Sessionstructexperimentalsince 0.36.1Source

A child process and its PTY. Close with close_session; do not copy live sessions.

Session :: struct {
    fd: s32 = -1;
    pid: s32 = -1;
    exited: bool;
    status: s32;
}