Skip to content

Commit

Permalink
feat: WithProgramOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
maaslalani committed Feb 28, 2024
1 parent 51d047d commit ccc1284
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
22 changes: 22 additions & 0 deletions examples/tea-options/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

import (
"fmt"

tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/huh"
)

func main() {
var name string
form := huh.NewForm(
huh.NewGroup(huh.NewInput().Description("What should we call you?").Value(&name)),
).WithProgramOptions(tea.WithAltScreen())

err := form.Run()
if err != nil {
fmt.Println("error:", err)
}

fmt.Println("Welcome, " + name + "!")
}
20 changes: 15 additions & 5 deletions form.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,11 @@ type Form struct {
aborted bool

// options
width int
height int
theme *Theme
keymap *KeyMap
width int
height int
theme *Theme
keymap *KeyMap
teaOptions []tea.ProgramOption
}

// NewForm returns a form with the given groups and default themes and
Expand All @@ -78,6 +79,9 @@ func NewForm(groups ...*Group) *Form {
width: 0,
height: 0,
results: make(map[string]any),
teaOptions: []tea.ProgramOption{
tea.WithOutput(os.Stderr),
},
}

// NB: If dynamic forms come into play this will need to be applied when
Expand Down Expand Up @@ -276,6 +280,12 @@ func (f *Form) WithHeight(height int) *Form {
return f
}

// WithProgramOptions sets the tea options of thea form.
func (f *Form) WithProgramOptions(opts ...tea.ProgramOption) *Form {
f.teaOptions = opts
return f
}

// UpdateFieldPositions sets the position on all the fields.
func (f *Form) UpdateFieldPositions() *Form {
firstGroup := 0
Expand Down Expand Up @@ -547,7 +557,7 @@ func (f *Form) Run() error {

// run runs the form in normal mode.
func (f *Form) run() error {
m, err := tea.NewProgram(f, tea.WithOutput(os.Stderr)).Run()
m, err := tea.NewProgram(f, f.teaOptions...).Run()
if m.(*Form).aborted {
err = ErrUserAborted
}
Expand Down

0 comments on commit ccc1284

Please sign in to comment.