MCPcopy
hub / github.com/peco/peco / Setup

Method Setup

source.go:79–205  ·  view source on GitHub ↗

Setup reads from the input os.File.

(ctx context.Context, state *Peco)

Source from the content-addressed store, hash-verified

77
78// Setup reads from the input os.File.
79func (s *Source) Setup(ctx context.Context, state *Peco) {
80 s.setupOnce.Do(func() {
81 done := make(chan struct{})
82 refresh := make(chan struct{}, 1)
83 defer close(done)
84 defer close(refresh)
85 // And also, close the done channel so we can tell the consumers
86 // we have finished reading everything
87 defer close(s.setupDone)
88
89 draw := func(state *Peco) {
90 state.Hub().SendDraw(ctx, nil)
91 }
92
93 go func() {
94 ticker := time.NewTicker(drawRefreshInterval)
95 defer ticker.Stop()
96
97 for {
98 select {
99 case <-done:
100 draw(state)
101 return
102 case <-ticker.C:
103 draw(state)
104 }
105 }
106 }()
107
108 // This sync.Once var is used to receive the notification
109 // that there was at least 1 line read from the source
110 // This is wrapped in a sync.Notify so we can safely call
111 // it in multiple places
112 var notify sync.Once
113 notifycb := func() {
114 // close the ready channel so others can be notified
115 // that there's at least 1 line in the buffer
116 state.Hub().SendStatusMsg(ctx, "", 0)
117 close(s.ready)
118 }
119
120 // Register this to be called in a defer, just in case we could bailed
121 // out without reading a single line.
122 // Note: this will be a no-op if notify.Do has been called before
123 defer notify.Do(notifycb)
124
125 if pdebug.Enabled {
126 pdebug.Printf("Source: using buffer size of %dkb", state.maxScanBufferSize)
127 }
128 scanbuf := make([]byte, state.maxScanBufferSize*1024)
129 scanner := bufio.NewScanner(s.in)
130 scanner.Buffer(scanbuf, state.maxScanBufferSize*1024)
131 defer func() {
132 if util.IsTty(s.in) {
133 return
134 }
135 if closer, ok := s.in.(io.Closer); ok {
136 s.mutex.Lock()

Callers 3

SetupSourceMethod · 0.95
TestSourceFunction · 0.95
TestSourceScannerErrFunction · 0.95

Calls 11

AppendMethod · 0.95
IsTtyFunction · 0.92
NewRawFunction · 0.92
HubMethod · 0.80
ErrMethod · 0.80
SendDrawMethod · 0.65
SendStatusMsgMethod · 0.65
BufferMethod · 0.65
CloseMethod · 0.65
DoneMethod · 0.65
NextMethod · 0.65

Tested by 2

TestSourceFunction · 0.76
TestSourceScannerErrFunction · 0.76