Skip to content
Go Shell
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore initial May 3, 2011
Dockerfile.riscv Bump go 1.13 Sep 25, 2019
LICENSE s/kr/creack/ in README Jun 29, 2019
README.md s/kr/creack/ in README Jun 29, 2019
doc.go define public symbols without build constraints Mar 5, 2014
go.mod Bump go 1.13 Sep 25, 2019
ioctl.go support for Solaris (#64) Jun 29, 2019
ioctl_bsd.go Portable ioctl calls Mar 14, 2014
ioctl_solaris.go support for Solaris (#64) Jun 29, 2019
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 remove deprecated nomenclature Jan 31, 2019
pty_openbsd.go remove deprecated nomenclature Jan 31, 2019
pty_solaris.go support for Solaris (#64) Jun 29, 2019
pty_unsupported.go support for Solaris (#64) Jun 29, 2019
run.go Set Ctty in SysProcAttr (#75) Apr 1, 2019
test_crosscompile.sh Add riscv Jun 29, 2019
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 support for Solaris (#64) Jun 29, 2019
util_solaris.go support for Solaris (#64) Jun 29, 2019
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_freebsd_arm64.go Add support for freebsd/arm64 (#87) Dec 9, 2019
ztypes_mipsx.go
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_riscvx.go Add riscv Jun 29, 2019
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/creack/pty

Example

Command

package main

import (
	"github.com/creack/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/creack/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)
        }
}
You can’t perform that action at this time.