forked from u-root/u-root
-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup_unix.go
60 lines (46 loc) · 1.29 KB
/
setup_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
// +build !windows,!plan9
package tty
import (
"fmt"
"os"
"github.com/u-root/u-root/cmds/elvish/sys"
"github.com/u-root/u-root/cmds/elvish/util"
)
const flushInputDuringSetup = false
func setup(in, out *os.File) (func() error, error) {
// On Unix, use input file for changing termios. All fds pointing to the
// same terminal are equivalent.
fd := int(in.Fd())
term, err := sys.NewTermiosFromFd(fd)
if err != nil {
return nil, fmt.Errorf("can't get terminal attribute: %s", err)
}
savedTermios := term.Copy()
term.SetICanon(false)
term.SetEcho(false)
term.SetVMin(1)
term.SetVTime(0)
// Enforcing crnl translation on readline. Assuming user won't set
// inlcr or -onlcr, otherwise we have to hardcode all of them here.
term.SetICRNL(true)
err = term.ApplyToFd(fd)
if err != nil {
return nil, fmt.Errorf("can't set up terminal attribute: %s", err)
}
var errFlushInput error
if flushInputDuringSetup {
err = sys.FlushInput(fd)
if err != nil {
errFlushInput = fmt.Errorf("can't flush input: %s", err)
}
}
var errSetupVT error
err = setupVT(out)
if err != nil {
errSetupVT = fmt.Errorf("can't setup VT: %s", err)
}
restore := func() error {
return util.Errors(savedTermios.ApplyToFd(fd), restoreVT(out))
}
return restore, util.Errors(errFlushInput, errSetupVT)
}