pty 实验性
实验性
本包属于实验层:任何 0.x 版本都可能有不兼容变化(docs/release.md「API 稳定性」)。
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 稳定性").
变量
var ErrUnsupported = errors.New("pty: not supported on this platform")ErrUnsupported is returned by Start on platforms without local pseudo-terminals.
函数
func DefaultShell
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
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
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
func Start(o Options) (*PTY, error)Start starts the program described by o on a new pseudo-terminal.
func (*PTY) Close
func (p *PTY) Close() errorClose 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
func (p *PTY) Pid() intPid returns the program's process ID.
func (*PTY) Read
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
func (p *PTY) Resize(cols, rows, widthPx, heightPx int) errorResize 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
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
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.