Skip to content

Commit 9a3e11f

Browse files
authored
Add key/value feature for gum choose (#530) (#598)
* Add key/value feature for `gum choose` (#530) * trim the space of key and value * using SplitN
1 parent 9660c74 commit 9a3e11f

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

choose/command.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,17 @@ func (o Options) Run() error {
3333
}
3434

3535
theme := huh.ThemeCharm()
36-
options := huh.NewOptions(o.Options...)
36+
options := make([]huh.Option[string], len(o.Options))
37+
for i, option := range o.Options {
38+
parsed := strings.SplitN(option, o.Deliminator, 2)
39+
if len(parsed) == 2 {
40+
key := strings.TrimSpace(parsed[0])
41+
value := strings.TrimSpace(parsed[1])
42+
options[i] = huh.NewOption(key, value)
43+
} else {
44+
options[i] = huh.NewOption(option, option)
45+
}
46+
}
3747

3848
theme.Focused.Base = lipgloss.NewStyle()
3949
theme.Focused.Title = o.HeaderStyle.ToLipgloss()

choose/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,5 @@ type Options struct {
2626
ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
2727
SelectedItemStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_SELECTED_"`
2828
Timeout time.Duration `help:"Timeout until choose returns selected element" default:"0" env:"GUM_CCHOOSE_TIMEOUT"` // including timeout command options [Timeout,...]
29+
Deliminator string `help:"Deliminator to split the options to keys and values" default:"=" env:"GUM_CHOOSE_DELIMINATOR"`
2930
}

0 commit comments

Comments
 (0)