forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtty.go
119 lines (98 loc) · 2.44 KB
/
tty.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package tea
import (
"errors"
"fmt"
"io"
"os"
"time"
"github.com/muesli/cancelreader"
"golang.org/x/term"
)
func (p *Program) initTerminal() error {
if err := p.initInput(); err != nil {
return err
}
p.renderer.hideCursor()
return nil
}
// restoreTerminalState restores the terminal to the state prior to running the
// Bubble Tea program.
func (p *Program) restoreTerminalState() error {
if p.renderer != nil {
p.renderer.disableBracketedPaste()
p.renderer.showCursor()
p.disableMouse()
if p.renderer.altScreen() {
p.renderer.exitAltScreen()
// give the terminal a moment to catch up
time.Sleep(time.Millisecond * 10) //nolint:gomnd
}
}
return p.restoreInput()
}
// restoreInput restores the tty input to its original state.
func (p *Program) restoreInput() error {
if p.tty != nil && p.previousTtyState != nil {
if err := term.Restore(int(p.tty.Fd()), p.previousTtyState); err != nil {
return fmt.Errorf("error restoring console: %w", err)
}
}
return nil
}
// initCancelReader (re)commences reading inputs.
func (p *Program) initCancelReader() error {
var err error
p.cancelReader, err = newInputReader(p.input)
if err != nil {
return fmt.Errorf("error creating cancelreader: %w", err)
}
p.readLoopDone = make(chan struct{})
go p.readLoop()
return nil
}
func (p *Program) readLoop() {
defer close(p.readLoopDone)
err := readInputs(p.ctx, p.msgs, p.cancelReader)
if !errors.Is(err, io.EOF) && !errors.Is(err, cancelreader.ErrCanceled) {
select {
case <-p.ctx.Done():
case p.errs <- err:
}
}
}
// waitForReadLoop waits for the cancelReader to finish its read loop.
func (p *Program) waitForReadLoop() {
select {
case <-p.readLoopDone:
case <-time.After(500 * time.Millisecond): //nolint:gomnd
// 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.
}
}
var lastHeight, lastWidth int
// 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 || !term.IsTerminal(int(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
}
if w == lastWidth && h == lastHeight {
return
}
lastWidth, lastHeight = w, h
p.Send(WindowSizeMsg{
Width: w,
Height: h,
})
}