Skip to content

feat: allow entering non-default values in multi-select #15935

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Dec 27, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: duplicates, cursor positioning
  • Loading branch information
joobisb committed Dec 20, 2024
commit cc41fe79798c70437cadadb4bcac37e831f671ea
67 changes: 52 additions & 15 deletions cli/cliui/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ type multiSelectModel struct {
message string
canceled bool
selected bool
isInputMode bool // New field to track if we're adding a custom option
isCustomInputMode bool // New field to track if we're adding a custom option
customInput string // New field to store custom input
enableCustomInput bool // New field to control whether custom input is allowed
}
Expand All @@ -391,7 +391,7 @@ func (multiSelectModel) Init() tea.Cmd {
func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd

if m.isInputMode {
if m.isCustomInputMode {
return m.handleCustomInputMode(msg)
}

Expand All @@ -409,7 +409,7 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
case tea.KeyEnter:
// Switch to custom input mode if we're on the "+ Add custom value:" option
if m.enableCustomInput && m.cursor == len(m.filteredOptions()) {
m.isInputMode = true
m.isCustomInputMode = true
return m, nil
}
if len(m.options) != 0 {
Expand All @@ -427,17 +427,15 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, nil

case tea.KeyUp:
options := m.filteredOptions()
maxIndex := len(options)
maxIndex := m.getMaxIndex()
if m.cursor > 0 {
m.cursor--
} else {
m.cursor = maxIndex
}

case tea.KeyDown:
options := m.filteredOptions()
maxIndex := len(options)
maxIndex := m.getMaxIndex()
if m.cursor < maxIndex {
m.cursor++
} else {
Expand Down Expand Up @@ -473,6 +471,16 @@ func (m multiSelectModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m, cmd
}

func (m multiSelectModel) getMaxIndex() int {
options := m.filteredOptions()
if m.enableCustomInput {
// Include the "+ Add custom value" entry
return len(options)
}
// Includes only the actual options
return len(options) - 1
}

// handleCustomInputMode manages keyboard interactions when in custom input mode
func (m *multiSelectModel) handleCustomInputMode(msg tea.Msg) (tea.Model, tea.Cmd) {
keyMsg, ok := msg.(tea.KeyMsg)
Expand All @@ -499,15 +507,44 @@ func (m *multiSelectModel) handleCustomInputMode(msg tea.Msg) (tea.Model, tea.Cm

// handleCustomInputSubmission processes the submission of custom input
func (m *multiSelectModel) handleCustomInputSubmission() (tea.Model, tea.Cmd) {
if m.customInput != "" {
m.options = append(m.options, &multiSelectOption{
option: m.customInput,
chosen: true,
})
if m.customInput == "" {
m.isCustomInputMode = false
return m, nil
}

// Clear search to ensure option is visible and cursor points to the new option
m.search.SetValue("")

// Check for duplicates
for i, opt := range m.options {
if strings.EqualFold(opt.option, m.customInput) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not 100% sure about the case-insensitive matching. On the surface, it seems innocuous enough but I'm not sure if there's going to be a case where someone is going to want to differentiate A from a. Let's keep matching case-sensitive for now.
We can add an option for case-insensitive matching later if someone requests it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the PR!

// If the option exists but isn't chosen, select it
if !opt.chosen {
opt.chosen = true
}

// Point cursor to the new option
m.cursor = i

// Reset custom input mode to disabled
m.isCustomInputMode = false
m.customInput = ""
return m, nil
}
}
// Reset input state regardless of whether input was empty

// Add new unique option
m.options = append(m.options, &multiSelectOption{
option: m.customInput,
chosen: true,
})

// Point cursor to the newly added option
m.cursor = len(m.options) - 1

// Reset custom input mode to disabled
m.customInput = ""
m.isInputMode = false
m.isCustomInputMode = false
return m, nil
}

Expand All @@ -531,7 +568,7 @@ func (m multiSelectModel) View() string {
return s.String()
}

if m.isInputMode {
if m.isCustomInputMode {
_, _ = s.WriteString(fmt.Sprintf("%s\nEnter custom value: %s\n", msg, m.customInput))
return s.String()
}
Expand Down
13 changes: 11 additions & 2 deletions cli/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func (RootCmd) promptExample() *serpent.Command {
Default: "",
Value: serpent.StringArrayOf(&multiSelectValues),
}

enableCustomInput bool
enableCustomInputOption = serpent.Option{
Name: "enable-custom-input",
Description: "Enable custom input option in multi-select.",
Required: false,
Flag: "enable-custom-input",
Value: serpent.BoolOf(&enableCustomInput),
}
)
cmd := &serpent.Command{
Use: "prompt-example",
Expand Down Expand Up @@ -159,12 +168,12 @@ func (RootCmd) promptExample() *serpent.Command {
"Code", "Chairs", "Whale", "Diamond", "Carrot",
},
Defaults: []string{"Code"},
EnableCustomInput: true,
EnableCustomInput: enableCustomInput,
})
}
_, _ = fmt.Fprintf(inv.Stdout, "%q are nice choices.\n", strings.Join(multiSelectValues, ", "))
return multiSelectError
}, useThingsOption),
}, useThingsOption, enableCustomInputOption),
promptCmd("rich-parameter", func(inv *serpent.Invocation) error {
value, err := cliui.RichSelect(inv, cliui.RichSelectOptions{
Options: []codersdk.TemplateVersionParameterOption{
Expand Down
Loading