Setup reads from the input os.File.
(ctx context.Context, state *Peco)
| 77 | |
| 78 | // Setup reads from the input os.File. |
| 79 | func (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() |