-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathresultsview.go
138 lines (109 loc) · 2.6 KB
/
resultsview.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
package main
import (
"errors"
"github.com/nsf/termbox-go"
)
var ScrollOff = 5
type ResultsView struct {
// Array of results to be filtered
// initialset ResultArray
results ResultArray
// Current user input
lastuserinput string
// Visible result lines
top_result int
bottom_result int
// Total number of results
resultCount int
// Index of currently selected line
result_selected int
// View size
x, y, h, w int
}
func (r *ResultsView) SelectFirst() {
r.result_selected = 0
r.top_result = 0
if r.resultCount > r.h {
r.bottom_result = r.h
} else {
r.bottom_result = r.resultCount
}
}
func (r *ResultsView) SelectPrevious() {
if r.result_selected > 0 {
r.result_selected--
}
if r.top_result > 0 && r.result_selected < r.top_result+ScrollOff {
r.top_result--
r.bottom_result--
}
}
func (r *ResultsView) SelectNext() {
if r.result_selected < (r.resultCount - 1) {
r.result_selected++
if r.result_selected >= r.bottom_result-ScrollOff && r.bottom_result < r.resultCount {
r.top_result++
r.bottom_result++
}
}
}
func tbprint(x, y int, fg, bg termbox.Attribute, msg string) {
for _, c := range msg {
termbox.SetCell(x, y, c, fg, bg)
x++
}
}
func (r *ResultsView) Draw() {
tclear(r.x, r.y, r.w, r.h)
cy := r.y
for cnt, res := range r.results[r.top_result:r.bottom_result] {
is_selected := (cnt + r.top_result) == r.result_selected
res.Draw(r.x, cy, r.w, is_selected)
cy++
}
}
func (r *ResultsView) ToggleMark() {
if r.resultCount > 0 {
r.results[r.result_selected].marked = !r.results[r.result_selected].marked
r.SelectNext()
}
}
func (r *ResultsView) ToggleMarkAll() error {
if len(r.results) > 1000 {
return errors.New("too many files to mark! I WONT DO IT!")
}
for _, res := range r.results {
res.marked = !res.marked
}
return nil
}
func (r *ResultsView) SetSize(x, y, w, h int) {
r.x, r.y, r.w, r.h = x, y, w, h
r.top_result = 0
if r.resultCount > r.h {
r.bottom_result = r.h
} else {
r.bottom_result = r.resultCount
}
}
func (r *ResultsView) Update(results ResultArray) {
r.results = results
r.resultCount = len(results)
r.SetSize(r.x, r.y, r.w, r.h)
}
// If there isnt any marked, return the selection. Otherwise return the array of marked results.
func (rv *ResultsView) GetMarkedOrSelected() ResultArray {
selected := make(ResultArray, 0, 1)
for _, res := range rv.results {
if res.marked {
selected = append(selected, res)
}
}
if len(selected) > 0 {
return selected
}
if len(rv.results) > 0 && rv.result_selected < len(rv.results) {
selected = append(selected, rv.results[rv.result_selected])
}
return selected
}