Skip to content

Commit

Permalink
fix(choose): --ordered
Browse files Browse the repository at this point in the history
closes #687

Signed-off-by: Carlos Alexandro Becker <caarlos0@users.noreply.github.com>
  • Loading branch information
caarlos0 committed Nov 18, 2024
1 parent 3cec9b7 commit 6730324
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 6730324

Please sign in to comment.