Skip to content

Commit

Permalink
chore(examples): update examples to use the new huh api
Browse files Browse the repository at this point in the history
  • Loading branch information
aymanbagabas committed Jan 29, 2025
1 parent ba282a6 commit 28f1e71
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 51 deletions.
55 changes: 27 additions & 28 deletions examples/bubbletea/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,15 @@ import (
tea "github.com/charmbracelet/bubbletea/v2"
"github.com/charmbracelet/huh/v2"
"github.com/charmbracelet/lipgloss/v2"
"github.com/charmbracelet/lipgloss/v2/compat"
)

const maxWidth = 80

var (
red = lipgloss.AdaptiveColor{Light: "#FE5F86", Dark: "#FE5F86"}
indigo = lipgloss.AdaptiveColor{Light: "#5A56E0", Dark: "#7571F9"}
green = lipgloss.AdaptiveColor{Light: "#02BA84", Dark: "#02BF87"}
red = compat.AdaptiveColor{Light: lipgloss.Color("#FE5F86"), Dark: lipgloss.Color("#FE5F86")}
indigo = compat.AdaptiveColor{Light: lipgloss.Color("#5A56E0"), Dark: lipgloss.Color("#7571F9")}
green = compat.AdaptiveColor{Light: lipgloss.Color("#02BA84"), Dark: lipgloss.Color("#02BF87")}
)

