/
cursor.go
198 lines (161 loc) · 4.05 KB
/
cursor.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
package replay
import (
"github.com/cfoust/cy/pkg/emu"
"github.com/cfoust/cy/pkg/geom"
"github.com/mattn/go-runewidth"
)
// Get the occupancy state of the given line.
func getOccupancy(line emu.Line) []bool {
occupancy := make([]bool, len(line))
for i := 0; i < len(line); i++ {
if line[i].IsEmpty() {
continue
}
// handle wide runes
r := line[i].Char
w := runewidth.RuneWidth(r)
for j := 0; j < w; j++ {
occupancy[i+j] = true
}
i += geom.Max(w-1, 0)
}
return occupancy
}
func isLineEmpty(line emu.Line) bool {
occupancy := getOccupancy(line)
for _, occupied := range occupancy {
if occupied {
return false
}
}
return true
}
// Get the indices of the first and last non-empty cells for the given line.
func getNonWhitespace(line emu.Line) (first, last int) {
for i := 0; i < len(line); i++ {
if line[i].IsEmpty() {
continue
}
first = i
break
}
for i := len(line) - 1; i >= 0; i-- {
if line[i].IsEmpty() {
continue
}
last = i
break
}
return
}
// Given a point in term space representing a desired cursor position, return
// the best available cursor position. This enables behavior akin to moving up
// and down in a text editor.
func (r *Replay) resolveDesiredColumn(point geom.Vec2) int {
line := r.getLine(point.R)
if line == nil {
return 0
}
occupancy := getOccupancy(line)
// desiredCol occupied -> return that col
if occupancy[point.C] {
return point.C
}
var haveBefore, haveAfter bool
// check for occupied cells before and after the desired column
for i := 0; i < len(line); i++ {
if i == point.C || !occupancy[i] {
continue
}
if i > point.C {
haveAfter = true
} else {
haveBefore = true
}
}
// the line is empty, just go to col 0
if !haveBefore && !haveAfter {
return 0
}
// point.C is before last non-whitespace and after first
// non-whitespace: remain in place
if haveBefore && haveAfter {
return point.C
}
// first non-whitespace is after point.C: last column before first
// non-whitespace
if haveAfter && !haveBefore {
first, _ := getNonWhitespace(line)
return geom.Max(first-1, 0)
}
// last non-whitespace is before point.C: last non-whitespace column
if haveBefore && !haveAfter {
_, last := getNonWhitespace(line)
return last
}
return 0
}
// Move the cursor to a point in term space, adjusting the viewport the minimum
// amount necessary to keep the cursor in view.
func (r *Replay) moveCursor(point geom.Vec2) {
viewport := r.viewport
newCursor := r.termToViewport(point)
if newCursor.C < 0 {
r.setOffsetX(r.offset.C + newCursor.C)
}
if newCursor.C >= viewport.C {
r.setOffsetX(r.offset.C + (newCursor.C - viewport.C + 1))
}
if newCursor.R < 0 {
r.setOffsetY(r.offset.R + newCursor.R)
}
if newCursor.R >= viewport.R {
r.setOffsetY(r.offset.R + (newCursor.R - viewport.R + 1))
}
r.cursor = r.termToViewport(point)
}
// Attempt to move the cursor relative to its current position. Sets
// `desiredCol` if the motion is horizontal.
func (r *Replay) moveCursorDelta(dy, dx int) {
oldPos := r.viewportToTerm(r.cursor)
newPos := r.clampToTerminal(oldPos.Add(geom.Vec2{
R: dy,
C: dx,
}))
// Don't allow user to move onto blank lines at end of terminal
numBlank := 0
screen := r.terminal.Screen()
termSize := r.getTerminalSize()
for row := termSize.R - 1; row >= 0; row-- {
if !isLineEmpty(screen[row]) {
break
}
numBlank++
}
newPos.R = geom.Min(termSize.R-1-numBlank, newPos.R)
// Don't do anything if we can't move
if newPos == oldPos {
return
}
// Motion to the right is bounded to the last non-whitespace character
if newPos.C > oldPos.C {
line := r.getLine(newPos.R)
if line == nil {
return
}
_, lastCell := getNonWhitespace(line)
newPos.C = geom.Min(lastCell, newPos.C)
}
if newPos.R != oldPos.R && newPos.C == oldPos.C {
newPos.C = r.resolveDesiredColumn(geom.Vec2{
R: newPos.R,
C: r.desiredCol,
})
}
r.mode = ModeCopy
r.moveCursor(newPos)
// If the column motion was intentional, set the desiredCol
if newPos.C != oldPos.C && dx != 0 {
r.desiredCol = r.termToViewport(newPos).C
}
}