Skip to content
Myr

feat(choose): label delimiters (#783)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

I'm not sure if I like this impl, but it does work. closes #406

authored by Carlos Alexandro Becker · committed by GitHub · parent 6d405c4 · Browse files · 3 files+22 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+22 added−3 removed

  • other:+22 added−3 removed

3 files+22 added−3 removed

modified:choose/command.go+20 added-2 removed
1 unmodified line
23import (4 "errors"5 "fmt"6 "maps"7 "os"8 "slices"9 "sort"10 "strings"
22 unmodified lines
33 }34 o.Options = strings.Split(input, o.InputDelimiter)35 }3637 // normalize options into a map38 options := map[string]string{}39 for _, opt := range o.Options {40 if o.LabelDelimiter == "" {41 options[opt] = opt42 continue43 }44 label, value, ok := strings.Cut(opt, o.LabelDelimiter)45 if !ok {46 return fmt.Errorf("invalid option format: %q", opt)47 }48 options[label] = value49 }50 if o.LabelDelimiter != "" {51 o.Options = slices.Collect(maps.Keys(options))52 }5354 if o.SelectIfOne && len(o.Options) == 1 {37 fmt.Println(o.Options[0])55 fmt.Println(options[o.Options[0]])56 return nil57 }5859 // We don't need to display prefixes if we are only picking one option.
106 unmodified lines
166167 var out []string168 for _, item := range m.items {169 if item.selected {152 out = append(out, item.text)170 out = append(out, options[item.text])171 }172 }173 tty.Println(strings.Join(out, o.OutputDelimiter))174 return nil
1 unmodified line
modified:choose/options.go+1 added
22 unmodified lines
23 Selected []string `help:"Options that should start as selected (selects all if given '*')" default:"" env:"GUM_CHOOSE_SELECTED"`24 SelectIfOne bool `help:"Select the given option if there is only one" group:"Selection"`25 InputDelimiter string `help:"Option delimiter when reading from STDIN" default:"\n" env:"GUM_CHOOSE_INPUT_DELIMITER"`26 OutputDelimiter string `help:"Option delimiter when writing to STDOUT" default:"\n" env:"GUM_CHOOSE_OUTPUT_DELIMITER"`27 LabelDelimiter string `help:"Allows to set a delimiter, so options can be set as label:value" default:"" env:"GUM_CHOOSE_LABEL_DELIMITER"`2829 CursorStyle style.Styles `embed:"" prefix:"cursor." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_CURSOR_"`30 HeaderStyle style.Styles `embed:"" prefix:"header." set:"defaultForeground=99" envprefix:"GUM_CHOOSE_HEADER_"`31 ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
2 unmodified lines
modified:go.mod+1 added-1 removed
1module github.com/charmbracelet/gum23go 1.213go 1.23.045require (6 github.com/Masterminds/semver/v3 v3.3.17 github.com/alecthomas/kong v1.6.0
43 unmodified lines