-
Notifications
You must be signed in to change notification settings - Fork 14
/
term_unix.go
96 lines (73 loc) · 1.9 KB
/
term_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
// +build aix linux darwin freebsd openbsd netbsd dragonfly solaris
// Copyright 2013-2015 Bowery, Inc.
package prompt
import (
"os"
"golang.org/x/sys/unix"
)
// List of unsupported $TERM values.
var unsupported = []string{"", "dumb", "cons25"}
// supportsEditing checks if the terminal supports ansi escapes.
func supportsEditing() bool {
term := os.Getenv("TERM")
for _, t := range unsupported {
if t == term {
return false
}
}
return true
}
// isNotTerminal checks if an error is related to the input not being a terminal.
func isNotTerminal(err error) bool {
return err == unix.ENOTTY
}
// terminal contains the private fields for a Unix terminal.
type terminal struct {
supportsEditing bool
fd uintptr
origMode unix.Termios
}
// newTerminal creates a terminal and sets it to raw input mode.
func newTerminal(in *os.File) (*terminal, error) {
term := &terminal{fd: in.Fd()}
if !supportsEditing() {
return term, nil
}
t, err := getTermios(term.fd)
if err != nil {
if IsNotTerminal(err) {
return term, nil
}
return nil, err
}
term.origMode = *t
mode := term.origMode
term.supportsEditing = true
// Set new mode flags, for reference see cfmakeraw(3).
mode.Iflag &^= (unix.BRKINT | unix.IGNBRK | unix.ICRNL |
unix.INLCR | unix.IGNCR | unix.ISTRIP | unix.IXON |
unix.PARMRK)
mode.Oflag &^= unix.OPOST
mode.Lflag &^= (unix.ECHO | unix.ECHONL | unix.ICANON |
unix.ISIG | unix.IEXTEN)
mode.Cflag &^= (unix.CSIZE | unix.PARENB)
mode.Cflag |= unix.CS8
// Set controls; min num of bytes, and timeouts.
mode.Cc[unix.VMIN] = 1
mode.Cc[unix.VTIME] = 0
err = setTermios(term.fd, true, &mode)
if err != nil {
return nil, err
}
return term, nil
}
// Close disables the terminals raw input.
func (term *terminal) Close() error {
if term.supportsEditing {
err := setTermios(term.fd, false, &term.origMode)
if err != nil {
return err
}
}
return nil
}