2020-10-13 12:52:30 -04:00
|
|
|
package tea
|
|
|
|
|
2022-04-12 10:23:10 -04:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
|
|
|
|
|
|
|
"github.com/muesli/cancelreader"
|
|
|
|
)
|
|
|
|
|
2021-02-26 18:38:52 -05:00
|
|
|
func (p *Program) initTerminal() error {
|
|
|
|
err := p.initInput()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-01-11 16:33:14 -05:00
|
|
|
}
|
|
|
|
|
2021-07-29 16:47:13 -04:00
|
|
|
if p.console != nil {
|
2021-02-26 18:38:52 -05:00
|
|
|
err = p.console.SetRaw()
|
2021-01-11 16:33:14 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-04 09:14:03 -04:00
|
|
|
p.output.HideCursor()
|
2020-10-13 12:52:30 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-12 10:23:10 -04:00
|
|
|
// restoreTerminalState restores the terminal to the state prior to running the
|
|
|
|
// Bubble Tea program.
|
|
|
|
func (p Program) restoreTerminalState() error {
|
2022-06-04 09:14:03 -04:00
|
|
|
p.output.ShowCursor()
|
2021-02-26 18:38:52 -05:00
|
|
|
|
|
|
|
if p.console != nil {
|
|
|
|
err := p.console.Reset()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2021-01-11 16:33:14 -05:00
|
|
|
}
|
2021-02-26 18:38:52 -05:00
|
|
|
|
2021-09-28 13:49:54 -04:00
|
|
|
return p.restoreInput()
|
2020-10-13 12:52:30 -04:00
|
|
|
}
|
2022-04-12 10:23:10 -04:00
|
|
|
|
|
|
|
// initCancelReader (re)commences reading inputs.
|
|
|
|
func (p *Program) initCancelReader() error {
|
|
|
|
var err error
|
|
|
|
p.cancelReader, err = cancelreader.NewReader(p.input)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.readLoopDone = make(chan struct{})
|
|
|
|
go func() {
|
|
|
|
defer close(p.readLoopDone)
|
|
|
|
|
|
|
|
for {
|
|
|
|
if p.ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
msgs, err := readInputs(p.cancelReader)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, io.EOF) && !errors.Is(err, cancelreader.ErrCanceled) {
|
|
|
|
p.errs <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, msg := range msgs {
|
|
|
|
p.msgs <- msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// cancelInput cancels the input reader.
|
|
|
|
func (p *Program) cancelInput() {
|
|
|
|
p.cancelReader.Cancel()
|
|
|
|
}
|