-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
124 lines (105 loc) · 2.34 KB
/
event.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
package startprompt
type Event interface {
Type() EventType
}
// EventKey 代表键盘事件
type EventKey struct {
cli *CommandLine
tcli *TCommandLine
data []rune
eventType EventType
}
//goland:noinspection GoUnusedExportedFunction
func NewEventKey(eventType EventType, data []rune, cli *CommandLine, tcli *TCommandLine) *EventKey {
return &EventKey{
eventType: eventType,
data: data,
cli: cli,
tcli: tcli,
}
}
func (ek *EventKey) Type() EventType {
return ek.eventType
}
func (ek *EventKey) GetData() []rune {
return ek.data
}
func (ek *EventKey) appendData(data []rune) {
ek.data = append(ek.data, data...)
}
func (ek *EventKey) GetCommandLine() *CommandLine {
if ek.cli == nil {
panic("not found CommandLine from EventKey")
}
return ek.cli
}
func (ek *EventKey) GetTCommandLine() *TCommandLine {
if ek.tcli == nil {
panic("not found TCommandLine from EventKey")
}
return ek.tcli
}
// EventMouse 代表鼠标事件
type EventMouse struct {
cli *CommandLine
tcli *TCommandLine
coordinate Coordinate
eventType EventType
}
func NewEventMouse(
eventType EventType,
coordinate Coordinate,
cli *CommandLine,
tcli *TCommandLine,
) *EventMouse {
return &EventMouse{
eventType: eventType,
coordinate: coordinate,
cli: cli,
tcli: tcli,
}
}
func (em *EventMouse) Type() EventType {
return em.eventType
}
func (em *EventMouse) GetCommandLine() *CommandLine {
if em.cli == nil {
panic("not found CommandLine from EventMouse")
}
return em.cli
}
func (em *EventMouse) GetTCommandLine() *TCommandLine {
if em.tcli == nil {
panic("not found TCommandLine from EventMouse")
}
return em.tcli
}
func (em *EventMouse) GetCoordinate() Coordinate {
return em.coordinate
}
type EventBuffer struct {
buffer []Event
}
func newEventBuffer() *EventBuffer {
return &EventBuffer{}
}
func (ebuf *EventBuffer) append(event Event) {
length := len(ebuf.buffer)
if length > 0 {
// 尝试合并事件
last := ebuf.buffer[length-1]
if last.Type() == event.Type() && event.Type() == EventTypeInsertChar {
lastk := last.(*EventKey)
eventk := event.(*EventKey)
lastk.appendData(eventk.GetData())
return
}
}
ebuf.buffer = append(ebuf.buffer, event)
}
func (ebuf *EventBuffer) getAll() []Event {
return ebuf.buffer
}
func (ebuf *EventBuffer) reset() {
ebuf.buffer = nil
}