feat: ability to change default value of confirmation

This commit is contained in:
Maas Lalani 2022-07-29 11:48:37 -04:00
parent 5b7d006487
commit 671777aa59
3 changed files with 15 additions and 16 deletions

View File

@ -12,7 +12,7 @@ func (o Options) Run() error {
m, err := tea.NewProgram(model{
affirmative: o.Affirmative,
negative: o.Negative,
selected: 0,
confirmation: o.Default,
prompt: o.Prompt,
selectedStyle: o.SelectedStyle.ToLipgloss(),
unselectedStyle: o.UnselectedStyle.ToLipgloss(),
@ -23,7 +23,11 @@ func (o Options) Run() error {
return err
}
os.Exit(m.(model).selected)
if m.(model).confirmation {
os.Exit(0)
} else {
os.Exit(1)
}
return nil
}

View File

@ -21,7 +21,8 @@ type model struct {
affirmative string
negative string
quitting bool
selected int
confirmation bool
// styles
promptStyle lipgloss.Style
@ -37,25 +38,18 @@ func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil
case tea.KeyMsg:
switch msg.String() {
case "left", "h", "ctrl+p", "tab":
m.selected = m.selected - 1
if m.selected < 0 {
m.selected = 1
}
case "right", "l", "ctrl+n", "shift+tab":
m.selected = m.selected + 1
if m.selected > 1 {
m.selected = 0
}
case "left", "h", "ctrl+p", "tab",
"right", "l", "ctrl+n", "shift+tab":
m.confirmation = !m.confirmation
case "enter":
m.quitting = true
return m, tea.Quit
case "y", "Y":
m.quitting = true
m.selected = 0
m.confirmation = true
return m, tea.Quit
case "ctrl+c", "q", "n", "N":
m.selected = 1
m.confirmation = false
m.quitting = true
return m, tea.Quit
}
@ -70,7 +64,7 @@ func (m model) View() string {
var aff, neg string
if m.selected == 0 {
if m.confirmation {
aff = m.selectedStyle.Render(m.affirmative)
neg = m.unselectedStyle.Render(m.negative)
} else {

View File

@ -6,6 +6,7 @@ import "github.com/charmbracelet/gum/style"
type Options struct {
Affirmative string `help:"The title of the affirmative action" default:"Yes"`
Negative string `help:"The title of the negative action" default:"No"`
Default bool `help:"Default confirmation action" default:"true"`
Prompt string `arg:"" help:"Prompt to display." default:"Are you sure?"`
PromptStyle style.Styles `embed:"" prefix:"prompt." help:"The style of the prompt" set:"defaultMargin=1 0 0 0"`
UnselectedStyle style.Styles `embed:"" prefix:"unselected." help:"The style of the unselected action" set:"defaultBackground=235" set:"defaultPadding=0 3" set:"defaultMargin=1 1"`