-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
tty_unix.go
163 lines (136 loc) · 3.26 KB
/
tty_unix.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//go:build !windows && !plan9
// +build !windows,!plan9
package tty
import (
"bufio"
"os"
"os/signal"
"syscall"
"golang.org/x/sys/unix"
)
type TTY struct {
in *os.File
bin *bufio.Reader
out *os.File
termios unix.Termios
ss chan os.Signal
}
func open(path string) (*TTY, error) {
tty := new(TTY)
in, err := os.Open(path)
if err != nil {
return nil, err
}
tty.in = in
tty.bin = bufio.NewReader(in)
out, err := os.OpenFile(path, unix.O_WRONLY, 0)
if err != nil {
return nil, err
}
tty.out = out
termios, err := unix.IoctlGetTermios(int(tty.in.Fd()), ioctlReadTermios)
if err != nil {
return nil, err
}
tty.termios = *termios
termios.Iflag &^= unix.ISTRIP | unix.INLCR | unix.ICRNL | unix.IGNCR | unix.IXOFF
termios.Lflag &^= unix.ECHO | unix.ICANON /*| unix.ISIG*/
termios.Cc[unix.VMIN] = 1
termios.Cc[unix.VTIME] = 0
if err := unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, termios); err != nil {
return nil, err
}
tty.ss = make(chan os.Signal, 1)
return tty, nil
}
func (tty *TTY) buffered() bool {
return tty.bin.Buffered() > 0
}
func (tty *TTY) readRune() (rune, error) {
r, _, err := tty.bin.ReadRune()
return r, err
}
func (tty *TTY) close() error {
if tty.out == nil || tty.in == nil {
return nil
}
signal.Stop(tty.ss)
close(tty.ss)
err1 := unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, &tty.termios)
err2 := tty.out.Close()
err3 := tty.in.Close()
tty.out = nil
tty.in = nil
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
return err3
}
func (tty *TTY) size() (int, int, error) {
x, y, _, _, err := tty.sizePixel()
return x, y, err
}
func (tty *TTY) sizePixel() (int, int, int, int, error) {
ws, err := unix.IoctlGetWinsize(int(tty.out.Fd()), unix.TIOCGWINSZ)
if err != nil {
return -1, -1, -1, -1, err
}
return int(ws.Col), int(ws.Row), int(ws.Xpixel), int(ws.Ypixel), nil
}
func (tty *TTY) input() *os.File {
return tty.in
}
func (tty *TTY) output() *os.File {
return tty.out
}
func (tty *TTY) raw() (func() error, error) {
termios, err := unix.IoctlGetTermios(int(tty.in.Fd()), ioctlReadTermios)
if err != nil {
return nil, err
}
backup := *termios
termios.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
termios.Oflag &^= unix.OPOST
termios.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
termios.Cflag &^= unix.CSIZE | unix.PARENB
termios.Cflag |= unix.CS8
termios.Cc[unix.VMIN] = 1
termios.Cc[unix.VTIME] = 0
if err = unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, termios); err != nil {
return nil, err
}
if err = syscall.SetNonblock(int(tty.in.Fd()), true); err != nil {
return nil, err
}
return func() error {
if err := unix.IoctlSetTermios(int(tty.in.Fd()), ioctlWriteTermios, &backup); err != nil {
return err
}
return nil
}, nil
}
func (tty *TTY) sigwinch() <-chan WINSIZE {
signal.Notify(tty.ss, unix.SIGWINCH)
ws := make(chan WINSIZE)
go func() {
defer close(ws)
for sig := range tty.ss {
if sig != unix.SIGWINCH {
continue
}
w, h, err := tty.size()
if err != nil {
continue
}
// send but do not block for it
select {
case ws <- WINSIZE{W: w, H: h}:
default:
}
}
}()
return ws
}