command.go
This file
Path
confirm/command.go
71 lines · 1.6 KiB · Go
Last changed by
chore: prepare v2 (#1116)Drew Smirnoffbefore adoption02d6f06
History of this fileTake it
1package confirm23import (4 "context"5 "fmt"6 "os"78 "charm.land/bubbles/v2/help"9 tea "charm.land/bubbletea/v2"10 "charm.land/gum/v2/internal/exit"11 "charm.land/gum/v2/internal/stdin"12 "charm.land/gum/v2/internal/timeout"13 "charm.land/gum/v2/style"14)1516// Run provides a shell script interface for prompting a user to confirm an17// action with an affirmative or negative answer.18func (o Options) Run() error {19 line, err := stdin.Read(stdin.SingleLine(true))20 if err == nil {21 switch line {22 case "yes", "y":23 return nil24 default:25 return exit.ErrExit(1)26 }27 }2829 ctx, cancel := timeout.Context(o.Timeout)30 defer cancel()3132 top, right, bottom, left := style.ParsePadding(o.Padding)33 m := model{34 affirmative: o.Affirmative,35 negative: o.Negative,36 showOutput: o.ShowOutput,37 confirmation: o.Default,38 defaultSelection: o.Default,39 keys: defaultKeymap(o.Affirmative, o.Negative),40 help: help.New(),41 showHelp: o.ShowHelp,42 prompt: o.Prompt,43 selectedStyle: o.SelectedStyle.ToLipgloss(),44 unselectedStyle: o.UnselectedStyle.ToLipgloss(),45 promptStyle: o.PromptStyle.ToLipgloss(),46 padding: []int{top, right, bottom, left},47 }48 tm, err := tea.NewProgram(49 m,50 tea.WithOutput(os.Stderr),51 tea.WithContext(ctx),52 ).Run()53 if err != nil && ctx.Err() != context.DeadlineExceeded {54 return fmt.Errorf("unable to confirm: %w", err)55 }56 m = tm.(model)5758 if o.ShowOutput {59 confirmationText := m.negative60 if m.confirmation {61 confirmationText = m.affirmative62 }63 fmt.Println(m.prompt, confirmationText)64 }6566 if m.confirmation {67 return nil68 }6970 return exit.ErrExit(1)71}