A small Go package for starting commands attached to a pseudo terminal.
It supports Unix PTYs on AIX, Linux, macOS, DragonFly BSD, FreeBSD, NetBSD,
OpenBSD, Solaris, and z/OS, plus Windows ConPTY. Unsupported platforms return
errors.ErrUnsupported.
go get github.com/phuslu/ptypackage main
import (
"context"
"fmt"
"io"
"os"
"os/exec"
"runtime"
"time"
"github.com/phuslu/pty"
)
func main() {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
cmd := exec.Command("/bin/sh", "-c", "printf 'hello from pty\\n'")
if runtime.GOOS == "windows" {
cmd = exec.Command("cmd.exe", "/d", "/c", "echo hello from pty")
}
ptmx, err := pty.StartWithSize(ctx, cmd, &pty.Winsize{Rows: 30, Cols: 100})
if err != nil {
panic(err)
}
defer ptmx.Close()
if _, err := io.Copy(os.Stdout, ptmx); err != nil {
fmt.Fprintln(os.Stderr, err)
}
}Start(ctx, cmd)startscmdwith stdin, stdout, and stderr connected to a PTY.StartWithSize(ctx, cmd, size)startscmdwith an initial terminal size.GetSize(pty)returns a running PTY's current terminal size. On Windows, it returnserrors.ErrUnsupported.SetSize(pty, size)resizes a running PTY.IsTerminal(fd)reports whether a file descriptor, such asos.Stdout.Fd(), is a terminal.