initInputReader (re)commences reading inputs.
(cancel bool)
| 57 | |
| 58 | // initInputReader (re)commences reading inputs. |
| 59 | func (p *Program) initInputReader(cancel bool) error { |
| 60 | if cancel && p.cancelReader != nil { |
| 61 | p.cancelReader.Cancel() |
| 62 | p.waitForReadLoop() |
| 63 | } |
| 64 | |
| 65 | // Close the reader we are about to replace, otherwise its resources (e.g. |
| 66 | // the epoll file descriptor on Linux) leak on every ReleaseTerminal and |
| 67 | // RestoreTerminal cycle. Only close it once its read loop has exited: if |
| 68 | // waitForReadLoop timed out, the loop may still be using it. |
| 69 | if p.cancelReader != nil && p.readLoopDone != nil { |
| 70 | select { |
| 71 | case <-p.readLoopDone: |
| 72 | _ = p.cancelReader.Close() |
| 73 | default: |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | term := p.environ.Getenv("TERM") |
| 78 | |
| 79 | // Initialize the input reader. |
| 80 | // This need to be done after the terminal has been initialized and set to |
| 81 | // raw mode. |
| 82 | |
| 83 | var err error |
| 84 | p.cancelReader, err = uv.NewCancelReader(p.input) |
| 85 | if err != nil { |
| 86 | return fmt.Errorf("bubbletea: could not create cancelable reader: %w", err) |
| 87 | } |
| 88 | |
| 89 | drv := uv.NewTerminalReader(p.cancelReader, term) |
| 90 | drv.SetLogger(p.logger) |
| 91 | p.inputScanner = drv |
| 92 | p.readLoopDone = make(chan struct{}) |
| 93 | |
| 94 | go p.readLoop(p.ctx, drv, p.readLoopDone, p.errs) |
| 95 | |
| 96 | return nil |
| 97 | } |
| 98 | |
| 99 | // readLoop streams input events to the program until ctx is done or reading |
| 100 | // fails. It takes the per-run state it uses as arguments rather than reading |