type Styles struct {
Expand All @@ -28,27 +29,27 @@ type Styles struct {
Help lipgloss.Style
}

func NewStyles(lg *lipgloss.Renderer) *Styles {
func NewStyles() *Styles {
s := Styles{}
s.Base = lg.NewStyle().
s.Base = lipgloss.NewStyle().
Padding(1, 4, 0, 1)
s.HeaderText = lg.NewStyle().
s.HeaderText = lipgloss.NewStyle().
Foreground(indigo).
Bold(true).
Padding(0, 1, 0, 2)
s.Status = lg.NewStyle().
s.Status = lipgloss.NewStyle().
Border(lipgloss.RoundedBorder()).
BorderForeground(indigo).
PaddingLeft(1).
MarginTop(1)
s.StatusHeader = lg.NewStyle().
s.StatusHeader = lipgloss.NewStyle().
Foreground(green).
Bold(true)
s.Highlight = lg.NewStyle().
s.Highlight = lipgloss.NewStyle().
Foreground(lipgloss.Color("212"))
s.ErrorHeaderText = s.HeaderText.
Foreground(red)
s.Help = lg.NewStyle().
s.Help = lipgloss.NewStyle().
Foreground(lipgloss.Color("240"))
return &s
}
Expand All @@ -62,16 +63,14 @@ const (

type Model struct {
state state
lg *lipgloss.Renderer
styles *Styles
form *huh.Form
width int
}

func NewModel() Model {
m := Model{width: maxWidth}
m.lg = lipgloss.DefaultRenderer()
m.styles = NewStyles(m.lg)
m.styles = NewStyles()

m.form = huh.NewForm(
huh.NewGroup(
Expand Down Expand Up @@ -106,9 +105,9 @@ func NewModel() Model {
return m
}

func (m Model) Init() (tea.Model, tea.Cmd) {
form, cmd := m.form.Init()
m.form = form.(*huh.Form)
func (m Model) Init() (Model, tea.Cmd) {
var cmd tea.Cmd
m.form, cmd = m.form.Init()
return m, cmd
}

Expand All @@ -119,7 +118,7 @@ func min(x, y int) int {
return x
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.WindowSizeMsg:
m.width = min(msg.Width, maxWidth) - m.styles.Base.GetHorizontalFrameSize()
Expand All @@ -133,9 +132,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmds []tea.Cmd

// Process the form
form, cmd := m.form.Update(msg)
if f, ok := form.(*huh.Form); ok {
m.form = f
var cmd tea.Cmd
m.form, cmd = m.form.Update(msg)
if cmd != nil {
cmds = append(cmds, cmd)
}

Expand All @@ -147,7 +146,7 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m Model) View() string {
func (m Model) View() fmt.Stringer {
s := m.styles

switch m.form.State {
Expand All @@ -157,7 +156,7 @@ func (m Model) View() string {
var b strings.Builder
fmt.Fprintf(&b, "Congratulations, you’re Charm’s newest\n%s!\n\n", title)
fmt.Fprintf(&b, "Your job description is as follows:\n\n%s\n\nPlease proceed to HR immediately.", role)
return s.Status.Margin(0, 1).Padding(1, 2).Width(48).Render(b.String()) + "\n\n"
return tea.View(s.Status.Margin(0, 1).Padding(1, 2).Width(48).Render(b.String()) + "\n\n")
default:

var class string
Expand All @@ -166,8 +165,8 @@ func (m Model) View() string {
}

// Form (left side)
v := strings.TrimSuffix(m.form.View(), "\n\n")
form := m.lg.NewStyle().Margin(1, 0).Render(v)
v := strings.TrimSuffix(m.form.View().String(), "\n\n")
form := lipgloss.NewStyle().Margin(1, 0).Render(v)

// Status (right side)
var status string
Expand Down Expand Up @@ -213,7 +212,7 @@ func (m Model) View() string {
footer = m.appErrorBoundaryView("")
}

return s.Base.Render(header + "\n" + body + "\n\n" + footer)
return tea.View(s.Base.Render(header + "\n" + body + "\n\n" + footer))
}
}

Expand All @@ -231,7 +230,7 @@ func (m Model) appBoundaryView(text string) string {
lipgloss.Left,
m.styles.HeaderText.Render(text),
lipgloss.WithWhitespaceChars("/"),
lipgloss.WithWhitespaceForeground(indigo),
lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(indigo)),
)
}

Expand All @@ -241,7 +240,7 @@ func (m Model) appErrorBoundaryView(text string) string {
lipgloss.Left,
m.styles.ErrorHeaderText.Render(text),
lipgloss.WithWhitespaceChars("/"),
lipgloss.WithWhitespaceForeground(red),
lipgloss.WithWhitespaceStyle(lipgloss.NewStyle().Foreground(red)),
)
}

Expand Down Expand Up @@ -281,7 +280,7 @@ func (m Model) getRole() (string, string) {
}

func main() {
_, err := tea.NewProgram(NewModel()).Run()
err := tea.NewProgram(NewModel()).Run()
if err != nil {
fmt.Println("Oh no:", err)
os.Exit(1)
Expand Down
2 changes: 1 addition & 1 deletion examples/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module examples
go 1.22

require (
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250129155006-b31c7dc257be
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250128214736-a69f8566fb52
github.com/charmbracelet/glamour v0.8.0
github.com/charmbracelet/huh/v2 v2.0.0-00010101000000-000000000000
Expand Down
2 changes: 2 additions & 0 deletions examples/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ github.com/catppuccin/go v0.2.0 h1:ktBeIrIP42b/8FGiScP9sgrWOss3lw0Z5SktRoithGA=
github.com/catppuccin/go v0.2.0/go.mod h1:8IHJuMGaUUjQM82qBrGNBv7LFq6JI3NnQCF6MOlZjpc=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016 h1:qLul2uRoupbiXXjixWPE3yk5tMKEhWEmTHOGZpewFqs=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250123213707-518ff7d0d016/go.mod h1:DBfWo/ohdtbvjyDk/Q4UffdRd/rRKfYTwTqHdPFVHT8=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250129155006-b31c7dc257be h1:4zrQ8HkfxM3MfCHMaElHlrKTO4aMy8x9q3ZBspbF1H0=
github.com/charmbracelet/bubbles/v2 v2.0.0-alpha.2.0.20250129155006-b31c7dc257be/go.mod h1:DBfWo/ohdtbvjyDk/Q4UffdRd/rRKfYTwTqHdPFVHT8=
github.com/charmbracelet/bubbletea v1.0.0 h1:BlNvkVed3DADQlV+W79eioNUOrnMUY25EEVdFUoDoGA=
github.com/charmbracelet/bubbletea v1.0.0/go.mod h1:xc4gm5yv+7tbniEvQ0naiG9P3fzYhk16cTgDZQQW6YE=
github.com/charmbracelet/bubbletea/v2 v2.0.0-alpha.2.0.20250128214736-a69f8566fb52 h1:uZHGNBv3RtyTd98RA4Ex9Jc1VFMSl1sqP7qas3ffyiA=
Expand Down
2 changes: 1 addition & 1 deletion examples/tea-options/main.go → examples/tea-cmds/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ func main() {
var name string
form := huh.NewForm(
huh.NewGroup(huh.NewInput().Description("What should we call you?").Value(&name)),
).WithProgramOptions(tea.WithAltScreen())
).WithCommands(tea.EnterAltScreen)

err := form.Run()
if err != nil {
Expand Down
42 changes: 21 additions & 21 deletions examples/timer/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"log"
"strings"
"time"
Expand All @@ -11,15 +12,15 @@ import (
"github.com/charmbracelet/lipgloss/v2"
)

const (
focusColor = "#2EF8BB"
breakColor = "#FF5F87"
var (
focusColor = lipgloss.Color("#2EF8BB")
breakColor = lipgloss.Color("#FF5F87")
)

var (
focusTitleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(focusColor)).MarginRight(1).SetString("Focus Mode")
breakTitleStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(breakColor)).MarginRight(1).SetString("Break Mode")
pausedStyle = lipgloss.NewStyle().Foreground(lipgloss.Color(breakColor)).MarginRight(1).SetString("Continue?")
focusTitleStyle = lipgloss.NewStyle().Foreground(focusColor).MarginRight(1).SetString("Focus Mode")
breakTitleStyle = lipgloss.NewStyle().Foreground(breakColor).MarginRight(1).SetString("Break Mode")
pausedStyle = lipgloss.NewStyle().Foreground(breakColor).MarginRight(1).SetString("Continue?")
helpStyle = lipgloss.NewStyle().Foreground(lipgloss.Color("240")).MarginTop(2)
sidebarStyle = lipgloss.NewStyle().MarginLeft(3).Padding(1, 3).Border(lipgloss.RoundedBorder()).BorderForeground(helpStyle.GetForeground())
)
Expand Down Expand Up @@ -50,9 +51,9 @@ type Model struct {
progress progress.Model
}

func (m Model) Init() (tea.Model, tea.Cmd) {
form, cmd := m.form.Init()
m.form = form.(*huh.Form)
func (m Model) Init() (Model, tea.Cmd) {
var cmd tea.Cmd
m.form, cmd = m.form.Init()
return m, cmd
}

Expand All @@ -64,7 +65,7 @@ func tickCmd(t time.Time) tea.Msg {
return tickMsg(t)
}

func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmds []tea.Cmd

switch msg := msg.(type) {
Expand Down Expand Up @@ -97,8 +98,8 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
}

// Update form
f, cmd := m.form.Update(msg)
m.form = f.(*huh.Form)
var cmd tea.Cmd
m.form, cmd = m.form.Update(msg)
cmds = append(cmds, cmd)
if m.form.State != huh.StateCompleted {
return m, tea.Batch(cmds...)
Expand Down Expand Up @@ -130,9 +131,9 @@ func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, tea.Batch(cmds...)
}

func (m Model) View() string {
func (m Model) View() fmt.Stringer {
if m.quitting {
return ""
return tea.View("")
}

if m.form.State != huh.StateCompleted {
Expand Down Expand Up @@ -164,14 +165,14 @@ func (m Model) View() string {
s.WriteString(helpStyle.Render("press 'q' to quit"))
}

return baseTimerStyle.Render(s.String())
return tea.View(baseTimerStyle.Render(s.String()))
}

func NewModel() Model {
theme := huh.ThemeCharm()
theme := huh.ThemeCharm(true)
theme.Focused.Base.Border(lipgloss.HiddenBorder())
theme.Focused.Title.Foreground(lipgloss.Color(focusColor))
theme.Focused.SelectSelector.Foreground(lipgloss.Color(focusColor))
theme.Focused.Title.Foreground(focusColor)
theme.Focused.SelectSelector.Foreground(focusColor)
theme.Focused.SelectedOption.Foreground(lipgloss.Color("15"))
theme.Focused.Option.Foreground(lipgloss.Color("7"))

Expand Down Expand Up @@ -212,9 +213,8 @@ func NewModel() Model {

func main() {
m := NewModel()
mm, err := tea.NewProgram(&m).Run()
m = mm.(Model)
if err != nil {
p := tea.NewProgram(&m)
if err := p.Run(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 28f1e71

Please sign in to comment.