/
source.go
166 lines (147 loc) · 3.84 KB
/
source.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
package peco
import (
"bufio"
"io"
"sync"
"time"
"github.com/lestrrat/go-pdebug"
"github.com/peco/peco/internal/util"
"github.com/peco/peco/pipeline"
"golang.org/x/net/context"
)
// Creates a new Source. Does not start processing the input until you
// call Setup()
func NewSource(in io.Reader, idgen lineIDGenerator, enableSep bool) *Source {
s := &Source{
in: in, // Note that this may be closed, so do not rely on it
enableSep: enableSep,
idgen: idgen,
ready: make(chan struct{}),
setupDone: make(chan struct{}),
setupOnce: sync.Once{},
start: make(chan struct{}, 1),
OutputChannel: pipeline.OutputChannel(make(chan interface{})),
}
s.Reset()
return s
}
// Setup reads from the input os.File.
func (s *Source) Setup(state *Peco) {
s.setupOnce.Do(func() {
done := make(chan struct{})
refresh := make(chan struct{}, 1)
defer close(done)
defer close(refresh)
draw := func(state *Peco) {
// Not a great thing to do, allowing nil to be passed
// as state, but for testing I couldn't come up with anything
// better for the moment
if state != nil {
state.Hub().SendDraw(false)
}
}
go func() {
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-done:
draw(state)
return
case <-ticker.C:
draw(state)
}
}
}()
// This sync.Once var is used to receive the notification
// that there was at least 1 line read from the source
var notify sync.Once
notifycb := func() {
// close the ready channel so others can be notified
// that there's at least 1 line in the buffer
close(s.ready)
}
scanner := bufio.NewScanner(s.in)
defer func() {
if util.IsTty(s.in) {
return
}
if closer, ok := s.in.(io.Closer); ok {
closer.Close()
}
}()
readCount := 0
for scanner.Scan() {
txt := scanner.Text()
readCount++
s.Append(NewRawLine(s.idgen.next(), txt, s.enableSep))
notify.Do(notifycb)
}
// XXX Just in case scanner.Scan() did not return a single line...
// Note: this will be a no-op if notify.Do has been called before
notify.Do(notifycb)
// And also, close the done channel so we can tell the consumers
// we have finished reading everything
close(s.setupDone)
if pdebug.Enabled {
pdebug.Printf("Read all %d lines from source", readCount)
}
})
}
// Start starts
func (s *Source) Start(ctx context.Context) {
// I should be the only one running this method until I bail out
if pdebug.Enabled {
g := pdebug.Marker("Source.Start")
defer g.End()
defer pdebug.Printf("Source sent %d lines", len(s.lines))
}
s.start <- struct{}{}
defer func() { <-s.start }()
for _, l := range s.lines {
select {
case <-ctx.Done():
if pdebug.Enabled {
pdebug.Printf("Source: context.Done detected")
}
return
default:
s.OutputChannel.Send(l)
}
}
s.OutputChannel.SendEndMark("end of input")
}
// Reset resets the state of the source object so that it
// is ready to feed the filters
func (s *Source) Reset() {
if pdebug.Enabled {
g := pdebug.Marker("Source.Reset")
defer g.End()
}
s.OutputChannel = pipeline.OutputChannel(make(chan interface{}))
}
// Ready returns the "input ready" channel. It will be closed as soon as
// the first line of input is processed via Setup()
func (s *Source) Ready() <-chan struct{} {
return s.ready
}
// SetupDone returns the "read all lines" channel. It will be closed as soon as
// the all input has been read
func (s *Source) SetupDone() <-chan struct{} {
return s.done
}
func (s *Source) LineAt(n int) (Line, error) {
s.mutex.RLock()
defer s.mutex.RUnlock()
return bufferLineAt(s.lines, n)
}
func (s *Source) Size() int {
s.mutex.RLock()
defer s.mutex.RUnlock()
return bufferSize(s.lines)
}
func (s *Source) Append(l Line) {
s.mutex.Lock()
defer s.mutex.Unlock()
bufferAppend(&s.lines, l)
}