2022-07-26 22:43:26 +03:00
|
|
|
package confirm
|
|
|
|
|
|
|
|
import (
|
2022-08-05 01:45:19 +03:00
|
|
|
"fmt"
|
2022-07-26 22:43:26 +03:00
|
|
|
"os"
|
|
|
|
|
2023-06-30 16:20:33 +03:00
|
|
|
"github.com/charmbracelet/gum/internal/exit"
|
|
|
|
|
2022-07-26 22:43:26 +03:00
|
|
|
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{
|
2023-02-28 03:16:08 +03:00
|
|
|
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(),
|
2022-10-18 03:23:59 +03:00
|
|
|
}, tea.WithOutput(os.Stderr)).Run()
|
2022-07-26 22:43:26 +03:00
|
|
|
|
|
|
|
if err != nil {
|
2022-08-05 01:45:19 +03:00
|
|
|
return fmt.Errorf("unable to run confirm: %w", err)
|
2022-07-26 22:43:26 +03:00
|
|
|
}
|
|
|
|
|
2022-12-13 23:46:43 +03:00
|
|
|
if m.(model).aborted {
|
2023-06-29 23:36:47 +03:00
|
|
|
os.Exit(exit.StatusAborted)
|
2022-12-13 23:46:43 +03:00
|
|
|
} else if m.(model).confirmation {
|
2022-07-29 18:48:37 +03:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
2022-07-26 22:43:26 +03:00
|
|
|
|
2023-11-08 18:43:50 +03:00
|
|
|
os.Exit(1)
|
|
|
|
|
2022-07-26 22:43:26 +03:00
|
|
|
return nil
|
|
|
|
}
|