tty.go 2.9 KB

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