gum/confirm/command.go

45 lines
884 B
Go
Raw Normal View History

2022-07-26 21:43:26 +02:00
package confirm
import (
2022-08-05 00:45:19 +02:00
"fmt"
2022-07-26 21:43:26 +02:00
"os"
"github.com/charmbracelet/huh"
"github.com/charmbracelet/lipgloss"
2022-07-26 21:43:26 +02:00
)
// 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 {
theme := huh.ThemeCharm()
theme.Focused.Base = lipgloss.NewStyle().Margin(0, 1)
theme.Focused.Title = o.PromptStyle.ToLipgloss()
theme.Focused.FocusedButton = o.SelectedStyle.ToLipgloss()
theme.Focused.BlurredButton = o.UnselectedStyle.ToLipgloss()
choice := o.Default
err := huh.NewForm(
huh.NewGroup(
huh.NewConfirm().
Affirmative(o.Affirmative).
Negative(o.Negative).
Title(o.Prompt).
Value(&choice),
),
).
WithTheme(theme).
WithShowHelp(false).
Run()
2022-07-26 21:43:26 +02:00
if err != nil {
2022-08-05 00:45:19 +02:00
return fmt.Errorf("unable to run confirm: %w", err)
2022-07-26 21:43:26 +02:00
}
if !choice {
os.Exit(1)
}
2022-07-26 21:43:26 +02:00
return nil
}