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

Commit

Permalink
reverted Select back to Choice to maintain old API
Browse files Browse the repository at this point in the history
  • Loading branch information
AlecAivazis committed Apr 15, 2017
1 parent 704ec9f commit f0bb030
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 39 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ var qs = []*survey.Question{
},
{
Name: "color",
Prompt: &survey.Select{
Prompt: &survey.Choice{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Choices: []string{"red", "blue", "green"},
Default: "red",
},
},
Expand Down
38 changes: 19 additions & 19 deletions select.go → choice.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,46 +9,46 @@ import (
"github.com/chzyer/readline"
)

// Select is a prompt that presents a list of various options to the user
// Choice is a prompt that presents a list of various options to the user
// for them to select using the arrow keys and enter.
type Select struct {
type Choice struct {
Message string
Options []string
Choices []string
Default string
SelectedIndex int
}

// the data available to the templates when processing
type SelectTemplateData struct {
Select
Select Choice
Answer string
}

const (
SelectQuestionTemplate = `
{{- color "green+hb"}}? {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- color "default+hb"}}{{ $.Select.Message }} {{color "reset"}}
{{- if .Answer}}{{color "cyan"}}{{.Answer}}{{color "reset"}}{{end}}`
// the template used to show the list of Selects
SelectChoicesTemplate = `
{{- range $ix, $choice := .Options}}
{{- range $ix, $choice := $.Select.Choices}}
{{- if eq $ix $.Select.SelectedIndex}}{{color "cyan+b"}}> {{else}}{{color "default+hb"}} {{end}}
{{- $choice}}
{{- color "reset"}}
{{end}}`
)

// OnChange is called on every keypress.
func (s *Select) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
func (s *Choice) OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool) {
// if the user pressed the enter key
if key == terminal.KeyEnter {
return []rune(s.Options[s.SelectedIndex]), 0, true
return []rune(s.Choices[s.SelectedIndex]), 0, true
// if the user pressed the up arrow
} else if key == terminal.KeyArrowUp && s.SelectedIndex > 0 {
// decrement the selected index
s.SelectedIndex--
// if the user pressed down and there is room to move
} else if key == terminal.KeyArrowDown && s.SelectedIndex < len(s.Options)-1 {
} else if key == terminal.KeyArrowDown && s.SelectedIndex < len(s.Choices)-1 {
// increment the selected index
s.SelectedIndex++
}
Expand All @@ -57,11 +57,11 @@ func (s *Select) OnChange(line []rune, pos int, key rune) (newLine []rune, newPo
s.render()

// if we are not pressing ent
return []rune(s.Options[s.SelectedIndex]), 0, true
return []rune(s.Choices[s.SelectedIndex]), 0, true
}

func (s *Select) render() error {
for range s.Options {
func (s *Choice) render() error {
for range s.Choices {
terminal.CursorPreviousLine(1)
terminal.EraseInLine(1)
}
Expand All @@ -81,15 +81,15 @@ func (s *Select) render() error {
return nil
}

func (s *Select) Prompt(rl *readline.Instance) (string, error) {
func (s *Choice) Prompt(rl *readline.Instance) (string, error) {
config := &readline.Config{
Listener: s,
Stdout: ioutil.Discard,
}
rl.SetConfig(config)

// if there are no options to render
if len(s.Options) == 0 {
if len(s.Choices) == 0 {
// we failed
return "", errors.New("please provide options to select from")
}
Expand All @@ -99,7 +99,7 @@ func (s *Select) Prompt(rl *readline.Instance) (string, error) {
// if there is a default
if s.Default != "" {
// find the choice
for i, opt := range s.Options {
for i, opt := range s.Choices {
// if the option correponds to the default
if opt == s.Default {
// we found our initial value
Expand All @@ -125,7 +125,7 @@ func (s *Select) Prompt(rl *readline.Instance) (string, error) {
terminal.CursorHide()
// ask the question
terminal.Println(out)
for range s.Options {
for range s.Choices {
terminal.Println()
}
// start waiting for input
Expand All @@ -141,18 +141,18 @@ func (s *Select) Prompt(rl *readline.Instance) (string, error) {
val = s.Default
} else {
// there is no default value so use the first
val = s.Options[0]
val = s.Choices[0]
}
}

// return rl.Readline()
return val, err
}

func (s *Select) Cleanup(rl *readline.Instance, val string) error {
func (s *Choice) Cleanup(rl *readline.Instance, val string) error {
terminal.CursorPreviousLine(1)
terminal.EraseInLine(1)
for range s.Options {
for range s.Choices {
terminal.CursorPreviousLine(1)
terminal.EraseInLine(1)
}
Expand Down
14 changes: 7 additions & 7 deletions select_test.go → choice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ func init() {

func TestCanFormatSelectOptions(t *testing.T) {

prompt := &Select{
Options: []string{"foo", "bar", "baz", "buz"},
prompt := &Choice{
Choices: []string{"foo", "bar", "baz", "buz"},
Default: "baz",
}
// TODO: figure out a way for the test to actually test this bit of code
Expand All @@ -24,7 +24,7 @@ func TestCanFormatSelectOptions(t *testing.T) {
)

if err != nil {
t.Errorf("Failed to run template to format choice options: %s", err)
t.Errorf("Failed to run template to format choice choices: %s", err)
}

expected := ` foo
Expand All @@ -40,9 +40,9 @@ func TestCanFormatSelectOptions(t *testing.T) {

func TestSelectFormatQuestion(t *testing.T) {

prompt := &Select{
prompt := &Choice{
Message: "Pick your word:",
Options: []string{"foo", "bar", "baz", "buz"},
Choices: []string{"foo", "bar", "baz", "buz"},
Default: "baz",
}

Expand All @@ -63,9 +63,9 @@ func TestSelectFormatQuestion(t *testing.T) {

func TestSelectFormatAnswer(t *testing.T) {

prompt := &Select{
prompt := &Choice{
Message: "Pick your word:",
Options: []string{"foo", "bar", "baz", "buz"},
Choices: []string{"foo", "bar", "baz", "buz"},
Default: "baz",
}

Expand Down
4 changes: 2 additions & 2 deletions examples/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ var simpleQs = []*survey.Question{
},
{
Name: "color",
Prompt: &survey.Select{
Prompt: &survey.Choice{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Choices: []string{"red", "blue", "green"},
},
Validate: survey.Required,
},
Expand Down
18 changes: 9 additions & 9 deletions tests/select.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ import (

var goodTable = []TestUtil.TestTableEntry{
{
"standard", &survey.Select{
"standard", &survey.Choice{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Choices: []string{"red", "blue", "green"},
},
},
{
"short", &survey.Select{
"short", &survey.Choice{
Message: "Choose a color:",
Options: []string{"red", "blue"},
Choices: []string{"red", "blue"},
},
},
{
"default", &survey.Select{
"default", &survey.Choice{
Message: "Choose a color (should default blue):",
Options: []string{"red", "blue", "green"},
Choices: []string{"red", "blue", "green"},
Default: "blue",
},
},
{
"one", &survey.Select{
"one", &survey.Choice{
Message: "Choose one:",
Options: []string{"hello"},
Choices: []string{"hello"},
},
},
}

var badTable = []TestUtil.TestTableEntry{
{
"no options", &survey.Select{
"no Choices", &survey.Choice{
Message: "Choose one:",
},
},
Expand Down

0 comments on commit f0bb030

Please sign in to comment.