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