-
Notifications
You must be signed in to change notification settings - Fork 78
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Carlos Alexandro Becker <[email protected]>
- Loading branch information
Showing
46 changed files
with
217 additions
and
371 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,85 +4,26 @@ | |
package bubbletea | ||
|
||
import ( | ||
"image/color" | ||
"time" | ||
|
||
tea "github.com/charmbracelet/bubbletea" | ||
"github.com/charmbracelet/lipgloss" | ||
tea "github.com/charmbracelet/bubbletea/v2" | ||
"github.com/charmbracelet/colorprofile" | ||
"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() { | ||
envs := append(s.Environ(), "TERM="+pty.Term) | ||
return []tea.ProgramOption{ | ||
tea.WithInput(s), | ||
tea.WithOutput(s), | ||
tea.WithEnvironment(envs), | ||
tea.WithColorProfile(colorprofile.Env(envs)), | ||
} | ||
} | ||
|
||
return []tea.ProgramOption{ | ||
tea.WithInput(pty.Slave), | ||
tea.WithOutput(pty.Slave), | ||
tea.WithEnvironment(s.Environ()), | ||
} | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.