-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
console_raw_experimental.go
93 lines (78 loc) · 2.11 KB
/
console_raw_experimental.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
package console
import (
"errors"
"github.com/containerd/console"
"github.com/jamesits/libiferr/panicked"
log "github.com/sirupsen/logrus"
"golang.org/x/crypto/ssh/terminal"
)
// https://stackoverflow.com/a/58237997
func ConsoleRawExperimental() (in chan<- []byte, out <-chan []byte, err error) {
log.Tracef("term_raw_experimental setup console")
defer func() {
ret := recover()
if ret != nil {
log.Warningf("set console raw mode failed: %s", err)
err = errors.New("unable to set console raw mode")
}
}()
current := console.Current()
if err = current.SetRaw(); err != nil {
log.WithError(err).Debugln("console set raw mode failed")
return
}
// disable echo
// TODO: write a terminal emulator and detect if echo need to be enabled
if err = current.DisableEcho(); err != nil {
log.WithError(err).Debugln("console disable echo failed")
return
}
term := terminal.NewTerminal(current, "")
term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
log.WithField("line", line).WithField("pos", pos).WithField("key", key).Traceln("term.AutoCompleteCallback")
return "", 0, false
}
var inChan = make(chan []byte)
var outChan = make(chan []byte)
go func() {
log.Traceln("write process start")
for buffer := range inChan {
n, err := term.Write(buffer)
log.WithError(err).Tracef("term write: %d bytes", n)
if err != nil {
break
}
}
panicked.Catch(func() {
close(inChan)
})
log.Traceln("write process ended")
}()
go func() {
log.Traceln("read process start")
defer func(current console.Console) {
err := current.Reset()
if err != nil {
log.WithError(err).Warningln("console reset failed")
}
}(current)
for true {
line, err := term.ReadLine()
log.WithError(err).Tracef("term read: %d bytes", len(line))
if err != nil {
break
}
outChan <- []byte(line)
outChan <- linebreak
}
panicked.Catch(func() {
close(outChan)
})
// FIXME: correctly signal inChan to close
panicked.Catch(func() {
close(inChan)
})
log.Traceln("read process ended")
}()
return inChan, outChan, nil
}