44package bubbletea
55
66import (
7+ "image/color"
8+
79 tea "github.com/charmbracelet/bubbletea"
810 "github.com/charmbracelet/lipgloss"
911 "github.com/charmbracelet/ssh"
1012 "github.com/charmbracelet/x/exp/term"
13+ "github.com/charmbracelet/x/exp/term/ansi"
14+ "github.com/charmbracelet/x/exp/term/input"
1115 "github.com/lucasb-eyer/go-colorful"
1216 "github.com/muesli/termenv"
1317)
@@ -30,24 +34,45 @@ func makeOpts(s ssh.Session) []tea.ProgramOption {
3034func newRenderer (s ssh.Session ) * lipgloss.Renderer {
3135 pty , _ , ok := s .Pty ()
3236 env := sshEnviron (append (s .Environ (), "TERM=" + pty .Term ))
33- if ! ok || pty .Slave == nil {
34- return lipgloss .NewRenderer (
37+ var r * lipgloss.Renderer
38+ var bg color.Color
39+ if ok && pty .Slave != nil {
40+ r = lipgloss .NewRenderer (
41+ pty .Slave ,
42+ termenv .WithEnvironment (env ),
43+ termenv .WithColorCache (true ),
44+ )
45+ bg = term .BackgroundColor (pty .Slave , pty .Slave )
46+ } else {
47+ r = lipgloss .NewRenderer (
3548 s ,
3649 termenv .WithEnvironment (env ),
3750 termenv .WithUnsafe (),
3851 termenv .WithColorCache (true ),
3952 )
53+ bg = queryBackgroundColor (s )
4054 }
41- bg := term .BackgroundColor (pty .Slave , pty .Slave )
42- r := lipgloss .NewRenderer (
43- pty .Slave ,
44- termenv .WithEnvironment (env ),
45- termenv .WithColorCache (true ),
46- )
4755 c , ok := colorful .MakeColor (bg )
4856 if ok {
4957 _ , _ , l := c .Hsl ()
5058 r .SetHasDarkBackground (l < 0.5 )
5159 }
5260 return r
5361}
62+
63+ // copied from x/exp/term
64+ func queryBackgroundColor (s ssh.Session ) (bg color.Color ) {
65+ _ = term .QueryTerminal (s , s , func (events []input.Event ) bool {
66+ for _ , e := range events {
67+ switch e := e .(type ) {
68+ case input.BackgroundColorEvent :
69+ bg = e .Color
70+ continue // we need to consume the next DA1 event
71+ case input.PrimaryDeviceAttributesEvent :
72+ return false
73+ }
74+ }
75+ return true
76+ }, ansi .RequestBackgroundColor + ansi .RequestPrimaryDeviceAttributes )
77+ return
78+ }
0 commit comments