-
Notifications
You must be signed in to change notification settings - Fork 0
/
dispatcher.go
157 lines (134 loc) · 3.37 KB
/
dispatcher.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cmdparser
import (
"fmt"
"github.com/danielgatis/go-vte/vtparser"
"io"
"log"
)
var _ CMDParser = &dispatcherImpl{}
type dispatcherImpl struct {
cmd []rune // command buffer
cur int // cursor position
parser *vtparser.Parser
logger *log.Logger
}
func NewDispatcher() CMDParser {
dispatcher := &dispatcherImpl{cmd: []rune{}, logger: log.New(io.Discard, "", log.LstdFlags)}
parser := vtparser.New(
dispatcher.print,
dispatcher.execute,
dispatcher.put,
dispatcher.unhook,
dispatcher.hook,
dispatcher.oscDispatch,
dispatcher.csiDispatch,
dispatcher.escDispatch,
)
dispatcher.parser = parser
return dispatcher
}
func NewDispatcherWithLogger(logger *log.Logger) *dispatcherImpl {
dispatcher := &dispatcherImpl{cmd: []rune{}, logger: logger}
parser := vtparser.New(
dispatcher.print,
dispatcher.execute,
dispatcher.put,
dispatcher.unhook,
dispatcher.hook,
dispatcher.oscDispatch,
dispatcher.csiDispatch,
dispatcher.escDispatch,
)
dispatcher.parser = parser
return dispatcher
}
func (d *dispatcherImpl) Write(r io.Reader) error {
defer func() {
if err := recover(); err != nil {
d.logger.Printf("[Write] panic: %v\n", err)
}
}()
buff := make([]byte, 2048)
for {
n, err := r.Read(buff)
if err != nil {
if err == io.EOF {
d.logger.Printf("[Write] EOF\n")
return nil
}
d.logger.Printf("[Write] error: %v\n", err)
return err
}
for _, b := range buff[:n] {
d.parser.Advance(b)
}
}
}
func (d *dispatcherImpl) Flush() string {
d.logger.Printf("[Flush] %s\n", string(d.cmd))
str := string(d.cmd)
d.cmd = []rune{}
d.cur = 0
return str
}
func (d *dispatcherImpl) Parse(r io.Reader) string {
err := d.Write(r)
if err != nil {
return ""
}
return d.Flush()
}
func (d *dispatcherImpl) print(r rune) {
d.logger.Printf("[Print] %c\n", r)
if len(d.cmd) > d.cur {
d.cmd[d.cur] = r
} else {
d.cmd = append(d.cmd, r)
}
d.cur++
}
func (d *dispatcherImpl) execute(b byte) {
d.logger.Printf("[Execute] %02x\n", b)
if b == '\b' {
if d.cmd[d.cur-1] > 127 {
d.cmd[d.cur-1] = ' '
} else {
d.cur--
}
}
}
func (d *dispatcherImpl) put(b byte) {
fmt.Printf("[Put] %02x\n", b)
}
func (d *dispatcherImpl) unhook() {
d.logger.Printf("[Unhook]\n")
}
func (d *dispatcherImpl) hook(params []int64, intermediates []byte, ignore bool, r rune) {
d.logger.Printf("[Hook] params=%v, intermediates=%v, ignore=%v, r=%v\n", params, intermediates, ignore, r)
}
func (d *dispatcherImpl) oscDispatch(params [][]byte, bellTerminated bool) {
d.logger.Printf("[OscDispatch] params=%v, bellTerminated=%v\n", params, bellTerminated)
}
func (d *dispatcherImpl) csiDispatch(params []int64, intermediates []byte, ignore bool, r rune) {
d.logger.Printf("[CsiDispatch] params=%v, intermediates=%v, ignore=%v, r=%v\n", params, intermediates, ignore, r)
switch r {
case 64:
d.cmd = append(append(d.cmd[:d.cur], 0), d.cmd[d.cur:]...)
case 71:
d.cur = int(params[0]) - 1
d.cmd = d.cmd[:d.cur]
case 75:
if len(d.cmd) > d.cur {
d.cmd = append(d.cmd[:d.cur], d.cmd[d.cur+1:]...)
}
case 80:
if len(d.cmd) > d.cur {
d.cmd = append(d.cmd[:d.cur], d.cmd[d.cur+1:]...)
}
default:
d.logger.Printf("[CsiDispatch] unknown command: %v\n", r)
}
}
func (d *dispatcherImpl) escDispatch(intermediates []byte, ignore bool, b byte) {
d.logger.Printf("[EscDispatch] intermediates=%v, ignore=%v, byte=%02x\n", intermediates, ignore, b)
}