forked from charmbracelet/bubbletea
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tty.go
80 lines (64 loc) · 1.26 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
package tea
import (
"errors"
"io"
"github.com/muesli/cancelreader"
)
func (p *Program) initTerminal() error {
err := p.initInput()
if err != nil {
return err
}
if p.console != nil {
err = p.console.SetRaw()
if err != nil {
return err
}
}
hideCursor(p.output)
return nil
}
// restoreTerminalState restores the terminal to the state prior to running the
// Bubble Tea program.
func (p Program) restoreTerminalState() error {
showCursor(p.output)
if p.console != nil {
err := p.console.Reset()
if err != nil {
return err
}
}
return p.restoreInput()
}
// 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()
}