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
This commit is contained in:
Yachen Mao 2024-07-12 12:26:54 +08:00 committed by GitHub
parent 9660c743c5
commit 9a3e11f542
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 12 additions and 1 deletions

View File

@ -33,7 +33,17 @@ func (o Options) Run() error {
}
theme := huh.ThemeCharm()
options := huh.NewOptions(o.Options...)
options := make([]huh.Option[string], len(o.Options))
for i, option := range o.Options {
parsed := strings.SplitN(option, o.Deliminator, 2)
if len(parsed) == 2 {
key := strings.TrimSpace(parsed[0])
value := strings.TrimSpace(parsed[1])
options[i] = huh.NewOption(key, value)
} else {
options[i] = huh.NewOption(option, option)
}
}
theme.Focused.Base = lipgloss.NewStyle()
theme.Focused.Title = o.HeaderStyle.ToLipgloss()

View File

@ -26,4 +26,5 @@ type Options struct {
ItemStyle style.Styles `embed:"" prefix:"item." hidden:"" envprefix:"GUM_CHOOSE_ITEM_"`
SelectedItemStyle style.Styles `embed:"" prefix:"selected." set:"defaultForeground=212" envprefix:"GUM_CHOOSE_SELECTED_"`
Timeout time.Duration `help:"Timeout until choose returns selected element" default:"0" env:"GUM_CCHOOSE_TIMEOUT"` // including timeout command options [Timeout,...]
Deliminator string `help:"Deliminator to split the options to keys and values" default:"=" env:"GUM_CHOOSE_DELIMINATOR"`
}