Created
April 26, 2024 16:34
-
-
Save Aerochrome/5fd2af09f94eda22a0875068b8824acc to your computer and use it in GitHub Desktop.
Example of not working background detection when using lipgloss with wish
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
package main | |
// An example Bubble Tea server. This will put an ssh session into alt screen | |
// and continually print up to date terminal information. | |
import ( | |
"context" | |
"errors" | |
"net" | |
"os" | |
"os/signal" | |
"syscall" | |
"time" | |
tea "github.com/charmbracelet/bubbletea" | |
"github.com/charmbracelet/lipgloss" | |
"github.com/charmbracelet/log" | |
"github.com/charmbracelet/ssh" | |
"github.com/charmbracelet/wish" | |
"github.com/charmbracelet/wish/activeterm" | |
"github.com/charmbracelet/wish/bubbletea" | |
"github.com/charmbracelet/wish/logging" | |
) | |
const ( | |
host = "localhost" | |
port = "23234" | |
) | |
func main() { | |
s, err := wish.NewServer( | |
wish.WithAddress(net.JoinHostPort(host, port)), | |
wish.WithHostKeyPath(".ssh/id_ed25519"), | |
wish.WithMiddleware( | |
bubbletea.Middleware(teaHandler), | |
activeterm.Middleware(), // Bubble Tea apps usually require a PTY. | |
logging.Middleware(), | |
), | |
) | |
if err != nil { | |
log.Error("Could not start server", "error", err) | |
} | |
done := make(chan os.Signal, 1) | |
signal.Notify(done, os.Interrupt, syscall.SIGINT, syscall.SIGTERM) | |
log.Info("Starting SSH server", "host", host, "port", port) | |
go func() { | |
if err = s.ListenAndServe(); err != nil && !errors.Is(err, ssh.ErrServerClosed) { | |
log.Error("Could not start server", "error", err) | |
done <- nil | |
} | |
}() | |
<-done | |
log.Info("Stopping SSH server") | |
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | |
defer func() { cancel() }() | |
if err := s.Shutdown(ctx); err != nil && !errors.Is(err, ssh.ErrServerClosed) { | |
log.Error("Could not stop server", "error", err) | |
} | |
} | |
// You can wire any Bubble Tea model up to the middleware with a function that | |
// handles the incoming ssh.Session. Here we just grab the terminal info and | |
// pass it to the new model. You can also return tea.ProgramOptions (such as | |
// tea.WithAltScreen) on a session by session basis. | |
func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) { | |
// This should never fail, as we are using the activeterm middleware. | |
pty, _, _ := s.Pty() | |
// When running a Bubble Tea app over SSH, you shouldn't use the default | |
// lipgloss.NewStyle function. | |
// That function will use the color profile from the os.Stdin, which is the | |
// server, not the client. | |
// We provide a MakeRenderer function in the bubbletea middleware package, | |
// so you can easily get the correct renderer for the current session, and | |
// use it to create the styles. | |
// The recommended way to use these styles is to then pass them down to | |
// your Bubble Tea model. | |
renderer := bubbletea.MakeRenderer(s) | |
// Always returns true? | |
log.Info(renderer.HasDarkBackground()) | |
txtStyle := renderer.NewStyle().Foreground(lipgloss.Color("10")) | |
quitStyle := renderer.NewStyle().Foreground(lipgloss.Color("8")) | |
centerStyle := renderer.NewStyle().Align(lipgloss.Center, lipgloss.Center) | |
// Doesn't seem to work | |
adaptiveStyle := renderer.NewStyle().Foreground(lipgloss.AdaptiveColor{Light: "#ee82ee", Dark: "#ff0000"}) | |
m := model{ | |
term: pty.Term, | |
width: pty.Window.Width, | |
height: pty.Window.Height, | |
txtStyle: txtStyle, | |
quitStyle: quitStyle, | |
adaptiveStyle: adaptiveStyle, | |
centerStyle: centerStyle, | |
} | |
return m, []tea.ProgramOption{tea.WithAltScreen()} | |
} | |
// Just a generic tea.Model to demo terminal information of ssh. | |
type model struct { | |
term string | |
width int | |
height int | |
txtStyle lipgloss.Style | |
quitStyle lipgloss.Style | |
adaptiveStyle lipgloss.Style | |
centerStyle lipgloss.Style | |
} | |
func (m model) Init() tea.Cmd { | |
return nil | |
} | |
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { | |
switch msg := msg.(type) { | |
case tea.WindowSizeMsg: | |
m.height = msg.Height | |
m.width = msg.Width | |
m.centerStyle = m.centerStyle.Copy().Width(msg.Width).Height(msg.Height) | |
case tea.KeyMsg: | |
switch msg.String() { | |
case "q", "ctrl+c": | |
return m, tea.Quit | |
} | |
} | |
return m, nil | |
} | |
func (m model) View() string { | |
return m.centerStyle.Render("This is a simple test app!\n" + m.adaptiveStyle.Render("If you see red on a white bg ... that's bad!")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment