gum/confirm/command.go
2024-12-09 14:30:35 -03:00

47 lines
1.1 KiB
Go

package confirm
import (
"errors"
"fmt"
"os"
"github.com/charmbracelet/bubbles/help"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/gum/internal/timeout"
)
// Run provides a shell script interface for prompting a user to confirm an
// action with an affirmative or negative answer.
func (o Options) Run() error {
ctx, cancel := timeout.Context(o.Timeout)
defer cancel()
m := model{
affirmative: o.Affirmative,
negative: o.Negative,
confirmation: o.Default,
defaultSelection: o.Default,
keys: defaultKeymap(o.Affirmative, o.Negative),
help: help.New(),
showHelp: o.ShowHelp,
prompt: o.Prompt,
selectedStyle: o.SelectedStyle.ToLipgloss(),
unselectedStyle: o.UnselectedStyle.ToLipgloss(),
promptStyle: o.PromptStyle.ToLipgloss(),
}
tm, err := tea.NewProgram(
m,
tea.WithOutput(os.Stderr),
tea.WithContext(ctx),
).Run()
if err != nil {
return fmt.Errorf("unable to confirm: %w", err)
}
m = tm.(model)
if m.confirmation {
return nil
}
return errors.New("not confirmed")
}