fix(choose): order when using --label-delimiter (#867)

closes #829
This commit is contained in:
Carlos Alexandro Becker 2025-03-05 23:57:08 -03:00 committed by GitHub
commit 204d21940e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -3,7 +3,6 @@ package choose
import (
"errors"
"fmt"
"maps"
"os"
"slices"
"sort"
@ -38,6 +37,8 @@ func (o Options) Run() error {
// normalize options into a map
options := map[string]string{}
// keep the labels in the user-provided order
var labels []string
for _, opt := range o.Options {
if o.LabelDelimiter == "" {
options[opt] = opt
@ -47,10 +48,11 @@ func (o Options) Run() error {
if !ok {
return fmt.Errorf("invalid option format: %q", opt)
}
labels = append(labels, label)
options[label] = value
}
if o.LabelDelimiter != "" {
o.Options = slices.Collect(maps.Keys(options))
o.Options = labels
}
if o.SelectIfOne && len(o.Options) == 1 {