-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler.go
149 lines (118 loc) · 3.02 KB
/
eventhandler.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
package main
import (
"fmt"
"os"
"github.com/gdamore/tcell/v2"
)
func HandleEvents(screen *Screen, events chan string) {
tScreen := screen.tScreen
for {
switch ev := tScreen.PollEvent().(type) {
case *tcell.EventResize:
tScreen.Sync()
case *tcell.EventKey:
switch ev.Key() {
case tcell.KeyEscape:
tScreen.Fini()
os.Exit(0)
case tcell.KeyCtrlS:
// save the file
HandleSave(screen)
case tcell.KeyUp, tcell.KeyDown, tcell.KeyLeft, tcell.KeyRight:
HandleDirection(screen, ev.Key())
case tcell.KeyEnter:
HandleReturn(screen)
events <- "XD"
case tcell.KeyTab:
// HandleTab(screen)
// events <- "tab"
HandleTestingInsert(screen, "This is a test", events)
case tcell.KeyRune:
ch := ev.Rune()
lines := screen.tabBuffer.GetValidLines()
screen.WriteDebug(fmt.Sprintf("Buffer before insert: %s ", lines[0].GetText()), 3)
HandleInsertRune(screen, ch)
events <- "XD"
screen.WriteDebug(fmt.Sprintf("Buffer after insert: %s ", lines[0].GetText()), 4)
case tcell.KeyBackspace, tcell.KeyBackspace2:
HandleBackspace(screen)
events <- "XD"
}
}
}
}
func HandleSave(screen *Screen) {
tb := screen.tabBuffer
tb.file.Save(tb)
screen.WriteDebug("File saved", 4)
}
func HandleTestingInsert(screen *Screen, s string, events chan string) {
tb := screen.tabBuffer
cursor := tb.cursor
line := tb.GetLine(cursor.y)
for _, r := range s {
line.Insert(r)
}
events <- "XD"
}
func HandleInsertRune(screen *Screen, r rune) {
// figure out where the cursor is
// insert the rune at the cursor
tb := screen.tabBuffer
cursor := tb.cursor
line := tb.GetLine(cursor.y)
line.Insert(r)
}
func HandleBackspace(screen *Screen) {
tb := screen.tabBuffer
cursor := tb.cursor
line := tb.GetLine(cursor.y)
line.Delete()
}
func HandleTab(screen *Screen) {
// TODO: implement tabbing
tb := screen.tabBuffer
cursor := tb.cursor
line := tb.GetLine(cursor.y)
line.Insert('\t')
}
func HandleReturn(screen *Screen) {
tb := screen.tabBuffer
cursor := tb.cursor
// line := tb.GetLine(cursor.y)
// line.Insert('\n')
tb.AddLine("", cursor.y, cursor.x)
}
func HandleDirection(screen *Screen, key tcell.Key) {
tb := screen.tabBuffer
cursor := tb.cursor
switch key {
case tcell.KeyUp:
upperBound := tb.bounds[0][1]
if cursor.y > upperBound {
prevLine := tb.GetLine(cursor.y - 1)
if cursor.x > prevLine.Len()-1 {
cursor.SetPos(prevLine.Len(), cursor.y-1, tb)
} else {
cursor.SetPos(cursor.x, cursor.y-1, tb)
}
}
// TODO: noticed when i open a file with more than 20 lines, i cannot scroll down below line 20
case tcell.KeyDown:
if cursor.y < tb.Len() {
nextLine := tb.GetLine(cursor.y + 1)
if cursor.x > nextLine.Len()-1 {
cursor.SetPos(nextLine.Len(), cursor.y+1, tb)
} else {
cursor.SetPos(cursor.x, cursor.y+1, tb)
}
}
case tcell.KeyLeft:
cursor.SetPos(cursor.x-1, cursor.y, tb)
case tcell.KeyRight:
line := tb.GetLine(cursor.y)
if cursor.x <= line.Len()-1 {
cursor.SetPos(cursor.x+1, cursor.y, tb)
}
}
}