-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathquery.go
95 lines (83 loc) · 2.15 KB
/
query.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
package bubbletea
import (
"image/color"
"io"
"time"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/input"
)
// queryBackgroundColor queries the terminal for the background color.
// If the terminal does not support querying the background color, nil is
// returned.
//
// Note: you will need to set the input to raw mode before calling this
// function.
//
// state, _ := term.MakeRaw(in.Fd())
// defer term.Restore(in.Fd(), state)
//
// copied from x/[email protected].
func queryBackgroundColor(in io.Reader, out io.Writer) (c color.Color, err error) {
// nolint: errcheck
err = queryTerminal(in, out, defaultQueryTimeout,
func(events []input.Event) bool {
for _, e := range events {
switch e := e.(type) {
case input.BackgroundColorEvent:
c = e.Color
continue // we need to consume the next DA1 event
case input.PrimaryDeviceAttributesEvent:
return false
}
}
return true
}, ansi.RequestBackgroundColor+ansi.RequestPrimaryDeviceAttributes)
return
}
const defaultQueryTimeout = time.Second * 2
// QueryTerminalFilter is a function that filters input events using a type
// switch. If false is returned, the QueryTerminal function will stop reading
// input.
type QueryTerminalFilter func(events []input.Event) bool
// queryTerminal queries the terminal for support of various features and
// returns a list of response events.
// Most of the time, you will need to set stdin to raw mode before calling this
// function.
// Note: This function will block until the terminal responds or the timeout
// is reached.
// copied from x/[email protected].
func queryTerminal(
in io.Reader,
out io.Writer,
timeout time.Duration,
filter QueryTerminalFilter,
query string,
) error {
rd, err := input.NewReader(in, "", 0)
if err != nil {
return err
}
defer rd.Close() // nolint: errcheck
done := make(chan struct{}, 1)
defer close(done)
go func() {
select {
case <-done:
case <-time.After(timeout):
rd.Cancel()
}
}()
if _, err := io.WriteString(out, query); err != nil {
return err
}
for {
events, err := rd.ReadEvents()
if err != nil {
return err
}
if !filter(events) {
break
}
}
return nil
}