Skip to content

Commit

Permalink
docs: HasDarkBackground() in examples (#265)
Browse files Browse the repository at this point in the history
* docs: bubbletea example bg color detection

* docs: allocate pty colors example

* fix: improve example
  • Loading branch information
caarlos0 authored May 2, 2024
1 parent e6e9fc4 commit 9b32a8b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
9 changes: 8 additions & 1 deletion examples/bubbletea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand All @@ -97,6 +103,7 @@ type model struct {
term string
width int
height int
bg string
txtStyle lipgloss.Style
quitStyle lipgloss.Style
}
Expand All @@ -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")
}
14 changes: 12 additions & 2 deletions examples/pty/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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)
}
},
Expand Down

0 comments on commit 9b32a8b

Please sign in to comment.