Skip to content

Commit

Permalink
Add key/value feature for gum choose (#530) (#598)
Browse files Browse the repository at this point in the history
* Add key/value feature for `gum choose` (#530)

* trim the space of key and value

* using SplitN
  • Loading branch information
clysto authored Jul 12, 2024
1 parent 9660c74 commit 9a3e11f
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
12 changes: 11 additions & 1 deletion choose/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 38 in choose/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 2, in <argument> detected (gomnd)
if len(parsed) == 2 {

Check failure on line 39 in choose/command.go

View workflow job for this annotation

GitHub Actions / lint-soft

Magic number: 2, in <condition> detected (gomnd)
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()
Expand Down
1 change: 1 addition & 0 deletions choose/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}

0 comments on commit 9a3e11f

Please sign in to comment.