pty

package
v0.3.5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 14, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsInteractiveTerminal

func IsInteractiveTerminal() bool

IsInteractiveTerminal returns true if stdin is a real terminal (TTY). Returns false in CI environments (when the "CI" env var set to "true"), when input is piped, or in non-interactive shells.

Types

type ExitError

type ExitError struct {
	Code int
	Err  error // Underlying error from ptyx
}

ExitError is returned when the child process exits with non-zero code

func (*ExitError) Error

func (e *ExitError) Error() string

func (*ExitError) Unwrap

func (e *ExitError) Unwrap() error

Unwrap allows errors.Is and errors.As to work

type InputRouter

type InputRouter struct {
	// contains filtered or unexported fields
}

InputRouter manages routing stdin to either PTY or a prompt pipe. Only ONE goroutine should call ReadLoop().

func NewInputRouter

func NewInputRouter(ptyWriter io.Writer) (*InputRouter, error)

func (*InputRouter) ReadLoop

func (r *InputRouter) ReadLoop(src io.Reader)

ReadLoop continuously reads from src and routes data to the current destination.

IMPORTANT: Only ONE goroutine should call ReadLoop() because:

  1. Multiple readers on the same source (e.g., stdin) cause data splitting - one goroutine might read "hel" while another reads "lo\n"
  2. Concurrent routing decisions create race conditions on the destination
  3. User input becomes unpredictably interleaved between readers

This function blocks until src returns an error (e.g., EOF).

func (*InputRouter) RouteToPTY

func (r *InputRouter) RouteToPTY()

RouteToPTY switches input back to the PTY (default)

func (*InputRouter) RouteToPrompt

func (r *InputRouter) RouteToPrompt(w io.Writer)

RouteToPrompt switches input to go to the given writer (prompt pipe)

type InteractiveSession

type InteractiveSession interface {
	// PtyWriter returns the writer to send input to the child process
	PtyWriter() io.Writer

	// PtyReader returns the reader to receive output from the child process
	PtyReader() io.Reader

	// SetRawMode puts terminal in raw mode (for PTY passthrough)
	SetRawMode() error

	// SetCookedMode restores normal terminal mode (for prompts)
	SetCookedMode() error

	// Wait blocks until the child process exits
	// Returns ExitError if process exited with non-zero code
	Wait() error

	// Close cleans up resources (PTY, terminal state)
	Close() error
}

InteractiveSession manages a PTY-based command execution with support for input/output routing and terminal mode switching.

func NewSession

func NewSession(ctx context.Context, cfg SessionConfig) (InteractiveSession, error)

NewSession creates a new interactive PTY session. The terminal is put into raw mode automatically.

type OutputRouter

type OutputRouter struct {
	// contains filtered or unexported fields
}

OutputRouter manages buffered vs live output.

func NewOutputRouter

func NewOutputRouter(out io.Writer) (*OutputRouter, error)

func (*OutputRouter) Pause

func (r *OutputRouter) Pause()

Pause starts buffering output. Call this before showing a confirmation prompt.

func (*OutputRouter) Resume

func (r *OutputRouter) Resume()

Resume stops buffering, flushes any buffered output, and resumes live output. Call this after the confirmation prompt is complete.

func (*OutputRouter) Write

func (r *OutputRouter) Write(p []byte) (n int, err error)

type SessionConfig

type SessionConfig struct {
	Command string
	Args    []string
	Env     []string
}

SessionConfig holds options for creating a session

func NewSessionConfig

func NewSessionConfig(cmd string, args, env []string) SessionConfig

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL