-
Notifications
You must be signed in to change notification settings - Fork 17
/
pagermode-jump-to-mark.go
84 lines (67 loc) · 1.64 KB
/
pagermode-jump-to-mark.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
package m
import (
"sort"
"github.com/walles/moar/twin"
"golang.org/x/exp/maps"
)
type PagerModeJumpToMark struct {
pager *Pager
}
func (m PagerModeJumpToMark) drawFooter(_ string, _ string) {
p := m.pager
_, height := p.screen.Size()
pos := 0
for _, token := range m.getMarkPrompt() {
p.screen.SetCell(pos, height-1, twin.NewCell(token, twin.StyleDefault))
pos++
}
}
func (m PagerModeJumpToMark) getMarkPrompt() string {
// Special case having zero, one or multiple marks
if len(m.pager.marks) == 0 {
return "No marks set, press 'm' to set one!"
}
if len(m.pager.marks) == 1 {
for key := range m.pager.marks {
return "Jump to your mark: " + string(key)
}
}
// Multiple marks, list them
marks := maps.Keys(m.pager.marks)
sort.Slice(marks, func(i, j int) bool {
return marks[i] < marks[j]
})
prompt := "Jump to one of these marks: "
for i, mark := range marks {
if i > 0 {
prompt += ", "
}
prompt += string(mark)
}
return prompt
}
func (m PagerModeJumpToMark) onKey(key twin.KeyCode) {
p := m.pager
switch key {
case twin.KeyEnter, twin.KeyEscape:
// Never mind I
p.mode = PagerModeViewing{pager: p}
default:
// Never mind II
p.mode = PagerModeViewing{pager: p}
p.mode.onKey(key)
}
}
func (m PagerModeJumpToMark) onRune(char rune) {
if len(m.pager.marks) == 0 && char == 'm' {
//nolint:gosimple // The linter's advice is just wrong here
m.pager.mode = PagerModeMark{pager: m.pager}
return
}
destination, ok := m.pager.marks[char]
if ok {
m.pager.scrollPosition = destination
}
//nolint:gosimple // The linter's advice is just wrong here
m.pager.mode = PagerModeViewing{pager: m.pager}
}