Skip to content

feat: CLI use multiselect for list(string) #6631

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 6 commits into from
Mar 16, 2023
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
Next Next commit
feat: CLI use multiselect for list(string)
  • Loading branch information
mtojek committed Mar 16, 2023
commit bad2fe2a5fe1a5c12da63e254d284f020c9d7a91
24 changes: 23 additions & 1 deletion cli/cliui/parameter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cliui

import (
"encoding/json"
"fmt"
"strings"

Expand Down Expand Up @@ -69,7 +70,28 @@ func RichParameter(cmd *cobra.Command, templateVersionParameter codersdk.Templat

var err error
var value string
if len(templateVersionParameter.Options) > 0 {
if templateVersionParameter.Type == "list(string)" {
// Move the cursor up a single line for nicer display!
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")

var options []string
err = json.Unmarshal([]byte(templateVersionParameter.DefaultValue), &options)
if err != nil {
return "", err
}

values, err := MultiSelect(cmd, options)
if err == nil {
v, err := json.Marshal(&values)
if err != nil {
return "", err
}

_, _ = fmt.Fprintln(cmd.OutOrStdout())
_, _ = fmt.Fprintln(cmd.OutOrStdout(), " "+Styles.Prompt.String()+Styles.Field.Render(strings.Join(values, ", ")))
value = string(v)
}
} else if len(templateVersionParameter.Options) > 0 {
// Move the cursor up a single line for nicer display!
_, _ = fmt.Fprint(cmd.OutOrStdout(), "\033[1A")
var richParameterOption *codersdk.TemplateVersionParameterOption
Expand Down
39 changes: 39 additions & 0 deletions cli/cliui/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ func init() {
{{- template "option" $.IterateOption $ix $option}}
{{- end}}
{{- end }}`

survey.MultiSelectQuestionTemplate = `
{{- define "option"}}
{{- if eq .SelectedIndex .CurrentIndex }}{{color .Config.Icons.SelectFocus.Format }}{{ .Config.Icons.SelectFocus.Text }}{{color "reset"}}{{else}} {{end}}
{{- if index .Checked .CurrentOpt.Index }}{{color .Config.Icons.MarkedOption.Format }} {{ .Config.Icons.MarkedOption.Text }} {{else}}{{color .Config.Icons.UnmarkedOption.Format }} {{ .Config.Icons.UnmarkedOption.Text }} {{end}}
{{- color "reset"}}
{{- " "}}{{- .CurrentOpt.Value}}
{{end}}
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- if not .ShowAnswer }}
{{- "\n"}}
{{- range $ix, $option := .PageEntries}}
{{- template "option" $.IterateOption $ix $option}}
{{- end}}
{{- end}}`
}

type SelectOptions struct {
Expand Down Expand Up @@ -118,6 +133,30 @@ func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
return value, err
}

func MultiSelect(cmd *cobra.Command, items []string) ([]string, error) {
// Similar hack is applied to Select()
if flag.Lookup("test.v") != nil {
Copy link
Member

Choose a reason for hiding this comment

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

Hmm, so it looks like we're not actually testing the multi select behavior with this hack?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, I was surprised about it too, it just goes with the default. I wasn't able to properly simulate the user marking options.

return items, nil
}

prompt := &survey.MultiSelect{
Message: "Options:",
Options: items,
Default: items,
}

var values []string
err := survey.AskOne(prompt, &values, survey.WithStdio(fileReadWriter{
Reader: cmd.InOrStdin(),
}, fileReadWriter{
Writer: cmd.OutOrStdout(),
}, cmd.OutOrStdout()))
if errors.Is(err, terminal.InterruptErr) {
return nil, Canceled
}
return values, err
}

type fileReadWriter struct {
io.Reader
io.Writer
Expand Down