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

Commit

Permalink
Merge pull request #37 from AlecAivazis/fix/select-default-value
Browse files Browse the repository at this point in the history
Fixed bug with multiple selects in the same readline instance
  • Loading branch information
AlecAivazis authored Apr 21, 2017
2 parents 74e3258 + 03031b4 commit 687a9a9
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
18 changes: 14 additions & 4 deletions select.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Select struct {
Options []string
Default string
selectedIndex int
useDefault bool
}

// the data available to the templates when processing
Expand Down Expand Up @@ -47,10 +48,12 @@ func (s *Select) OnChange(line []rune, pos int, key rune) (newLine []rune, newPo
return []rune(s.Options[s.selectedIndex]), 0, true
// if the user pressed the up arrow
} else if key == terminal.KeyArrowUp && s.selectedIndex > 0 {
s.useDefault = false
// 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 {
s.useDefault = false
// increment the selected index
s.selectedIndex++
}
Expand Down Expand Up @@ -130,13 +133,17 @@ func (s *Select) Prompt(rl *readline.Instance) (interface{}, error) {
for range s.Options {
terminal.Println()
}
// by default, use the default value
s.useDefault = true

// start waiting for input
val, err := rl.Readline()
_, err = rl.Readline()
// show the cursor when we're done
terminal.CursorShow()

// if the value is empty (not sure why)
if val == "" {
var val string
// if we are supposed to use the default value
if s.useDefault {
// if there is a default value
if s.Default != "" {
// use the default value
Expand All @@ -145,9 +152,12 @@ func (s *Select) Prompt(rl *readline.Instance) (interface{}, error) {
// there is no default value so use the first
val = s.Options[0]
}
// otherwise the selected index points to the value
} else {
// the
val = s.Options[s.selectedIndex]
}

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

Expand Down
2 changes: 2 additions & 0 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func Ask(qs []*Question, t interface{}) error {
if err != nil {
return err
}
defer rl.Close()

// if we weren't passed a place to record the answers
if t == nil {
Expand Down Expand Up @@ -96,6 +97,7 @@ func Ask(qs []*Question, t interface{}) error {
if err != nil {
return err
}

}
// return the response
return nil
Expand Down
42 changes: 42 additions & 0 deletions tests/doublselect.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package main

import (
"fmt"

"github.com/alecaivazis/survey"
)

var simpleQs = []*survey.Question{
{
Name: "color",
Prompt: &survey.Select{
Message: "select1:",
Options: []string{"red", "blue", "green"},
},
Validate: survey.Required,
},
{
Name: "color2",
Prompt: &survey.Select{
Message: "select2:",
Options: []string{"red", "blue", "green"},
},
Validate: survey.Required,
},
}

func main() {
answers := struct {
Color string
Color2 string
}{}
// ask the question
err := survey.Ask(simpleQs, &answers)

if err != nil {
fmt.Println(err.Error())
return
}
// print the answers
fmt.Printf("%s and %s.\n", answers.Color, answers.Color2)
}

0 comments on commit 687a9a9

Please sign in to comment.