From 9b32a8b9de65401af468e59efc16c8c332682df2 Mon Sep 17 00:00:00 2001 From: Carlos Alexandro Becker Date: Thu, 2 May 2024 15:12:57 -0300 Subject: [PATCH] docs: HasDarkBackground() in examples (#265) * docs: bubbletea example bg color detection * docs: allocate pty colors example * fix: improve example --- examples/bubbletea/main.go | 9 ++++++++- examples/pty/main.go | 14 ++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/examples/bubbletea/main.go b/examples/bubbletea/main.go index a87b75f..4e389ee 100644 --- a/examples/bubbletea/main.go +++ b/examples/bubbletea/main.go @@ -82,10 +82,16 @@ func teaHandler(s ssh.Session) (tea.Model, []tea.ProgramOption) { txtStyle := renderer.NewStyle().Foreground(lipgloss.Color("10")) quitStyle := renderer.NewStyle().Foreground(lipgloss.Color("8")) + bg := "light" + if renderer.HasDarkBackground() { + bg = "dark" + } + m := model{ term: pty.Term, width: pty.Window.Width, height: pty.Window.Height, + bg: bg, txtStyle: txtStyle, quitStyle: quitStyle, } @@ -97,6 +103,7 @@ type model struct { term string width int height int + bg string txtStyle lipgloss.Style quitStyle lipgloss.Style } @@ -120,6 +127,6 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) { } func (m model) View() string { - s := fmt.Sprintf("Your term is %s\nYour window size is %dx%d", m.term, m.width, m.height) + s := fmt.Sprintf("Your term is %s\nYour window size is %dx%d\nBackground: %s", m.term, m.width, m.height, m.bg) return m.txtStyle.Render(s) + "\n\n" + m.quitStyle.Render("Press 'q' to quit\n") } diff --git a/examples/pty/main.go b/examples/pty/main.go index 88abef0..afd3bde 100644 --- a/examples/pty/main.go +++ b/examples/pty/main.go @@ -15,6 +15,7 @@ import ( "github.com/charmbracelet/ssh" "github.com/charmbracelet/wish" "github.com/charmbracelet/wish/activeterm" + "github.com/charmbracelet/wish/bubbletea" "github.com/charmbracelet/wish/logging" ) @@ -35,9 +36,18 @@ func main() { func(next ssh.Handler) ssh.Handler { return func(sess ssh.Session) { pty, _, _ := sess.Pty() + renderer := bubbletea.MakeRenderer(sess) + + bg := "light" + if renderer.HasDarkBackground() { + bg = "dark" + } + wish.Printf(sess, "Hello, world!\r\n") - wish.Printf(sess, "Term: "+pty.Term+"\r\n") - wish.Printf(sess, "PTY: "+pty.Slave.Name()+"\r\n") + wish.Printf(sess, "Term: %s\r\n", pty.Term) + wish.Printf(sess, "PTY: %s\r\n", pty.Slave.Name()) + wish.Printf(sess, "FD: %d\r\n", pty.Slave.Fd()) + wish.Printf(sess, "Background: %v\r\n", bg) next(sess) } },