-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.go
250 lines (207 loc) · 6.53 KB
/
user.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
// Copyright 2022 wangqi. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package statesync
import (
"bytes"
"fmt"
"reflect"
"strings"
pb "github.com/ericwq/aprilsh/protobufs/user"
"github.com/ericwq/aprilsh/terminal"
"github.com/rivo/uniseg"
"google.golang.org/protobuf/proto"
)
type UserEventType uint8
const (
UserByteType UserEventType = iota
ResizeType
)
// UserByte instance is created by aprish client, when client got the user input.
// Resize instance is created by aprish client, when client change the window size.
type UserEvent struct {
theType UserEventType
userByte terminal.UserByte // Parser::UserByte
resize terminal.Resize // Parser::Resize
}
func NewUserEvent(userByte terminal.UserByte) (u UserEvent) {
u = UserEvent{}
u.theType = UserByteType
u.userByte = userByte
return u
}
func NewUserEventResize(resize terminal.Resize) (u UserEvent) {
u = UserEvent{}
u.theType = ResizeType
u.resize = resize
return u
}
// UserStream implements network.State[C any] interface
type UserStream struct {
actions []UserEvent
}
func (u *UserStream) String() string {
var output1 strings.Builder
var output2 strings.Builder
for _, v := range u.actions {
switch v.theType {
case UserByteType:
// output1.WriteRune(v.userByte.C)
output1.WriteString(string(v.userByte.Chs))
case ResizeType:
output2.WriteString(fmt.Sprintf("(%d,%d),", v.resize.Width, v.resize.Height))
}
}
return fmt.Sprintf("Keystroke:%q, Resize:%s, size=%d", output1.String(), output2.String(), len(u.actions))
}
func (u *UserStream) PushBack(x []rune) {
userStroke := terminal.UserByte{Chs: x}
u.actions = append(u.actions, NewUserEvent(userStroke))
}
func (u *UserStream) PushBackResize(width, height int) {
resize := terminal.Resize{Width: width, Height: height}
u.actions = append(u.actions, NewUserEventResize(resize))
}
func (u *UserStream) Empty() bool {
return len(u.actions) == 0
}
func (u *UserStream) Size() int {
return len(u.actions)
}
func (u *UserStream) GetAction(i int) terminal.ActOn {
if 0 <= i && i < len(u.actions) {
switch u.actions[i].theType {
case UserByteType:
return u.actions[i].userByte
case ResizeType:
return u.actions[i].resize
}
}
return nil
}
// implements network.State[C any] interface
// Subtract() the prefix UserStream from current UserStream
func (u *UserStream) Subtract(prefix *UserStream) {
// fmt.Printf("#Subtract %q %p from %q %p\n", prefix, prefix, u, u)
// if we are subtracting ourself from ourself, just clear the deque
if u.Equal(prefix) {
u.actions = make([]UserEvent, 0)
return
}
for i := range prefix.actions {
// fmt.Printf("#Subtract compare %q[0] vs %q[%d]\n", u.actions[0], prefix.actions[i], i)
if len(u.actions) > 0 && reflect.DeepEqual(u.actions[0], prefix.actions[i]) {
// fmt.Printf("#Subtract equal %d %q\n", i, prefix.actions[i])
u.actions = u.actions[1:]
} else {
// fmt.Printf("#Subtract save %d %q\n", i, prefix.actions[i])
break
}
}
}
// implements network.State[C any] interface
// DiffFrom() exclude the existing UserEvent and return the difference.
func (u *UserStream) DiffFrom(existing *UserStream) string {
// skip the existing part
pos := 0
for i := range existing.actions {
if len(u.actions[pos:]) > 0 && reflect.DeepEqual(u.actions[pos], existing.actions[i]) {
pos++
} else {
break
}
}
// create the UserMessage based on content in UserStream
um := pb.UserMessage{}
for _, ue := range u.actions[pos:] {
switch ue.theType {
case UserByteType:
idx := len(um.Instruction) - 1 // TODO the last one?
var buf bytes.Buffer
buf.WriteString(string(ue.userByte.Chs))
keys := buf.Bytes()
if len(um.Instruction) > 0 && um.Instruction[idx].Keystroke != nil {
// append Keys for Keystroke
um.Instruction[idx].Keystroke.Keys = append(um.Instruction[idx].Keystroke.Keys, keys...)
} else {
// create a new Instruction for Keystroke
um.Instruction = make([]*pb.Instruction, 0)
inst := pb.Instruction{
Keystroke: &pb.Keystroke{Keys: keys},
}
um.Instruction = append(um.Instruction, &inst)
}
case ResizeType:
// create a new Instruction for ResizeMessage
inst := pb.Instruction{
Resize: &pb.ResizeMessage{Width: int32(ue.resize.Width), Height: int32(ue.resize.Height)},
}
um.Instruction = append(um.Instruction, &inst)
}
}
// get the wire-format encoding of UserMessage
output, _ := proto.Marshal(&um)
// if err != nil {
// panic(fmt.Sprintf("#DiffFrom marshal %s ", err))
// }
return string(output)
}
// implements network.State[C any] interface
func (u *UserStream) InitDiff() string {
return ""
}
// implements network.State[C any] interface
// convert the UserMessage into a UserStream
func (u *UserStream) ApplyString(diff string) error {
// parse the wire-format encoding of UserMessage
input := pb.UserMessage{}
err := proto.Unmarshal([]byte(diff), &input)
if err != nil {
return err
}
// create the UserStream based on content of UserMessage
for i := range input.Instruction {
if input.Instruction[i].Keystroke != nil {
graphemes := uniseg.NewGraphemes(string(input.Instruction[i].Keystroke.Keys))
for graphemes.Next() {
chs := graphemes.Runes()
u.actions = append(u.actions, NewUserEvent(terminal.UserByte{Chs: chs}))
}
} else if input.Instruction[i].Resize != nil {
w := input.Instruction[i].Resize
u.actions = append(u.actions, NewUserEventResize(terminal.Resize{Width: int(w.Width), Height: int(w.Height)}))
}
}
return nil
}
// implements network.State[C any] interface
func (u *UserStream) Equal(x *UserStream) bool {
if u == x || (len(u.actions) == 0 && len(x.actions) == 0) {
return true
}
return reflect.DeepEqual(u.actions, x.actions)
}
// implements network.State[C any] interface
func (u *UserStream) ResetInput() {}
func (u *UserStream) Reset() {}
func (u *UserStream) InitSize(nCols, nRows int) {}
// implements network.State[C any] interface
func (u *UserStream) Clone() *UserStream {
clone := UserStream{}
clone.actions = make([]UserEvent, len(u.actions))
for i := range u.actions { // actions slice
if u.actions[i].theType == UserByteType { // clone UserByte
chs := make([]rune, len(u.actions[i].userByte.Chs))
copy(chs, u.actions[i].userByte.Chs)
clone.actions[i].userByte = terminal.UserByte{}
clone.actions[i].userByte.Chs = chs
} else {
clone.actions[i] = u.actions[i] // clone Resize
}
}
return &clone
}
// for test purpose
func (u *UserStream) EqualTrace(x *UserStream) bool {
return u.Equal(x)
}