2020-10-13 12:52:30 -04:00
|
|
|
package tea
|
|
|
|
|
2022-04-12 10:23:10 -04:00
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
2022-10-14 22:50:01 -04:00
|
|
|
"os"
|
2022-09-27 03:01:29 -04:00
|
|
|
"time"
|
2022-04-12 10:23:10 -04:00
|
|
|
|
2022-10-14 22:50:01 -04:00
|
|
|
isatty "github.com/mattn/go-isatty"
|
2022-04-12 10:23:10 -04:00
|
|
|
"github.com/muesli/cancelreader"
|
2022-10-14 22:50:01 -04:00
|
|
|
"golang.org/x/term"
|
2022-04-12 10:23:10 -04:00
|
|
|
)
|
|
|
|
|
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-10-04 00:04:39 -04:00
|
|
|
p.renderer.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.
|
2022-09-22 18:40:18 -04:00
|
|
|
func (p *Program) restoreTerminalState() error {
|
2022-10-04 00:04:39 -04:00
|
|
|
if p.renderer != nil {
|
|
|
|
p.renderer.showCursor()
|
2022-10-08 23:22:17 -04:00
|
|
|
p.renderer.disableMouseCellMotion()
|
|
|
|
p.renderer.disableMouseAllMotion()
|
|
|
|
|
|
|
|
if p.renderer.altScreen() {
|
|
|
|
p.renderer.exitAltScreen()
|
|
|
|
|
|
|
|
// give the terminal a moment to catch up
|
|
|
|
time.Sleep(time.Millisecond * 10)
|
|
|
|
}
|
2022-10-04 00:04:39 -04:00
|
|
|
}
|
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{})
|
2022-10-06 12:39:57 -04:00
|
|
|
go p.readLoop()
|
2022-04-12 10:23:10 -04:00
|
|
|
|
2022-09-27 03:01:29 -04:00
|
|
|
return nil
|
|
|
|
}
|
2022-04-12 10:23:10 -04:00
|
|
|
|
2022-10-06 12:39:57 -04:00
|
|
|
func (p *Program) readLoop() {
|
2022-09-27 03:01:29 -04:00
|
|
|
defer close(p.readLoopDone)
|
2022-04-12 10:23:10 -04:00
|
|
|
|
2022-09-27 03:01:29 -04:00
|
|
|
for {
|
|
|
|
if p.ctx.Err() != nil {
|
|
|
|
return
|
|
|
|
}
|
2022-04-12 10:23:10 -04:00
|
|
|
|
2022-09-27 03:01:29 -04:00
|
|
|
msgs, err := readInputs(p.cancelReader)
|
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, io.EOF) && !errors.Is(err, cancelreader.ErrCanceled) {
|
2022-10-14 22:50:01 -04:00
|
|
|
select {
|
|
|
|
case <-p.ctx.Done():
|
|
|
|
case p.errs <- err:
|
|
|
|
}
|
2022-04-12 10:23:10 -04:00
|
|
|
}
|
2022-09-27 03:01:29 -04:00
|
|
|
|
|
|
|
return
|
2022-04-12 10:23:10 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 03:01:29 -04:00
|
|
|
for _, msg := range msgs {
|
|
|
|
p.msgs <- msg
|
|
|
|
}
|
|
|
|
}
|
2022-04-12 10:23:10 -04:00
|
|
|
}
|
|
|
|
|
2022-09-27 03:01:29 -04:00
|
|
|
// waitForReadLoop waits for the cancelReader to finish its read loop.
|
|
|
|
func (p *Program) waitForReadLoop() {
|
|
|
|
select {
|
|
|
|
case <-p.readLoopDone:
|
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
// The read loop hangs, which means the input
|
|
|
|
// cancelReader's cancel function has returned true even
|
|
|
|
// though it was not able to cancel the read.
|
|
|
|
}
|
|
|
|
}
|
2022-10-14 22:50:01 -04:00
|
|
|
|
|
|
|
// checkResize detects the current size of the output and informs the program
|
|
|
|
// via a WindowSizeMsg.
|
|
|
|
func (p *Program) checkResize() {
|
|
|
|
f, ok := p.output.TTY().(*os.File)
|
|
|
|
if !ok || !isatty.IsTerminal(f.Fd()) {
|
|
|
|
// can't query window size
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w, h, err := term.GetSize(int(f.Fd()))
|
|
|
|
if err != nil {
|
|
|
|
select {
|
|
|
|
case <-p.ctx.Done():
|
|
|
|
case p.errs <- err:
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Send(WindowSizeMsg{
|
|
|
|
Width: w,
|
|
|
|
Height: h,
|
|
|
|
})
|
|
|
|
}
|