vendor/github.com/charmbracelet/bubbletea/tty.go

136 lines · 2951 bytes

  1package tea
  2
  3import (
  4	"errors"
  5	"fmt"
  6	"io"
  7	"time"
  8
  9	"github.com/charmbracelet/x/term"
 10	"github.com/muesli/cancelreader"
 11)
 12
 13func (p *Program) suspend() {
 14	if err := p.ReleaseTerminal(); err != nil {
 15		// If we can't release input, abort.
 16		return
 17	}
 18
 19	suspendProcess()
 20
 21	_ = p.RestoreTerminal()
 22	go p.Send(ResumeMsg{})
 23}
 24
 25func (p *Program) initTerminal() error {
 26	if _, ok := p.renderer.(*nilRenderer); ok {
 27		// No need to initialize the terminal if we're not rendering
 28		return nil
 29	}
 30
 31	if err := p.initInput(); err != nil {
 32		return err
 33	}
 34
 35	p.renderer.hideCursor()
 36	return nil
 37}
 38
 39// restoreTerminalState restores the terminal to the state prior to running the
 40// Bubble Tea program.
 41func (p *Program) restoreTerminalState() error {
 42	if p.renderer != nil {
 43		p.renderer.disableBracketedPaste()
 44		p.renderer.showCursor()
 45		p.disableMouse()
 46
 47		if p.renderer.reportFocus() {
 48			p.renderer.disableReportFocus()
 49		}
 50
 51		if p.renderer.altScreen() {
 52			p.renderer.exitAltScreen()
 53
 54			// give the terminal a moment to catch up
 55			time.Sleep(time.Millisecond * 10) //nolint:gomnd
 56		}
 57	}
 58
 59	return p.restoreInput()
 60}
 61
 62// restoreInput restores the tty input to its original state.
 63func (p *Program) restoreInput() error {
 64	if p.ttyInput != nil && p.previousTtyInputState != nil {
 65		if err := term.Restore(p.ttyInput.Fd(), p.previousTtyInputState); err != nil {
 66			return fmt.Errorf("error restoring console: %w", err)
 67		}
 68	}
 69	if p.ttyOutput != nil && p.previousOutputState != nil {
 70		if err := term.Restore(p.ttyOutput.Fd(), p.previousOutputState); err != nil {
 71			return fmt.Errorf("error restoring console: %w", err)
 72		}
 73	}
 74	return nil
 75}
 76
 77// initCancelReader (re)commences reading inputs.
 78func (p *Program) initCancelReader() error {
 79	var err error
 80	p.cancelReader, err = newInputReader(p.input)
 81	if err != nil {
 82		return fmt.Errorf("error creating cancelreader: %w", err)
 83	}
 84
 85	p.readLoopDone = make(chan struct{})
 86	go p.readLoop()
 87
 88	return nil
 89}
 90
 91func (p *Program) readLoop() {
 92	defer close(p.readLoopDone)
 93
 94	err := readInputs(p.ctx, p.msgs, p.cancelReader)
 95	if !errors.Is(err, io.EOF) && !errors.Is(err, cancelreader.ErrCanceled) {
 96		select {
 97		case <-p.ctx.Done():
 98		case p.errs <- err:
 99		}
100	}
101}
102
103// waitForReadLoop waits for the cancelReader to finish its read loop.
104func (p *Program) waitForReadLoop() {
105	select {
106	case <-p.readLoopDone:
107	case <-time.After(500 * time.Millisecond): //nolint:gomnd
108		// The read loop hangs, which means the input
109		// cancelReader's cancel function has returned true even
110		// though it was not able to cancel the read.
111	}
112}
113
114// checkResize detects the current size of the output and informs the program
115// via a WindowSizeMsg.
116func (p *Program) checkResize() {
117	if p.ttyOutput == nil {
118		// can't query window size
119		return
120	}
121
122	w, h, err := term.GetSize(p.ttyOutput.Fd())
123	if err != nil {
124		select {
125		case <-p.ctx.Done():
126		case p.errs <- err:
127		}
128
129		return
130	}
131
132	p.Send(WindowSizeMsg{
133		Width:  w,
134		Height: h,
135	})
136}