-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathterminal.go
97 lines (80 loc) · 1.91 KB
/
terminal.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
package chooser
import (
"fmt"
"syscall"
"unsafe"
"github.com/pkg/term/termios"
"golang.org/x/sys/unix"
)
const maxReadBytes = 1024
type terminal struct {
org unix.Termios
}
type winSize struct {
row uint16
col uint16
}
type ioctlWinsize struct {
Row uint16
Col uint16
X uint16
Y uint16
}
func (t *terminal) read() ([]byte, error) {
buf := make([]byte, maxReadBytes)
n, err := syscall.Read(syscall.Stdin, buf)
if err != nil {
return []byte{}, err
}
return buf[:n], nil
}
func (t *terminal) setRawMode() error {
org := t.org
org.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.IEXTEN | syscall.ISIG
org.Cc[syscall.VTIME] = 0
org.Cc[syscall.VMIN] = 1
return termios.Tcsetattr(uintptr(syscall.Stdin), termios.TCSANOW, &org)
}
func (t *terminal) resetMode() error {
return termios.Tcsetattr(uintptr(syscall.Stdin), termios.TCSANOW, &t.org)
}
func (t *terminal) getWinSize() (*winSize, error) {
ws := &ioctlWinsize{}
_, _, errno := syscall.Syscall(
syscall.SYS_IOCTL,
uintptr(syscall.Stdin),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if errno != 0 {
return &winSize{}, errno
}
return &winSize{
row: ws.Row,
col: ws.Col,
}, nil
}
func (t *terminal) setup() error {
if err := syscall.SetNonblock(syscall.Stdin, true); err != nil {
return fmt.Errorf("Cannot set non blocking mode: %w", err)
}
if err := t.setRawMode(); err != nil {
return fmt.Errorf("Cannot set raw mode: %w", err)
}
return nil
}
func (t *terminal) restore() error {
if err := syscall.SetNonblock(syscall.Stdin, false); err != nil {
return fmt.Errorf("Cannot set blocking mode: %w", err)
}
if err := t.resetMode(); err != nil {
return fmt.Errorf("Cannot reset from raw mode: %w", err)
}
return nil
}
func newTerminal() (*terminal, error) {
var org unix.Termios
if err := termios.Tcgetattr(uintptr(syscall.Stdin), &org); err != nil {
return &terminal{}, err
}
return &terminal{org: org}, nil
}