PTY interface for Go
Clone or download
Permalink
Failed to load latest commit information.
.gitignore initial May 3, 2011
License add MIT license; closes #2 Apr 5, 2012
README.md Add shell example to readme Jan 6, 2018
doc.go define public symbols without build constraints Mar 5, 2014
ioctl.go Don't attempt to build on windows Nov 4, 2015
ioctl_bsd.go Portable ioctl calls Mar 14, 2014
mktypes.bash add openbsd/amd64 support (#58) Jan 13, 2018
pty_darwin.go Improve error management for openpty (#57) Jan 13, 2018
pty_dragonfly.go Improve error management for openpty (#57) Jan 13, 2018
pty_freebsd.go Improve error management for openpty (#57) Jan 13, 2018
pty_linux.go Improve error management for openpty (#57) Jan 13, 2018
pty_openbsd.go add openbsd/amd64 support (#58) Jan 13, 2018
pty_unsupported.go add openbsd/amd64 support (#58) Jan 13, 2018
run.go Add StartWithSize to allow resizing pty before starting a command to … Sep 15, 2018
types.go Portable ioctl calls Mar 14, 2014
types_dragonfly.go Support DragonFly BSD May 20, 2016
types_freebsd.go add generated ztypes_arm.go and ztypes_freebsd_arm.go Mar 16, 2014
types_openbsd.go add openbsd/amd64 support (#58) Jan 13, 2018
util.go Added terminal/pty resize functionality and utility function to inher… Jan 6, 2018
ztypes_386.go Portable ioctl calls Mar 14, 2014
ztypes_amd64.go Portable ioctl calls Mar 14, 2014
ztypes_arm.go add generated ztypes_arm.go and ztypes_freebsd_arm.go Mar 16, 2014
ztypes_arm64.go Prevent arm64 file to be compiled anywhere else. Fixed #32. May 11, 2015
ztypes_dragonfly_amd64.go Support DragonFly BSD May 20, 2016
ztypes_freebsd_386.go add generated ztypes_arm.go and ztypes_freebsd_arm.go Mar 16, 2014
ztypes_freebsd_amd64.go add generated ztypes_arm.go and ztypes_freebsd_arm.go Mar 16, 2014
ztypes_freebsd_arm.go add generated ztypes_arm.go and ztypes_freebsd_arm.go Mar 16, 2014
ztypes_mipsx.go Add mips64 mips64le build tags (#47) Mar 7, 2017
ztypes_openbsd_386.go Add support for OpenBSD 386. Prevents compile errors. (#60) Jun 20, 2018
ztypes_openbsd_amd64.go add openbsd/amd64 support (#58) Jan 13, 2018
ztypes_ppc64.go Add support for Power (ppc64, ppc64le) and System z (s390x) Nov 21, 2014
ztypes_ppc64le.go Add support for Power (ppc64, ppc64le) and System z (s390x) Nov 21, 2014
ztypes_s390x.go Add support for Power (ppc64, ppc64le) and System z (s390x) Nov 21, 2014

README.md

pty

Pty is a Go package for using unix pseudo-terminals.

Install

go get github.com/kr/pty

Example

Command

package main

import (
	"github.com/kr/pty"
	"io"
	"os"
	"os/exec"
)

func main() {
	c := exec.Command("grep", "--color=auto", "bar")
	f, err := pty.Start(c)
	if err != nil {
		panic(err)
	}

	go func() {
		f.Write([]byte("foo\n"))
		f.Write([]byte("bar\n"))
		f.Write([]byte("baz\n"))
		f.Write([]byte{4}) // EOT
	}()
	io.Copy(os.Stdout, f)
}

Shell

package main

import (
        "io"
        "log"
        "os"
        "os/exec"
        "os/signal"
        "syscall"

        "github.com/kr/pty"
        "golang.org/x/crypto/ssh/terminal"
)

func test() error {
        // Create arbitrary command.
        c := exec.Command("bash")

        // Start the command with a pty.
        ptmx, err := pty.Start(c)
        if err != nil {
                return err
        }
        // Make sure to close the pty at the end.
        defer func() { _ = ptmx.Close() }() // Best effort.

        // Handle pty size.
        ch := make(chan os.Signal, 1)
        signal.Notify(ch, syscall.SIGWINCH)
        go func() {
                for range ch {
                        if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
                                log.Printf("error resizing pty: %s", err)
                        }
                }
        }()
        ch <- syscall.SIGWINCH // Initial resize.

        // Set stdin in raw mode.
        oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
        if err != nil {
                panic(err)
        }
        defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.

        // Copy stdin to the pty and the pty to stdout.
        go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
        _, _ = io.Copy(os.Stdout, ptmx)

        return nil
}

func main() {
        if err := test(); err != nil {
                log.Fatal(err)
        }
}