-
Notifications
You must be signed in to change notification settings - Fork 78
/
Copy pathtea_unix.go
88 lines (81 loc) · 2.13 KB
/
tea_unix.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
//go:build darwin || dragonfly || freebsd || linux || netbsd || openbsd || solaris
// +build darwin dragonfly freebsd linux netbsd openbsd solaris
package bubbletea
import (
"image/color"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/charmbracelet/ssh"
"github.com/charmbracelet/x/ansi"
"github.com/charmbracelet/x/input"
"github.com/charmbracelet/x/term"
"github.com/lucasb-eyer/go-colorful"
"github.com/muesli/termenv"
)
func makeOpts(s ssh.Session) []tea.ProgramOption {
pty, _, ok := s.Pty()
if !ok || s.EmulatedPty() {
return []tea.ProgramOption{
tea.WithInput(s),
tea.WithOutput(s),
}
}
return []tea.ProgramOption{
tea.WithInput(pty.Slave),
tea.WithOutput(pty.Slave),
}
}
func newRenderer(s ssh.Session) *lipgloss.Renderer {
pty, _, ok := s.Pty()
if !ok || pty.Term == "" || pty.Term == "dumb" {
return lipgloss.NewRenderer(s, termenv.WithProfile(termenv.Ascii))
}
env := sshEnviron(append(s.Environ(), "TERM="+pty.Term))
var r *lipgloss.Renderer
var bg color.Color
if ok && pty.Slave != nil {
r = lipgloss.NewRenderer(
pty.Slave,
termenv.WithEnvironment(env),
termenv.WithColorCache(true),
)
state, err := term.MakeRaw(pty.Slave.Fd())
if err == nil {
bg, _ = queryBackgroundColor(pty.Slave, pty.Slave)
term.Restore(pty.Slave.Fd(), state)
}
} else {
r = lipgloss.NewRenderer(
s,
termenv.WithEnvironment(env),
termenv.WithUnsafe(),
termenv.WithColorCache(true),
)
bg = querySessionBackgroundColor(s)
}
if bg != nil {
c, ok := colorful.MakeColor(bg)
if ok {
_, _, l := c.Hsl()
r.SetHasDarkBackground(l < 0.5)
}
}
return r
}
// copied from x/[email protected].
func querySessionBackgroundColor(s ssh.Session) (bg color.Color) {
_ = queryTerminal(s, s, time.Second, func(events []input.Event) bool {
for _, e := range events {
switch e := e.(type) {
case input.BackgroundColorEvent:
bg = e.Color
continue // we need to consume the next DA1 event
case input.PrimaryDeviceAttributesEvent:
return false
}
}
return true
}, ansi.RequestBackgroundColor+ansi.RequestPrimaryDeviceAttributes)
return
}