Skip to content
This repository has been archived by the owner on Apr 19, 2024. It is now read-only.

Commit

Permalink
Small tweaks (#233)
Browse files Browse the repository at this point in the history
* removed bold face of unhighlighted select options

* fixed panic

* fixed tooltip in select template

* tests pass
  • Loading branch information
AlecAivazis authored Jun 24, 2019
1 parent ab25a56 commit 9f0147a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
17 changes: 10 additions & 7 deletions multiselect.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"errors"
"fmt"

"github.com/AlecAivazis/survey/v2/terminal"
"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
)

/*
Expand Down Expand Up @@ -166,7 +166,6 @@ func (m *MultiSelect) filterOptions(config *PromptConfig) []core.OptionAnswer {
return core.OptionAnswerList(m.Options)
}


// the filter to apply
filter := m.Filter
if filter == nil {
Expand Down Expand Up @@ -194,7 +193,7 @@ func (m *MultiSelect) Prompt(config *PromptConfig) (interface{}, error) {
// if there is a default
if m.Default != nil {
// if the default is string values
if defaultValues, ok := m.Default.([]string) ; ok {
if defaultValues, ok := m.Default.([]string); ok {
for _, dflt := range defaultValues {
for i, opt := range m.Options {
// if the option corresponds to the default
Expand All @@ -206,8 +205,8 @@ func (m *MultiSelect) Prompt(config *PromptConfig) (interface{}, error) {
}
}
}
// if the default value is index values
} else if defaultIndices, ok := m.Default.([]int) ; ok {
// if the default value is index values
} else if defaultIndices, ok := m.Default.([]int); ok {
// go over every index we need to enable by default
for _, idx := range defaultIndices {
// and enable it
Expand Down Expand Up @@ -290,8 +289,12 @@ func (m *MultiSelect) Cleanup(config *PromptConfig, val interface{}) error {
for _, ans := range val.([]core.OptionAnswer) {
answer = fmt.Sprintf("%s, %s", answer, ans.Value)
}
// remove the precending commas
answer = answer[2:]

// if we answered anything
if len(answer) > 2 {
// remove the precending commas
answer = answer[2:]
}

// execute the output summary template with the answer
return m.Render(
Expand Down
14 changes: 6 additions & 8 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package survey
import (
"errors"

"github.com/AlecAivazis/survey/v2/terminal"
"github.com/AlecAivazis/survey/v2/core"
"github.com/AlecAivazis/survey/v2/terminal"
)

/*
Expand Down Expand Up @@ -34,7 +34,6 @@ type Select struct {
showingHelp bool
}


// SelectTemplateData is the data available to the templates when processing
type SelectTemplateData struct {
Select
Expand All @@ -52,10 +51,10 @@ var SelectQuestionTemplate = `
{{- color "default+hb"}}{{ .Message }}{{ .FilterMessage }}{{color "reset"}}
{{- if .ShowAnswer}}{{color "cyan"}} {{.Answer}}{{color "reset"}}{{"\n"}}
{{- else}}
{{- " "}}{{- color "cyan"}}[Use arrows to move, space to select, type to filter{{- if and .Help (not .ShowHelp)}}, {{ .Config.HelpInput }} for more help{{end}}]{{color "reset"}}
{{- " "}}{{- color "cyan"}}[Use arrows to move, type to filter{{- if and .Help (not .ShowHelp)}}, {{ .Config.HelpInput }} for more help{{end}}]{{color "reset"}}
{{- "\n"}}
{{- range $ix, $choice := .PageEntries}}
{{- if eq $ix $.SelectedIndex }}{{color $.Config.Icons.SelectFocus.Format }}{{ $.Config.Icons.SelectFocus.Text }} {{else}}{{color "default+hb"}} {{end}}
{{- if eq $ix $.SelectedIndex }}{{color $.Config.Icons.SelectFocus.Format }}{{ $.Config.Icons.SelectFocus.Text }} {{else}}{{color "default"}} {{end}}
{{- $choice.Value}}
{{- color "reset"}}{{"\n"}}
{{- end}}
Expand Down Expand Up @@ -177,7 +176,6 @@ func (s *Select) filterOptions(config *PromptConfig) []core.OptionAnswer {
return core.OptionAnswerList(s.Options)
}


// the filter to apply
filter := s.Filter
if filter == nil {
Expand Down Expand Up @@ -287,11 +285,11 @@ func (s *Select) Prompt(config *PromptConfig) (interface{}, error) {
// if there is a default value
if s.Default != nil {
// if the default is a string
if defaultString, ok := s.Default.(string) ; ok {
if defaultString, ok := s.Default.(string); ok {
// use the default value
val = defaultString
// the default value could also be an interpret which is interpretted as the index
} else if defaultIndex, ok := s.Default.(int) ; ok {
// the default value could also be an interpret which is interpretted as the index
} else if defaultIndex, ok := s.Default.(int); ok {
val = s.Options[defaultIndex]
} else {
return val, errors.New("default value of select must be an int or string")
Expand Down
6 changes: 3 additions & 3 deletions select_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func TestSelectRender(t *testing.T) {
SelectTemplateData{SelectedIndex: 2, PageEntries: core.OptionAnswerList(prompt.Options)},
strings.Join(
[]string{
fmt.Sprintf("%s Pick your word: [Use arrows to move, space to select, type to filter]", defaultIcons().Question.Text),
fmt.Sprintf("%s Pick your word: [Use arrows to move, type to filter]", defaultIcons().Question.Text),
" foo",
" bar",
fmt.Sprintf("%s baz", defaultIcons().SelectFocus.Text),
Expand All @@ -64,7 +64,7 @@ func TestSelectRender(t *testing.T) {
SelectTemplateData{SelectedIndex: 2, PageEntries: core.OptionAnswerList(prompt.Options)},
strings.Join(
[]string{
fmt.Sprintf("%s Pick your word: [Use arrows to move, space to select, type to filter, %s for more help]", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
fmt.Sprintf("%s Pick your word: [Use arrows to move, type to filter, %s for more help]", defaultIcons().Question.Text, string(defaultPromptConfig().HelpInput)),
" foo",
" bar",
fmt.Sprintf("%s baz", defaultIcons().SelectFocus.Text),
Expand All @@ -80,7 +80,7 @@ func TestSelectRender(t *testing.T) {
strings.Join(
[]string{
fmt.Sprintf("%s This is helpful", defaultIcons().Help.Text),
fmt.Sprintf("%s Pick your word: [Use arrows to move, space to select, type to filter]", defaultIcons().Question.Text),
fmt.Sprintf("%s Pick your word: [Use arrows to move, type to filter]", defaultIcons().Question.Text),
" foo",
" bar",
fmt.Sprintf("%s baz", defaultIcons().SelectFocus.Text),
Expand Down

0 comments on commit 9f0147a

Please sign in to comment.