pty

package
v0.0.56 Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package pty starts a command in a pseudo-terminal the engine owns and ends the command's process group. The command leads its own session with the terminal as its controlling terminal, so an agent's program in it behaves as it would in a person's terminal; the engine reads what it writes and types into it. It works on Linux and macOS; elsewhere Start refuses.

It parses nothing it reads: what a terminal's output means is another package's concern.

Index

Constants

View Source
const (
	DefaultCols = 120
	DefaultRows = 40
)

The terminal's size when Options leaves it unset.

Variables

This section is empty.

Functions

This section is empty.

Types

type Options

type Options struct {
	// Command is the command line, run as /bin/sh -c Command.
	Command string
	// Dir is the directory the command starts in; empty means the engine's.
	Dir string
	// Env is the command's whole environment, as KEY=value lines. Nothing
	// is taken from the engine's own: nil means an empty environment.
	Env []string
	// Cols and Rows are the terminal's size; 0 means DefaultCols and
	// DefaultRows.
	Cols, Rows uint16
}

Options says what to start and in what terminal.

type Process

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

Process is a command running in a terminal the engine owns.

Who waits: exactly one caller calls Wait, which reaps the process and then closes exited. End signals the process group and watches exited, so End is meant to be called while another goroutine is blocked in Wait (or before Wait is called); it never reaps the process itself.

func Start

func Start(o Options) (*Process, error)

Start starts o.Command in a new terminal and returns once the process is running. The process leads its own session and process group, with the terminal as its standard input, output and error and as its controlling terminal. The caller must call Wait once and Close once.

func (*Process) Close

func (p *Process) Close() error

Close closes the engine's side of the terminal. A Read of Output that is blocked returns, and the processes still holding the terminal see it hang up. Close does not end or wait for the process.

func (*Process) End

func (p *Process) End(grace time.Duration)

End ends the process group: it sends SIGHUP and SIGTERM to the group, then SIGKILL to the group if the process has not exited when grace has passed. It returns as soon as Wait has seen the process exit, or right after the SIGKILL; it does not reap the process, which is Wait's job. If no goroutine is in Wait, End cannot see the exit and sends SIGKILL after grace.

The group's id is the process's pid. Once the process has been reaped and every member of its group has exited, that id may be reused; End called then may signal a stranger's group, so callers end a process before, or while, they wait for it.

func (*Process) Output

func (p *Process) Output() io.Reader

Output reads what the process writes to its terminal. It returns io.EOF once the process side of the terminal is closed: the command and every process that inherited the terminal have exited or closed it. After Close it returns an error.

func (*Process) Pid

func (p *Process) Pid() int

Pid is the command's process id. The command leads its own session and process group, so it is also the group's id.

func (*Process) Wait

func (p *Process) Wait() (int, error)

Wait waits for the process to exit and returns its exit code: the code it exited with, or 128 plus the signal's number when a signal ended it, as a shell reports it. The error is for a wait that failed, not for a non-zero code. Wait may be called once; a second call returns an error at once.

func (*Process) Write

func (p *Process) Write(b []byte) (int, error)

Write writes to the terminal as if typed on its keyboard: the command reads it on its standard input.

Jump to

Keyboard shortcuts

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