Skip to content

Commit 94788c7

Browse files
committed
fix(tea): query bg color
1 parent 654e657 commit 94788c7

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

bubbletea/query.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package bubbletea
2+
3+
import (
4+
"image/color"
5+
"io"
6+
"time"
7+
8+
"github.com/charmbracelet/x/ansi"
9+
"github.com/charmbracelet/x/input"
10+
)
11+
12+
// queryBackgroundColor queries the terminal for the background color.
13+
// If the terminal does not support querying the background color, nil is
14+
// returned.
15+
//
16+
// Note: you will need to set the input to raw mode before calling this
17+
// function.
18+
//
19+
// state, _ := term.MakeRaw(in.Fd())
20+
// defer term.Restore(in.Fd(), state)
21+
//
22+
// copied from x/[email protected].
23+
func queryBackgroundColor(in io.Reader, out io.Writer) (c color.Color, err error) {
24+
// nolint: errcheck
25+
err = queryTerminal(in, out, defaultQueryTimeout,
26+
func(events []input.Event) bool {
27+
for _, e := range events {
28+
switch e := e.(type) {
29+
case input.BackgroundColorEvent:
30+
c = e.Color
31+
continue // we need to consume the next DA1 event
32+
case input.PrimaryDeviceAttributesEvent:
33+
return false
34+
}
35+
}
36+
return true
37+
}, ansi.RequestBackgroundColor+ansi.RequestPrimaryDeviceAttributes)
38+
return
39+
}
40+
41+
const defaultQueryTimeout = time.Second * 2
42+
43+
// QueryTerminalFilter is a function that filters input events using a type
44+
// switch. If false is returned, the QueryTerminal function will stop reading
45+
// input.
46+
type QueryTerminalFilter func(events []input.Event) bool
47+
48+
// queryTerminal queries the terminal for support of various features and
49+
// returns a list of response events.
50+
// Most of the time, you will need to set stdin to raw mode before calling this
51+
// function.
52+
// Note: This function will block until the terminal responds or the timeout
53+
// is reached.
54+
// copied from x/[email protected].
55+
func queryTerminal(
56+
in io.Reader,
57+
out io.Writer,
58+
timeout time.Duration,
59+
filter QueryTerminalFilter,
60+
query string,
61+
) error {
62+
rd, err := input.NewDriver(in, "", 0)
63+
if err != nil {
64+
return err
65+
}
66+
67+
defer rd.Close() // nolint: errcheck
68+
69+
done := make(chan struct{}, 1)
70+
defer close(done)
71+
go func() {
72+
select {
73+
case <-done:
74+
case <-time.After(timeout):
75+
rd.Cancel()
76+
}
77+
}()
78+
79+
if _, err := io.WriteString(out, query); err != nil {
80+
return err
81+
}
82+
83+
for {
84+
events, err := rd.ReadEvents()
85+
if err != nil {
86+
return err
87+
}
88+
89+
if !filter(events) {
90+
break
91+
}
92+
}
93+
94+
return nil
95+
}

bubbletea/tea_unix.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import (
1212
"github.com/charmbracelet/ssh"
1313
"github.com/charmbracelet/x/ansi"
1414
"github.com/charmbracelet/x/input"
15-
"github.com/charmbracelet/x/term"
1615
"github.com/lucasb-eyer/go-colorful"
1716
"github.com/muesli/termenv"
1817
)
@@ -46,15 +45,15 @@ func newRenderer(s ssh.Session) *lipgloss.Renderer {
4645
termenv.WithEnvironment(env),
4746
termenv.WithColorCache(true),
4847
)
49-
bg, _ = term.QueryBackgroundColor(pty.Slave, pty.Slave)
48+
bg, _ = queryBackgroundColor(pty.Slave, pty.Slave)
5049
} else {
5150
r = lipgloss.NewRenderer(
5251
s,
5352
termenv.WithEnvironment(env),
5453
termenv.WithUnsafe(),
5554
termenv.WithColorCache(true),
5655
)
57-
bg = queryBackgroundColor(s)
56+
bg = querySessionBackgroundColor(s)
5857
}
5958
if bg != nil {
6059
c, ok := colorful.MakeColor(bg)
@@ -66,9 +65,9 @@ func newRenderer(s ssh.Session) *lipgloss.Renderer {
6665
return r
6766
}
6867

69-
// copied from x/exp/term.
70-
func queryBackgroundColor(s ssh.Session) (bg color.Color) {
71-
_ = term.QueryTerminal(s, s, time.Second, func(events []input.Event) bool {
68+
// copied from x/term@v0.1.3.
69+
func querySessionBackgroundColor(s ssh.Session) (bg color.Color) {
70+
_ = queryTerminal(s, s, time.Second, func(events []input.Event) bool {
7271
for _, e := range events {
7372
switch e := e.(type) {
7473
case input.BackgroundColorEvent:

0 commit comments

Comments
 (0)