From fa533691c4d243f3882f9f0842650bbbb3c13840 Mon Sep 17 00:00:00 2001 From: Matt Farstad Date: Sun, 2 Oct 2022 12:51:35 -0400 Subject: [PATCH] feat(choose): Preselect the Selected Option When Limit is One (#166) * Add startingIndex variable to choose model * refactor: simplify code branches Co-authored-by: Matthew Farstad Co-authored-by: Maas Lalani --- choose/command.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/choose/command.go b/choose/command.go index 56d55ed..dea789f 100644 --- a/choose/command.go +++ b/choose/command.go @@ -49,16 +49,23 @@ func (o Options) Run() error { // Keep track of the selected items. currentSelected := 0 // Check if selected items should be used. - hasSelectedItems := o.Limit > 1 && len(o.Selected) > 0 + hasSelectedItems := len(o.Selected) > 0 + + startingIndex := 0 var items = make([]item, len(o.Options)) + for i, option := range o.Options { // Check if the option should be selected. isSelected := hasSelectedItems && currentSelected < o.Limit && arrayContains(o.Selected, option) // If the option is selected then increment the current selected count. if isSelected { currentSelected++ + if o.Limit == 1 { + startingIndex = i + } } + items[i] = item{text: option, selected: isSelected} } @@ -78,6 +85,7 @@ func (o Options) Run() error { pager.UsePgUpPgDownKeys = false tm, err := tea.NewProgram(model{ + index: startingIndex, height: o.Height, cursor: o.Cursor, selectedPrefix: o.SelectedPrefix,