Skip to content

pty 实验性 ​

实验性

本包属于实验层:任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。

go
import "github.com/tangoui/tango/sys/pty"

本页由 site/tools/apigen 从源码注释生成(按 darwin 构建标签选文件),请勿手改。

Package pty runs programs on local pseudo-terminals, the connection a terminal emulator such as ui/terminal needs to run a shell.

A PTY satisfies the ui/terminal Conn interface: Read returns the program's output, Write sends it input, Resize changes the terminal size it sees and Close hangs it up.

On macOS and Linux the terminal is a /dev/ptmx pair: the program runs in a new session with the subsidiary device as its controlling terminal and standard input, output and error. On Windows it is a ConPTY pseudo console, which needs Windows 10 1809 or later. Other platforms (iOS, Android, js/wasm…) have no local terminals: Start returns ErrUnsupported.

The program's environment defaults to the current process's, and TERM=xterm-256color and COLORTERM=truecolor are always set. On macOS and Linux, an environment without a locale (no LC_ALL, LC_CTYPE or LANG, as for an app opened from Finder or the Dock) also gets a UTF-8 LC_CTYPE, so the shell reads typed non-ASCII text as characters.

Experimental: this package's API may change in any 0.x release (see docs/release.md, "API 稳定性").

变量 ​

go
var ErrUnsupported = errors.New("pty: not supported on this platform")

ErrUnsupported is returned by Start on platforms without local pseudo-terminals.

函数 ​

func DefaultShell ​

go
func DefaultShell() (path string, args []string)

DefaultShell returns the user's shell, $SHELL, or /bin/sh when it is not set, with the arguments to pass as Options.Args.

类型 ​

Options ​

go
type Options struct {
	// Path is the program to run. A name without a path separator is
	// looked up in the current process's PATH, as exec.Command does; a
	// relative path with a separator is relative to Dir.
	Path string
	// Args are the program's arguments, starting with the program name
	// (argv[0]). Empty means {Path}. On Windows they are joined into one
	// command line quoted by the MSVCRT rules, which cmd.exe and
	// PowerShell do not follow: never embed untrusted strings in a
	// "cmd /c" or "pwsh -Command" line.
	Args []string
	// Dir is the working directory; empty means the current one.
	Dir string
	// Env is the environment, as "key=value" strings; nil means
	// os.Environ(). TERM and COLORTERM are replaced either way. On
	// Windows SYSTEMROOT is added from the current environment if Env
	// lacks it, as os/exec does. On macOS and Linux a UTF-8 LC_CTYPE is
	// added when Env sets no LC_ALL, LC_CTYPE or LANG.
	Env []string
	// Cols and Rows are the initial terminal size in cells; 0 means
	// 80×24.
	Cols, Rows int
}

Options configures the program Start runs.

PTY ​

go
type PTY struct {
	// contains filtered or unexported fields
}

PTY is a program running on a pseudo-terminal. Its methods are safe for concurrent use; Read and Write may each be pending in one goroutine while another calls Resize or Close.

func Start ​

go
func Start(o Options) (*PTY, error)

Start starts the program described by o on a new pseudo-terminal.

func (*PTY) Close ​

go
func (p *PTY) Close() error

Close closes the terminal and unblocks a pending Read and Write. On Unix the kernel then sends SIGHUP to the program's session; on Windows the pseudo console is closed, which ends the programs attached to it. Close does not wait for the program to exit; see PTY.Wait. It is safe to call more than once, and every call returns the first call's result.

func (*PTY) Pid ​

go
func (p *PTY) Pid() int

Pid returns the program's process ID.

func (*PTY) Read ​

go
func (p *PTY) Read(b []byte) (int, error)

Read reads the program's output. After Close it returns an error wrapping os.ErrClosed.

On Unix Read returns io.EOF once the program and every other process using the terminal (such as a job it left in the background) have exited and their output has been read. On Windows it returns io.EOF once the started program has exited: the pseudo console is then closed, after its output has been quiet for 100 ms (at most 1 s after the exit), and closing it ends any other process still attached to it.

func (*PTY) Resize ​

go
func (p *PTY) Resize(cols, rows, widthPx, heightPx int) error

Resize sets the terminal size the program sees, in cells and pixels (pixels may be 0). The program is told by SIGWINCH on Unix. Resize does not block; it returns an error after Close.

func (*PTY) Wait ​

go
func (p *PTY) Wait() (exitCode int, err error)

Wait waits for the program to exit and returns its exit code. The program is reaped in the background whether or not Wait is called, and Wait may be called more than once. A program killed by a signal has exit code -1 and err describes the signal; err is also set if the exit status could not be obtained. Wait waits only for the started program; on Windows its exit also closes the pseudo console, ending any other process attached to it (see PTY.Read).

func (*PTY) Write ​

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

Write sends input to the program, as typed keys. After Close it returns an error wrapping os.ErrClosed.

以 MIT 许可发布