mirror of
https://github.com/charmbracelet/gum.git
synced 2024-09-11 06:16:03 +03:00
feat: ability to change default value of confirmation
This commit is contained in:
parent
5b7d006487
commit
671777aa59
@ -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
|
||||
}
|
||||
|
@ -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 {
|
||||
|
@ -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"`
|
||||
|
Loading…
Reference in New Issue
Block a user