-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
stdin_raw.go
53 lines (44 loc) · 1.16 KB
/
stdin_raw.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
package console
import (
"github.com/Jamesits/serial/pkg/raw_term"
"github.com/jamesits/libiferr/panicked"
log "github.com/sirupsen/logrus"
)
// https://viewsourcecode.org/snaptoken/kilo/03.rawInputAndOutput.html
func StdinRaw() (out <-chan []byte, err error) {
log.Debugln("stdin_raw setup")
err, _ = panicked.CatchError(func() {
raw_term.SetRaw()
})
if err != nil {
return
}
var outChan = make(chan []byte)
go func() {
log.Debugln("read process start")
defer raw_term.Restore()
defer panicked.Catch(func() {
log.Debugln("closing read channel")
close(outChan)
})
for true {
buffer, err := raw_term.ReadByteSync()
log.WithError(err).WithField("len", len(buffer)).WithField("content", buffer).Tracef("term read")
if err != nil || len(buffer) != 1 {
break
}
// TODO: make a big keyboard state machine here for shortcut & escape key processing
if buffer[0] == CtrlKey('c') {
log.Traceln("^C detected")
break
}
outChan <- buffer
}
log.Debugln("read process ended")
}()
return outChan, nil
}
// CtrlKey makes a Ctrl-* combination from the input ASCII character.
func CtrlKey(key byte) byte {
return key & 0x1f
}