/
console_raw_syscall.go
59 lines (49 loc) · 1.1 KB
/
console_raw_syscall.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
package console
import (
"github.com/Jamesits/serial/pkg/raw_term"
"github.com/jamesits/libiferr/panicked"
log "github.com/sirupsen/logrus"
"os"
)
func ConsoleRawSyscall() (in chan<- []byte, out <-chan []byte, err error) {
var inChan = make(chan []byte)
var outChan = make(chan []byte)
log.Traceln("term_raw_syscall setup")
err, _ = panicked.CatchError(func() {
raw_term.SetRaw()
})
if err != nil {
return
}
go func() {
log.Traceln("write process start")
defer panicked.Catch(func() {
close(inChan)
})
for buffer := range inChan {
n, err := os.Stdout.Write(buffer)
log.WithError(err).Tracef("term write: %d bytes", n)
if err != nil {
break
}
}
log.Traceln("write process ended")
}()
go func() {
log.Traceln("read process start")
defer raw_term.Restore()
defer panicked.Catch(func() {
close(outChan)
})
for true {
buffer, err := raw_term.ReadByteSync()
log.WithError(err).Tracef("term read: %d bytes", len(buffer))
if err != nil {
break
}
outChan <- buffer
}
log.Traceln("read process ended")
}()
return inChan, outChan, nil
}