-
Notifications
You must be signed in to change notification settings - Fork 0
/
ctx.go
252 lines (214 loc) · 4.4 KB
/
ctx.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
242
243
244
245
246
247
248
249
250
251
252
package peco
import (
"bufio"
"fmt"
"io"
"os"
"sort"
"sync"
)
type Selection []int
func (s Selection) Has(v int) bool {
for _, i := range []int(s) {
if i == v {
return true
}
}
return false
}
func (s *Selection) Add(v int) {
if s.Has(v) {
return
}
*s = Selection(append([]int(*s), v))
sort.Sort(s)
}
func (s *Selection) Remove(v int) {
a := []int(*s)
for k, i := range a {
if i == v {
tmp := a[:k]
tmp = append(tmp, a[k+1:]...)
*s = Selection(tmp)
return
}
}
}
func (s *Selection) Clear() {
*s = Selection([]int{})
}
func (s Selection) Len() int {
return len(s)
}
func (s Selection) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func (s Selection) Less(i, j int) bool {
return s[i] < s[j]
}
// Ctx contains all the important data. while you can easily access
// data in this struct from anwyehre, only do so via channels
type Ctx struct {
enableSep bool
statusMessage string
result []Match
loopCh chan struct{}
queryCh chan string
drawCh chan []Match
pagingCh chan PagingRequest
mutex sync.Mutex
query []rune
caretPos int
currentLine int
selection Selection
lines []Match
current []Match
config *Config
Matchers []Matcher
CurrentMatcher int
ExitStatus int
wait *sync.WaitGroup
}
func NewCtx(enableSep bool) *Ctx {
return &Ctx{
enableSep,
"",
[]Match{},
make(chan struct{}), // loopCh. You never send messages to this. no point in buffering
make(chan string, 5), // queryCh.
make(chan []Match, 5), // drawCh.
make(chan PagingRequest, 5), // pagingCh
sync.Mutex{},
[]rune{},
0,
1,
Selection([]int{}),
[]Match{},
nil,
NewConfig(),
[]Matcher{
NewIgnoreCaseMatcher(enableSep),
NewCaseSensitiveMatcher(enableSep),
NewRegexpMatcher(enableSep),
},
0,
0,
&sync.WaitGroup{},
}
}
func (c *Ctx) ReadConfig(file string) error {
err := c.config.ReadFilename(file)
if err == nil {
c.LoadCustomMatcher()
c.SetCurrentMatcher(c.config.Matcher)
}
return err
}
func (c *Ctx) Result() []Match {
return c.result
}
func (c *Ctx) AddWaitGroup(v int) {
c.wait.Add(v)
}
func (c *Ctx) ReleaseWaitGroup() {
c.wait.Done()
}
func (c *Ctx) WaitDone() {
c.wait.Wait()
}
func (c *Ctx) LoopCh() chan struct{} {
return c.loopCh
}
func (c *Ctx) QueryCh() chan string {
return c.queryCh
}
func (c *Ctx) DrawCh() chan []Match {
return c.drawCh
}
func (c *Ctx) PagingCh() chan PagingRequest {
return c.pagingCh
}
func (c *Ctx) Terminate() {
close(c.loopCh)
}
func (c *Ctx) ExecQuery(v string) {
c.queryCh <- v
}
func (c *Ctx) DrawMatches(m []Match) {
c.drawCh <- m
}
func (c *Ctx) Refresh() {
c.DrawMatches(nil)
}
func (c *Ctx) Buffer() []Match {
// Copy lines so it's safe to read it
lcopy := make([]Match, len(c.lines))
copy(lcopy, c.lines)
return lcopy
}
func (c *Ctx) ReadBuffer(input io.Reader) error {
scanner := bufio.NewScanner(input)
for scanner.Scan() {
line := scanner.Text()
c.lines = append(c.lines, NewNoMatch(line, c.enableSep))
}
if len(c.lines) > 0 {
return nil
}
return fmt.Errorf("No buffer to work with was available")
}
func (c *Ctx) NewView() *View {
return &View{c}
}
func (c *Ctx) NewFilter() *Filter {
return &Filter{c, make(chan string)}
}
func (c *Ctx) NewInput() *Input {
return &Input{c, &sync.Mutex{}}
}
func (c *Ctx) Finish() {
close(c.LoopCh())
}
func (c *Ctx) SetQuery(q []rune) {
c.query = q
c.caretPos = len(q)
}
func (c *Ctx) Matcher() Matcher {
return c.Matchers[c.CurrentMatcher]
}
func (c *Ctx) AddMatcher(m Matcher) {
c.Matchers = append(c.Matchers, m)
}
func (c *Ctx) SetCurrentMatcher(n string) bool {
for i, m := range c.Matchers {
if m.String() == n {
c.CurrentMatcher = i
return true
}
}
return false
}
func (c *Ctx) LoadCustomMatcher() bool {
for name, args := range c.config.CustomMatcher {
c.AddMatcher(NewCustomMatcher(c.enableSep, name, args))
}
return false
}
func (c *Ctx) SignalHandlerLoop(sigCh chan os.Signal) {
defer c.ReleaseWaitGroup()
for {
select {
case <-c.LoopCh():
return
case <-sigCh:
// XXX For future reference: DO NOT, and I mean DO NOT call
// termbox.Close() here. Calling termbox.Close() twice in our
// context actually BLOCKS. Can you believe it? IT BLOCKS.
//
// So if we called termbox.Close() here, and then in main()
// defer termbox.Close() blocks. Not cool.
c.Finish()
return
}
}
}