mirror of
https://github.com/charmbracelet/gum
synced 2026-03-14 21:55:45 +01:00
* feat: yes|gum confirm Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com> * fix: rebase on main --------- Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package confirm
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
|
|
"github.com/charmbracelet/bubbles/help"
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
"github.com/charmbracelet/gum/internal/stdin"
|
|
"github.com/charmbracelet/gum/internal/timeout"
|
|
)
|
|
|
|
var errNotConfirmed = errors.New("not confirmed")
|
|
|
|
// 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 {
|
|
line, err := stdin.Read(stdin.SingleLine(true))
|
|
if err == nil {
|
|
switch line {
|
|
case "yes", "y":
|
|
return nil
|
|
default:
|
|
return errNotConfirmed
|
|
}
|
|
}
|
|
|
|
ctx, cancel := timeout.Context(o.Timeout)
|
|
defer cancel()
|
|
|
|
m := model{
|
|
affirmative: o.Affirmative,
|
|
negative: o.Negative,
|
|
showOutput: o.ShowOutput,
|
|
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)
|
|
}
|
|
|
|
if o.ShowOutput {
|
|
confirmationText := m.negative
|
|
if m.confirmation {
|
|
confirmationText = m.affirmative
|
|
}
|
|
fmt.Println(m.prompt, confirmationText)
|
|
}
|
|
|
|
m = tm.(model)
|
|
if m.confirmation {
|
|
return nil
|
|
}
|
|
|
|
return errNotConfirmed
|
|
}
|