-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscroll.go
241 lines (223 loc) · 6.54 KB
/
scroll.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package gmir
import (
"math"
"github.com/codesoap/gmir/parser"
"github.com/gdamore/tcell/v2"
)
// Scroll scrolls up or down the given amount of (wrapped) lines. Scrolls
// up, if lines is negative. Never scrolls past the top or bottom line.
func (v *View) Scroll(screen tcell.Screen, lines int) {
// TODO: Optimize, so that the same line is not wrapped multiple times.
if lines == 0 {
return
}
for lines > 0 && (v.line > 0 || v.lineOffset > 0) {
if v.lineOffset > 0 {
v.lineOffset--
} else {
v.line--
v.lineOffset = v.maxLineOffset(screen, v.line)
}
lines--
}
for lines < 0 &&
(v.line < len(v.lines)-1 || v.lineOffset < v.maxLineOffset(screen, v.line)) {
if v.lineOffset == v.maxLineOffset(screen, v.line) {
v.line++
v.lineOffset = 0
} else {
v.lineOffset++
}
lines++
}
// TODO: Maybe ensure the last line will not scroll over the bottom of screen.
}
// ScrollToTop scrolls to the first line.
func (v *View) ScrollToTop(screen tcell.Screen) {
v.line = 0
v.lineOffset = 0
}
// ScrollToBottom scrolls to the last line.
func (v *View) ScrollToBottom(screen tcell.Screen) {
v.line = len(v.lines) - 1
v.lineOffset = v.maxLineOffset(screen, v.line)
}
// ScrollToNextHeading scrolls to the first line of the next heading.
func (v *View) ScrollToNextHeading(screen tcell.Screen) {
if v.line == len(v.lines)-1 {
return
}
for i, line := range v.lines[v.line+1:] {
if isHeading(line) {
v.line += i + 1
v.lineOffset = 0
return
}
}
}
// ScrollToPrevHeading scrolls to the first line of the previous heading.
func (v *View) ScrollToPrevHeading(screen tcell.Screen) {
if v.line == 0 {
return
}
for i := v.line - 1; i >= 0; i-- {
if isHeading(v.lines[i]) {
v.line = i
v.lineOffset = 0
return
}
}
}
// ScrollToNthHeading scrolls to the first line of the nth heading.
func (v *View) ScrollToNthHeading(screen tcell.Screen, n int) {
for i, line := range v.lines {
if isHeading(line) {
n--
if n < 0 {
v.line = i
v.lineOffset = 0
return
}
}
}
}
func isHeading(line parser.Line) bool {
_, isHeading1 := line.(parser.Heading1Line)
_, isHeading2 := line.(parser.Heading2Line)
_, isHeading3 := line.(parser.Heading3Line)
return isHeading1 || isHeading2 || isHeading3
}
// ScrollToNextParagraph scrolls to the first line of the next pargraph.
func (v *View) ScrollToNextParagraph(screen tcell.Screen) {
for i := v.line + 1; i < len(v.lines)-2; i++ {
if v.lines[i].Text() == "" && v.lines[i+1].Text() != "" {
v.line = i + 1
v.lineOffset = 0
return
}
}
}
// ScrollToPrevParagraph scrolls to the first line of the current
// paragraph, if not already there. Otherwise, it scrolls to the first
// line of the previous paragraph.
func (v *View) ScrollToPrevParagraph(screen tcell.Screen) {
if v.lineOffset > 0 {
v.lineOffset = 0
return
}
for i := v.line - 1; i >= 0; i-- {
if v.lines[i].Text() != "" && (i == 0 || v.lines[i-1].Text() == "") {
v.line = i
return
}
}
}
// ScrollDownToSearchMatch scrolls to the next line, that matches
// v.Searchpattern. If the current line matches v.Searchpattern, nothing
// is done.
//
// Returns false, if neither the current line nor any line after matches
// v.Searchpattern.
func (v *View) ScrollDownToSearchMatch(screen tcell.Screen) bool {
return v.scrollDownToSearchMatch(screen, false)
}
// ScrollDownToSearchMatch scrolls to the next line, that matches
// v.Searchpattern.
//
// Returns false, if none of the lines after the current one matches
// v.Searchpattern.
func (v *View) ScrollDownToNextSearchMatch(screen tcell.Screen) bool {
return v.scrollDownToSearchMatch(screen, true)
}
// ScrollUpToSearchMatch scrolls to the previous line, that matches
// v.Searchpattern. If the current line matches v.Searchpattern, nothing
// is done.
//
// Returns false, if neither the current line nor any line after matches
// v.Searchpattern.
func (v *View) ScrollUpToSearchMatch(screen tcell.Screen) bool {
return v.scrollUpToSearchMatch(screen, false)
}
// ScrollUpToSearchMatch scrolls to the previous line, that matches
// v.Searchpattern.
//
// Returns false, if none of the lines after the current one matches
// v.Searchpattern.
func (v *View) ScrollUpToNextSearchMatch(screen tcell.Screen) bool {
return v.scrollUpToSearchMatch(screen, true)
}
func (v *View) scrollDownToSearchMatch(screen tcell.Screen, skipFirst bool) bool {
if v.Searchpattern == nil {
return false
}
screenWidth, _ := screen.Size()
_, _, textWidth := v.columnWidths(screenWidth)
for i, line := range v.lines[v.line:] {
matches := v.Searchpattern.FindAllStringIndex(line.Text(), -1)
if len(matches) == 0 {
continue
}
if wrappable, isWrappable := line.(parser.WrappableLine); isWrappable {
wrapIndexes := wrappable.WrapIndexes(textWidth)
for _, offset := range lineOffsetsWithMatches(wrapIndexes, matches) {
if i > 0 || (skipFirst && offset > v.lineOffset) || (!skipFirst && offset >= v.lineOffset) {
v.line += i
v.lineOffset = offset
return true
}
}
} else if (skipFirst && i > 0) || (!skipFirst && i >= 0) {
v.line += i
v.lineOffset = 0
return true
}
}
return false
}
func (v *View) scrollUpToSearchMatch(screen tcell.Screen, skipFirst bool) bool {
if v.Searchpattern == nil {
return false
}
screenWidth, _ := screen.Size()
_, _, textWidth := v.columnWidths(screenWidth)
for lineIndex := v.line; lineIndex >= 0; lineIndex-- {
line := v.lines[lineIndex]
matches := v.Searchpattern.FindAllStringIndex(line.Text(), -1)
if len(matches) == 0 {
continue
}
if wrappable, isWrappable := line.(parser.WrappableLine); isWrappable {
wrapIndexes := wrappable.WrapIndexes(textWidth)
matchingLineOffsets := lineOffsetsWithMatches(wrapIndexes, matches)
for i := len(matchingLineOffsets) - 1; i >= 0; i-- {
offset := matchingLineOffsets[i]
if lineIndex < v.line || (skipFirst && offset < v.lineOffset) || (!skipFirst && offset <= v.lineOffset) {
v.line = lineIndex
v.lineOffset = offset
return true
}
}
} else if (skipFirst && lineIndex < v.line) || (!skipFirst && lineIndex <= v.line) {
v.line = lineIndex
v.lineOffset = 0
return true
}
}
return false
}
func lineOffsetsWithMatches(wrapIndexes []int, matches [][]int) []int {
matchStops := append(wrapIndexes, math.MaxInt)
offsets := make([]int, 0)
offset := 0
for offset < len(matchStops) {
for _, match := range matches {
if match[0] < matchStops[offset] &&
(offset == 0 || match[0] >= matchStops[offset-1]) &&
(len(offsets) == 0 || offsets[len(offsets)-1] != offset) {
offsets = append(offsets, offset)
}
}
offset++
}
return offsets
}