-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommand.go
68 lines (50 loc) · 1.23 KB
/
command.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
package iterm
import (
"fmt"
"os"
"github.com/normatov07/iterm/cursor"
"golang.org/x/sys/unix"
)
var defaultTermios unix.Termios
func SetRawMode() (err error) {
fd := int(os.Stdin.Fd())
term, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil {
return err
}
defaultTermios = *term
term.Iflag &^= unix.IGNBRK | unix.BRKINT | unix.PARMRK | unix.ISTRIP | unix.INLCR | unix.IGNCR | unix.ICRNL | unix.IXON
term.Oflag &^= unix.OPOST
term.Lflag &^= unix.ECHO | unix.ECHONL | unix.ICANON | unix.ISIG | unix.IEXTEN
term.Cflag &^= unix.CSIZE | unix.PARENB
term.Cflag |= unix.CS8
err = unix.IoctlSetTermios(fd, unix.TCSETS, term)
if err != nil {
return err
}
return nil
}
func RestoreTermMode() error {
return unix.IoctlSetTermios(int(os.Stdin.Fd()), unix.TCSETS, &defaultTermios)
}
func DisableOutput() error {
fd := int(os.Stdin.Fd())
term, err := unix.IoctlGetTermios(fd, unix.TCGETS)
if err != nil {
return err
}
defaultTermios = *term
term.Lflag &^= unix.ECHO
err = unix.IoctlSetTermios(fd, unix.TCSETS, term)
if err != nil {
return err
}
return nil
}
func restoreAndExit(code int) {
if defaultTermios != (unix.Termios{}) {
RestoreTermMode()
}
fmt.Print(cursor.SHOW_CURSOR, "\n\n")
os.Exit(code)
}