-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathentry.go
113 lines (92 loc) · 2.03 KB
/
entry.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
package dotui
import (
"github.com/kreativka/dot-ui/desktop"
)
type ents struct {
names []*desktop.Entry
list [][]string
filter string
curr, iter, limit, start int
}
func (e *ents) Next() bool {
e.iter++
if e.iter < e.limit && e.iter < len(e.names) {
return true
}
return false
}
func (e *ents) Value() string {
return e.names[e.currIndex()].Name
}
func (e *ents) Reset() {
e.iter = -1
}
func (e ents) currIndex() int {
return e.iter + e.start
}
func (e *ents) IsCurrentHighlighted() bool {
return e.curr == e.currIndex()
}
func (e ents) IsCurrentEven() bool {
return e.iter%2 == 0
}
func (e ents) CurrentSelection() *desktop.Entry {
return e.names[e.curr]
}
func (e ents) Len() int {
return len(e.names)
}
func (e *ents) CursorDown() bool {
if e.curr < len(e.names)-1 {
e.curr++
if e.curr >= e.limit && e.curr < len(e.names) &&
e.start+e.limit+1 <= len(e.names) {
e.start++
}
return true
}
return false
}
func (e *ents) CursorUp() bool {
if e.curr > 0 {
if e.curr == e.start && e.start > 0 {
e.start--
}
e.curr--
return true
}
return false
}
func (e *ents) handleResize(height, entryHeight int) {
limit := height / entryHeight
// Check if we should hide last non-fully visible entry.
// 0.8 on scale 0 - 1 how much of entry is visible
// 0.8 makes it almost fully visible
offset := float32(height) / float32(entryHeight)
if offset-float32(limit) < 0.8 {
limit--
}
switch {
case limit == e.limit:
return
case limit < e.limit:
// When shrinking hits cursor position, keep it on the last visible
// entry.
if e.curr >= e.start+limit {
if e.start+limit < len(e.names)-1 && e.start+limit > 0 {
e.curr = limit + e.start - 1
}
}
case limit > e.limit:
// When growing continue showing next entries, when at the end: start
// showing previous entries.
if e.start > 0 && e.start+limit >= len(e.names) {
for ; e.start > 0; e.start-- {
if e.start+limit <= len(e.names) {
break
}
}
}
}
e.limit = limit
}