• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

.gitignoreH A D07-May-202027

Dockerfile.riscvH A D07-May-2020490

LICENSEH A D07-May-20201 KiB

README.mdH A D07-May-20201.9 KiB

doc.goH A D07-May-2020349

go.modH A D07-May-202039

ioctl.goH A D07-May-2020203

ioctl_bsd.goH A D07-May-20201.1 KiB

ioctl_solaris.goH A D07-May-2020630

mktypes.bashH A D07-May-2020329

pty_darwin.goH A D07-May-20201.2 KiB

pty_dragonfly.goH A D07-May-20201.5 KiB

pty_freebsd.goH A D07-May-20201.5 KiB

pty_linux.goH A D07-May-20201,004

pty_openbsd.goH A D07-May-2020773

pty_solaris.goH A D07-May-20203.1 KiB

pty_unsupported.goH A D07-May-2020175

run.goH A D07-May-20201.9 KiB

test_crosscompile.shH A D07-May-20201.1 KiB

types.goH A D07-May-202083

types_dragonfly.goH A D07-May-2020248

types_freebsd.goH A D07-May-2020210

types_openbsd.goH A D07-May-2020169

util.goH A D07-May-20201.5 KiB

util_solaris.goH A D07-May-20201.1 KiB

ztypes_386.goH A D07-May-2020118

ztypes_amd64.goH A D07-May-2020118

ztypes_arm.goH A D07-May-2020118

ztypes_arm64.goH A D07-May-2020135

ztypes_dragonfly_amd64.goH A D07-May-2020205

ztypes_freebsd_386.goH A D07-May-2020171

ztypes_freebsd_amd64.goH A D07-May-2020202

ztypes_freebsd_arm.goH A D07-May-2020171

ztypes_freebsd_arm64.goH A D07-May-2020182

ztypes_mipsx.goH A D07-May-2020173

ztypes_openbsd_386.goH A D07-May-2020188

ztypes_openbsd_amd64.goH A D07-May-2020190

ztypes_ppc64.goH A D07-May-2020135

ztypes_ppc64le.goH A D07-May-2020137

ztypes_riscvx.goH A D07-May-2020154

ztypes_s390x.goH A D07-May-2020135

README.md

1# pty
2
3Pty is a Go package for using unix pseudo-terminals.
4
5## Install
6
7    go get github.com/creack/pty
8
9## Example
10
11### Command
12
13```go
14package main
15
16import (
17	"github.com/creack/pty"
18	"io"
19	"os"
20	"os/exec"
21)
22
23func main() {
24	c := exec.Command("grep", "--color=auto", "bar")
25	f, err := pty.Start(c)
26	if err != nil {
27		panic(err)
28	}
29
30	go func() {
31		f.Write([]byte("foo\n"))
32		f.Write([]byte("bar\n"))
33		f.Write([]byte("baz\n"))
34		f.Write([]byte{4}) // EOT
35	}()
36	io.Copy(os.Stdout, f)
37}
38```
39
40### Shell
41
42```go
43package main
44
45import (
46        "io"
47        "log"
48        "os"
49        "os/exec"
50        "os/signal"
51        "syscall"
52
53        "github.com/creack/pty"
54        "golang.org/x/crypto/ssh/terminal"
55)
56
57func test() error {
58        // Create arbitrary command.
59        c := exec.Command("bash")
60
61        // Start the command with a pty.
62        ptmx, err := pty.Start(c)
63        if err != nil {
64                return err
65        }
66        // Make sure to close the pty at the end.
67        defer func() { _ = ptmx.Close() }() // Best effort.
68
69        // Handle pty size.
70        ch := make(chan os.Signal, 1)
71        signal.Notify(ch, syscall.SIGWINCH)
72        go func() {
73                for range ch {
74                        if err := pty.InheritSize(os.Stdin, ptmx); err != nil {
75                                log.Printf("error resizing pty: %s", err)
76                        }
77                }
78        }()
79        ch <- syscall.SIGWINCH // Initial resize.
80
81        // Set stdin in raw mode.
82        oldState, err := terminal.MakeRaw(int(os.Stdin.Fd()))
83        if err != nil {
84                panic(err)
85        }
86        defer func() { _ = terminal.Restore(int(os.Stdin.Fd()), oldState) }() // Best effort.
87
88        // Copy stdin to the pty and the pty to stdout.
89        go func() { _, _ = io.Copy(ptmx, os.Stdin) }()
90        _, _ = io.Copy(os.Stdout, ptmx)
91
92        return nil
93}
94
95func main() {
96        if err := test(); err != nil {
97                log.Fatal(err)
98        }
99}
100```
101