Skip to content

Commit 8a3cd26

Browse files
chore: use strings.Builder, fix select all logic, apply nitpicks
1 parent 2b621ce commit 8a3cd26

File tree

1 file changed

+56
-48
lines changed

1 file changed

+56
-48
lines changed

cli/cliui/select.go

Lines changed: 56 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -179,31 +179,33 @@ func (m selectModel) View() string {
179179

180180
msg := pretty.Sprintf(pretty.Bold(), "? %s", m.message)
181181

182-
if m.selected == "" {
183-
if m.hideSearch {
184-
s += fmt.Sprintf("%s [Use arrows to move]\n", msg)
185-
} else {
186-
s += fmt.Sprintf("%s %s[Use arrows to move, type to filter]\n", msg, m.search.View())
187-
}
182+
if m.selected != "" {
183+
selected := pretty.Sprint(DefaultStyles.Keyword, m.selected)
184+
s += fmt.Sprintf("%s %s\n", msg, selected)
188185

189-
options, start := m.viewableOptions()
186+
return s
187+
}
190188

191-
for i, option := range options {
192-
// Is this the currently selected option?
193-
style := pretty.Wrap(" ", "")
194-
if m.cursor == start+i {
195-
style = pretty.Style{
196-
pretty.Wrap("> ", ""),
197-
pretty.FgColor(Green),
198-
}
199-
}
189+
if m.hideSearch {
190+
s += fmt.Sprintf("%s [Use arrows to move]\n", msg)
191+
} else {
192+
s += fmt.Sprintf("%s %s[Use arrows to move, type to filter]\n", msg, m.search.View())
193+
}
194+
195+
options, start := m.viewableOptions()
200196

201-
s += pretty.Sprint(style, option)
202-
s += "\n"
197+
for i, option := range options {
198+
// Is this the currently selected option?
199+
style := pretty.Wrap(" ", "")
200+
if m.cursor == start+i {
201+
style = pretty.Style{
202+
pretty.Wrap("> ", ""),
203+
pretty.FgColor(Green),
204+
}
203205
}
204-
} else {
205-
selected := pretty.Sprint(DefaultStyles.Keyword, m.selected)
206-
s += fmt.Sprintf("%s %s\n", msg, selected)
206+
207+
s += pretty.Sprint(style, option)
208+
s += "\n"
207209
}
208210

209211
return s
@@ -259,6 +261,7 @@ func MultiSelect(inv *serpent.Invocation, opts MultiSelectOptions) ([]string, er
259261
for _, d := range opts.Defaults {
260262
if option == d {
261263
chosen = true
264+
break
262265
}
263266
}
264267

@@ -353,7 +356,7 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
353356
case tea.KeyRight:
354357
options := m.filteredOptions()
355358
for _, option := range options {
356-
option.chosen = false
359+
option.chosen = true
357360
}
358361

359362
case tea.KeyLeft:
@@ -381,42 +384,47 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
381384
}
382385

383386
func (m multiSelectModel) View() string {
384-
var s string
387+
var s strings.Builder
385388

386389
msg := pretty.Sprintf(pretty.Bold(), "? %s", m.message)
387390

388-
if !m.selected {
389-
s += fmt.Sprintf("%s %s[Use arrows to move, space to select, <right> to all, <left> to none, type to filter]\n", msg, m.search.View())
390-
391-
for i, option := range m.filteredOptions() {
392-
cursor := " "
393-
chosen := "[ ]"
394-
o := option.option
391+
if m.selected {
392+
selected := pretty.Sprint(DefaultStyles.Keyword, strings.Join(m.selectedOptions(), ", "))
393+
_, _ = s.WriteString(fmt.Sprintf("%s %s\n", msg, selected))
395394

396-
if m.cursor == i {
397-
cursor = pretty.Sprint(pretty.FgColor(Green), "> ")
398-
chosen = pretty.Sprint(pretty.FgColor(Green), "[ ]")
399-
o = pretty.Sprint(pretty.FgColor(Green), o)
400-
}
395+
return s.String()
396+
}
401397

402-
if option.chosen {
403-
chosen = pretty.Sprint(pretty.FgColor(Green), "[x]")
404-
}
398+
_, _ = s.WriteString(fmt.Sprintf(
399+
"%s %s[Use arrows to move, space to select, <right> to all, <left> to none, type to filter]\n",
400+
msg,
401+
m.search.View(),
402+
))
403+
404+
for i, option := range m.filteredOptions() {
405+
cursor := " "
406+
chosen := "[ ]"
407+
o := option.option
408+
409+
if m.cursor == i {
410+
cursor = pretty.Sprint(pretty.FgColor(Green), "> ")
411+
chosen = pretty.Sprint(pretty.FgColor(Green), "[ ]")
412+
o = pretty.Sprint(pretty.FgColor(Green), o)
413+
}
405414

406-
s += fmt.Sprintf(
407-
"%s%s %s\n",
408-
cursor,
409-
chosen,
410-
o,
411-
)
415+
if option.chosen {
416+
chosen = pretty.Sprint(pretty.FgColor(Green), "[x]")
412417
}
413-
} else {
414-
selected := pretty.Sprint(DefaultStyles.Keyword, strings.Join(m.selectedOptions(), ", "))
415418

416-
s += fmt.Sprintf("%s %s\n", msg, selected)
419+
_, _ = s.WriteString(fmt.Sprintf(
420+
"%s%s %s\n",
421+
cursor,
422+
chosen,
423+
o,
424+
))
417425
}
418426

419-
return s
427+
return s.String()
420428
}
421429

422430
func (m multiSelectModel) filteredOptions() []*multiSelectOption {

0 commit comments

Comments
 (0)