diff --git a/choose/command.go b/choose/command.go index f96b1bebac..72de108a21 100644 --- a/choose/command.go +++ b/choose/command.go @@ -4,8 +4,10 @@ import ( "errors" "fmt" "os" + "slices" "strings" + "github.com/charmbracelet/bubbles/key" "github.com/charmbracelet/huh" "github.com/charmbracelet/lipgloss" "github.com/charmbracelet/x/ansi" @@ -33,6 +35,8 @@ func (o Options) Run() error { } theme := huh.ThemeCharm() + keymap := huh.NewDefaultKeyMap() + keymap.Quit = key.NewBinding(key.WithKeys("ctrl+c", "ctrl+q")) options := huh.NewOptions(o.Options...) theme.Focused.Base = lipgloss.NewStyle() @@ -44,6 +48,12 @@ func (o Options) Run() error { theme.Focused.SelectedPrefix = o.SelectedItemStyle.ToLipgloss().SetString(o.SelectedPrefix) theme.Focused.UnselectedPrefix = o.ItemStyle.ToLipgloss().SetString(o.UnselectedPrefix) + if o.Ordered { + slices.SortFunc(options, func(a, b huh.Option[string]) int { + return strings.Compare(a.Key, b.Key) + }) + } + for _, s := range o.Selected { for i, opt := range options { if s == opt.Key || s == opt.Value { @@ -76,6 +86,7 @@ func (o Options) Run() error { WithWidth(width). WithShowHelp(o.ShowHelp). WithTheme(theme). + WithKeyMap(keymap). Run() if err != nil && !errors.Is(err, huh.ErrTimeout) { return err @@ -100,6 +111,7 @@ func (o Options) Run() error { ). WithWidth(width). WithTheme(theme). + WithKeyMap(keymap). WithShowHelp(o.ShowHelp). Run() if err != nil && !errors.Is(err, huh.ErrTimeout) {