| 1 | // Package xpty provides platform-independent interfaces to interact with |
| 2 | // pseudo-terminals (PTYs) in Go. It abstracts the differences between Unix and |
| 3 | // Windows systems and supports both ConPTY and classic Unix PTYs. |
| 4 | package xpty |
| 5 | |
| 6 | import ( |
| 7 | "context" |
| 8 | "errors" |
| 9 | "io" |
| 10 | "os" |
| 11 | "os/exec" |
| 12 | "runtime" |
| 13 | |
| 14 | "github.com/charmbracelet/x/term" |
| 15 | "github.com/creack/pty" |
| 16 | ) |
| 17 | |
| 18 | // ErrUnsupported is returned when a feature is not supported. |
| 19 | var ErrUnsupported = pty.ErrUnsupported |
| 20 | |
| 21 | // Pty represents a PTY (pseudo-terminal) interface. |
| 22 | type Pty interface { |
| 23 | term.File |
| 24 | io.ReadWriteCloser |
| 25 | |
| 26 | // Resize resizes the PTY. |
| 27 | Resize(width, height int) error |
| 28 | |
| 29 | // Size returns the size of the PTY. |
| 30 | Size() (width, height int, err error) |
| 31 | |
| 32 | // Name returns the name of the PTY. |
| 33 | Name() string |
| 34 | |
| 35 | // Start starts a command on the PTY. |
| 36 | // The command started will have its standard input, output, and error |
| 37 | // connected to the PTY. |
| 38 | // On Windows, calling Wait won't work since the Go runtime doesn't handle |
| 39 | // ConPTY processes correctly. See https://github.com/golang/go/pull/62710. |
| 40 | Start(cmd *exec.Cmd) error |
| 41 | } |
| 42 | |
| 43 | // Options represents PTY options. |
| 44 | type Options struct { |
| 45 | Flags int |
| 46 | } |
| 47 | |
| 48 | // PtyOption is a PTY option. |
| 49 | type PtyOption func(o Options) |
| 50 | |
| 51 | // NewPty creates a new PTY. |
| 52 | // |
| 53 | // The returned PTY will be a Unix PTY on Unix systems and a ConPTY on Windows. |
| 54 | // The width and height parameters specify the initial size of the PTY. |
| 55 | // You can pass additional options to the PTY by passing PtyOptions. |
| 56 | // |
| 57 | // pty, err := xpty.NewPty(80, 24) |
| 58 | // if err != nil { |
| 59 | // // handle error |
| 60 | // } |
| 61 | // |
| 62 | // defer pty.Close() // Make sure to close the PTY when done. |
| 63 | // switch pty := pty.(type) { |
| 64 | // case xpty.UnixPty: |
| 65 | // // Unix PTY |
| 66 | // case xpty.ConPty: |
| 67 | // // ConPTY |
| 68 | // } |
| 69 | func NewPty(width, height int, opts ...PtyOption) (Pty, error) { |
| 70 | if runtime.GOOS == "windows" { |
| 71 | return NewConPty(width, height, opts...) |
| 72 | } |
| 73 | return NewUnixPty(width, height, opts...) |
| 74 | } |
| 75 | |
| 76 | // WaitProcess waits for the process to exit. |
| 77 | // This exists because on Windows, cmd.Wait() doesn't work with ConPty. |
| 78 | // When the OS is not windows, it'll simply fall back to cmd.Wait(). |
| 79 | func WaitProcess(ctx context.Context, cmd *exec.Cmd) (err error) { |
| 80 | if runtime.GOOS != "windows" { |
| 81 | return cmd.Wait() //nolint:wrapcheck |
| 82 | } |
| 83 | |
| 84 | if cmd.Process == nil { |
| 85 | return errors.New("process not started") |
| 86 | } |
| 87 | |
| 88 | type result struct { |
| 89 | *os.ProcessState |
| 90 | error |
| 91 | } |
| 92 | |
| 93 | donec := make(chan result, 1) |
| 94 | go func() { |
| 95 | state, err := cmd.Process.Wait() |
| 96 | donec <- result{state, err} |
| 97 | }() |
| 98 | |
| 99 | select { |
| 100 | case <-ctx.Done(): |
| 101 | err = cmd.Process.Kill() |
| 102 | case r := <-donec: |
| 103 | cmd.ProcessState = r.ProcessState |
| 104 | err = r.error |
| 105 | } |
| 106 | |
| 107 | return err |
| 108 | } |