-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathkeyboard.go
99 lines (89 loc) · 2.43 KB
/
keyboard.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
package main
import (
"github.com/gdamore/tcell"
)
func (c *Display) getBlinkerX() int {
blinkerX, _ := c.getCurrentEl().getRelativeBlinkerCoordsByPos()
return blinkerX
}
func (c *Display) getBlinkerY() int {
_, blinkerY := c.getCurrentEl().getRelativeBlinkerCoordsByPos()
return blinkerY + c.getCurrentEl().startingCoordY - c.offsetY
}
func (c *Display) handleKeyPress(op typeOperation) {
switch op.key {
case tcell.KeyLeft:
{
oldCursorY := c.getCurrentEl().getRelativeCursorY()
c.getCurrentEl().moveLeft()
newCursorY := c.getCurrentEl().getRelativeCursorY()
if oldCursorY != newCursorY {
c.resyncNewCursorY()
}
}
case tcell.KeyRight:
{
oldCursorY := c.getCurrentEl().getRelativeCursorY()
c.getCurrentEl().moveRight()
newCursorY := c.getCurrentEl().getRelativeCursorY()
if oldCursorY != newCursorY {
c.resyncNewCursorY()
}
}
case tcell.KeyUp:
{
if c.hasPrevEl() {
pos := c.getCurrentEl().pos
c.currentElement = c.currentElement.Prev()
c.getCurrentEl().pos = minOf(len(c.getCurrentEl().data), pos)
if c.getCurrentEl().getOnScreenLineStartingY() < 0 {
c.offsetY = c.getCurrentEl().startingCoordY
c.resyncBelow(c.data.Front())
}
}
}
case tcell.KeyDown:
{
if c.hasNextEl() {
pos := c.getCurrentEl().pos
c.currentElement = c.currentElement.Next()
c.getCurrentEl().pos = minOf(len(c.getCurrentEl().data), pos)
if c.getCurrentEl().getOnScreenLineEndingY() >= c.getHeight() {
// Try to fit next line.
c.getCurrentEl().makeSmallestOffsetToFitLineOnDisplay()
c.resyncBelow(c.data.Front())
}
}
}
case tcell.KeyEnter:
{
cur := c.getCurrentEl()
// Create new line
newData := make([]rune, len(cur.data)-cur.pos)
copy(newData, cur.data[cur.pos:])
newItem := Line{data: newData, startingCoordY: cur.startingCoordY + cur.getRelativeCharBeforeCursorY() + 1, pos: 0, display: c}
c.data.InsertAfter(&newItem, c.currentElement)
cur.data = cur.data[:cur.pos] // we can optimize memory here, by duplicating it.
c.currentElement = c.currentElement.Next()
if c.getCurrentEl().getOnScreenLineEndingY() >= c.getHeight() {
// Try to fit next line.
c.getCurrentEl().makeSmallestOffsetToFitLineOnDisplay()
c.resyncBelow(c.data.Front())
} else {
c.resyncBelow(c.currentElement.Prev())
}
}
case tcell.KeyDEL:
{
c.remove()
}
case tcell.KeyCtrlF:
{
c.dump()
}
default:
{
c.insert(op.rn)
}
}
}