mirror of
https://github.com/charmbracelet/gum
synced 2024-06-27 01:30:06 +02:00
01a66511a1
* refactor: hide style flags on error to not clutter usage * docs(style): add comment regarding dynamically hiding flags
42 lines
944 B
Go
42 lines
944 B
Go
package confirm
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/charmbracelet/gum/internal/exit"
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
)
|
|
|
|
// 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 {
|
|
m, err := tea.NewProgram(model{
|
|
affirmative: o.Affirmative,
|
|
negative: o.Negative,
|
|
confirmation: o.Default,
|
|
defaultSelection: o.Default,
|
|
timeout: o.Timeout,
|
|
hasTimeout: o.Timeout > 0,
|
|
prompt: o.Prompt,
|
|
selectedStyle: o.SelectedStyle.ToLipgloss(),
|
|
unselectedStyle: o.UnselectedStyle.ToLipgloss(),
|
|
promptStyle: o.PromptStyle.ToLipgloss(),
|
|
}, tea.WithOutput(os.Stderr)).Run()
|
|
|
|
if err != nil {
|
|
return fmt.Errorf("unable to run confirm: %w", err)
|
|
}
|
|
|
|
if m.(model).aborted {
|
|
os.Exit(exit.StatusAborted)
|
|
} else if m.(model).confirmation {
|
|
os.Exit(0)
|
|
}
|
|
|
|
os.Exit(1)
|
|
|
|
return nil
|
|
}
|