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

..03-May-2022-

.gitignoreH A D16-Oct-202127 54

Dockerfile.golangH A D16-Oct-2021327 1813

Dockerfile.riscvH A D16-Oct-2021735 2418

LICENSEH A D16-Oct-20211 KiB2420

README.mdH A D16-Oct-20212.3 KiB10885

asm_solaris_amd64.sH A D16-Oct-2021435 195

doc.goH A D16-Oct-2021348 179

go.modH A D16-Oct-202138 42

ioctl.goH A D16-Oct-2021311 2013

ioctl_bsd.goH A D16-Oct-20211.1 KiB4128

ioctl_solaris.goH A D16-Oct-20211.1 KiB4931

mktypes.bashH A D16-Oct-2021336 2014

pty_darwin.goH A D16-Oct-20211.3 KiB6953

pty_dragonfly.goH A D16-Oct-20211.6 KiB8465

pty_freebsd.goH A D16-Oct-20211.5 KiB8267

pty_linux.goH A D16-Oct-20211.2 KiB5542

pty_netbsd.goH A D16-Oct-20211.4 KiB7047

pty_openbsd.goH A D16-Oct-2021810 3720

pty_solaris.goH A D16-Oct-20213.3 KiB153113

pty_unsupported.goH A D16-Oct-2021272 137

run.goH A D16-Oct-20212 KiB7645

test_crosscompile.shH A D16-Oct-20212 KiB6540

types.goH A D16-Oct-2021100 126

types_dragonfly.goH A D16-Oct-2021265 196

types_freebsd.goH A D16-Oct-2021227 176

types_netbsd.goH A D16-Oct-2021236 197

types_openbsd.goH A D16-Oct-2021186 164

winsize.goH A D16-Oct-2021642 2516

winsize_unix.goH A D16-Oct-2021878 3622

winsize_unsupported.goH A D16-Oct-2021423 2413

ztypes_386.goH A D16-Oct-2021147 135

ztypes_amd64.goH A D16-Oct-2021151 135

ztypes_arm.goH A D16-Oct-2021147 135

ztypes_arm64.goH A D16-Oct-2021151 135

ztypes_dragonfly_amd64.goH A D16-Oct-2021261 189

ztypes_freebsd_386.goH A D16-Oct-2021219 178

ztypes_freebsd_amd64.goH A D16-Oct-2021254 189

ztypes_freebsd_arm.goH A D16-Oct-2021219 178

ztypes_freebsd_arm64.goH A D16-Oct-2021234 178

ztypes_freebsd_ppc64.goH A D16-Oct-2021202 159

ztypes_loong64.goH A D16-Oct-2021156 135

ztypes_mipsx.goH A D16-Oct-2021230 145

ztypes_netbsd_32bit_int.goH A D16-Oct-2021261 1811

ztypes_openbsd_32bit_int.goH A D16-Oct-2021234 158

ztypes_ppc64.goH A D16-Oct-2021151 135

ztypes_ppc64le.goH A D16-Oct-2021155 135

ztypes_riscvx.goH A D16-Oct-2021181 135

ztypes_s390x.goH A D16-Oct-2021151 135

README.md

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