-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtty.go
108 lines (86 loc) · 2.2 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
package bento
import (
"errors"
"fmt"
"io"
"time"
"github.com/muesli/cancelreader"
)
func (a *appRunner) initTerminal() error {
if err := a.terminal.EnableRawMode(); err != nil {
return fmt.Errorf("enable raw mode: %w", err)
}
if err := a.terminal.HideCursor(); err != nil {
return fmt.Errorf("hide cursor: %w", err)
}
return nil
}
// restoreTerminal restores the terminal to the state prior to running the
// Bento app.
func (a *appRunner) restoreTerminal() error {
if err := a.terminal.DisableBracketedPaste(); err != nil {
return fmt.Errorf("disable bracketed paste: %w", err)
}
if err := a.terminal.ShowCursor(); err != nil {
return fmt.Errorf("show cursor: %w", err)
}
// a.disableMouse()
// if p.renderer.reportFocus() {
// p.renderer.disableReportFocus()
// }
// if a.renderer.altScreen() {
// p.renderer.exitAltScreen()
// give the terminal a moment to catch up
// time.Sleep(time.Millisecond * 10) //nolint:gomnd
// }
if err := a.terminal.DisableRawMode(); err != nil {
return fmt.Errorf("disable raw mode: %w", err)
}
return nil
}
func (a *appRunner) initCancelReader() error {
r, err := newInputReader(a.terminal)
if err != nil {
return fmt.Errorf("new reader: %w", err)
}
a.cancelReader = r
a.readLoopDone = make(chan struct{})
go a.readLoop()
return nil
}
func (a *appRunner) readLoop() {
defer close(a.readLoopDone)
err := readInputs(a.ctx, a.msgs, a.cancelReader)
if !errors.Is(err, io.EOF) && !errors.Is(err, cancelreader.ErrCanceled) {
select {
case <-a.ctx.Done():
case a.errs <- err:
}
}
}
// waitForReadLoop waits for the cancelReader to finish its read loop.
func (a *appRunner) waitForReadLoop() {
select {
case <-a.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.
}
}
// checkResize detects the current size of the output and informs the program
// via a WindowSizeMsg.
func (a *appRunner) checkResize() {
size, ok, err := a.terminal.Size()
if !ok {
return
}
if err != nil {
select {
case <-a.ctx.Done():
case a.errs <- err:
}
return
}
a.Send(WindowSizeMsg(size))
}