-
Notifications
You must be signed in to change notification settings - Fork 0
/
tcellbindings.go
125 lines (118 loc) · 2.47 KB
/
tcellbindings.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
package edit
import (
"log"
"github.com/gdamore/tcell/v2"
)
type TcellEventConverter struct {
lastButtonMask tcell.ButtonMask
pasting bool
pasteBuffer []rune
}
const wheelButtons = tcell.WheelDown | tcell.WheelUp | tcell.WheelLeft | tcell.WheelRight
func (c *TcellEventConverter) EventFromTcell(tcevt tcell.Event) Event {
log.Printf("tcell %#v", tcevt)
switch tevt := tcevt.(type) {
case *tcell.EventResize:
w, h := tevt.Size()
return Event{
EventType: Resize,
Size: Size{
W: w,
H: h,
},
}
case *tcell.EventMouse:
buttonMask := tevt.Buttons()
mx, my := tevt.Position()
lastButtonMask := c.lastButtonMask
c.lastButtonMask = buttonMask & ^wheelButtons
return Event{
EventType: Mouse,
MouseData: MouseData{
Position: Position{
X: mx,
Y: my,
},
Buttons: buttonsFromTcell(buttonMask),
ButtonsPressed: buttonsFromTcell(buttonMask &^ lastButtonMask),
ButtonsReleased: buttonsFromTcell(lastButtonMask &^ buttonMask),
},
Modifiers: modifiersFromTcell(tevt.Modifiers()),
}
case *tcell.EventKey:
if c.pasting {
c.pasteBuffer = append(c.pasteBuffer, tevt.Rune())
return Event{}
}
if tevt.Key() == tcell.KeyRune {
return Event{
EventType: Rune,
Rune: tevt.Rune(),
Modifiers: modifiersFromTcell(tevt.Modifiers()),
}
}
return Event{
EventType: Key,
Rune: tevt.Rune(),
KeyData: KeyData(tevt.Key()),
Modifiers: modifiersFromTcell(tevt.Modifiers()),
}
case *tcell.EventPaste:
if tevt.Start() {
c.pasting = true
c.pasteBuffer = c.pasteBuffer[:0]
return Event{}
} else {
c.pasting = false
buf := string(c.pasteBuffer)
c.pasteBuffer = c.pasteBuffer[:0]
return Event{
EventType: Paste,
PasteString: buf,
}
}
default:
return Event{}
}
}
func modifiersFromTcell(mod tcell.ModMask) Modifiers {
var m Modifiers
if mod&tcell.ModAlt != 0 {
m |= Alt
}
if mod&tcell.ModShift != 0 {
m |= Shift
}
if mod&tcell.ModCtrl != 0 {
m |= Control
}
if mod&tcell.ModMeta != 0 {
m |= Meta
}
return m
}
func buttonsFromTcell(btn tcell.ButtonMask) MouseButtons {
var b MouseButtons
if btn&tcell.Button1 != 0 {
b |= Button1
}
if btn&tcell.Button2 != 0 {
b |= Button2
}
if btn&tcell.Button3 != 0 {
b |= Button3
}
if btn&tcell.WheelDown != 0 {
b |= WheelDown
}
if btn&tcell.WheelUp != 0 {
b |= WheelUp
}
if btn&tcell.WheelLeft != 0 {
b |= WheelLeft
}
if btn&tcell.WheelRight != 0 {
b |= WheelRight
}
return b
}