Closed singhrasster closed 5 years ago
Hey @singhrasster that doesn't sound right to me either. Can you tell me some more info about your system? I'm mostly interested in which operating system and terminal you are using
I'm going to close this since it's gone stale
Hey @AlecAivazis , I am getting same issue:
package main
import (
"fmt"
"gopkg.in/AlecAivazis/survey.v1"
)
// the questions to ask
var qs = []*survey.Question{
{
Name: "name",
Prompt: &survey.Input{Message: "What is your name?"},
Validate: survey.Required,
Transform: survey.Title,
},
{
Name: "color",
Prompt: &survey.Select{
Message: "Choose a color:",
Options: []string{"red", "blue", "green"},
Default: "red",
},
},
{
Name: "age",
Prompt: &survey.Input{Message: "How old are you?"},
},
}
func main() {
// the answers will be written to this struct
answers := struct {
Name string // survey will match the question and field names
FavoriteColor string `survey:"color"` // or you can tag fields to match a specific name
Age int // if the types don't match, survey will convert it
}{}
// perform the questions
err := survey.Ask(qs, &answers)
if err != nil {
fmt.Println(err.Error())
return
}
fmt.Printf("%s chose %s.", answers.Name, answers.FavoriteColor)
}
Trying to run with go-expect:
package main
import (
"github.com/Netflix/go-expect"
"log"
"os"
"os/exec"
)
func main() {
c, err := expect.NewConsole(expect.WithStdout(os.Stdout))
if err != nil {
log.Fatal(err)
}
defer c.Close()
cmd := exec.Command("/example")
cmd.Stdin = c.Tty()
cmd.Stdout = c.Tty()
cmd.Stderr = c.Tty()
err = cmd.Start()
if err != nil {
log.Fatal(err)
}
c.ExpectString("What is your name?")
c.SendLine("A")
c.ExpectString("Choose a color:")
c.SendLine("red")
c.ExpectString("How old are you?")
c.SendLine("10")
c.ExpectEOF()
err = cmd.Wait()
if err != nil {
log.Fatal(err)
}
}
Getting:
? What is your name?
^[[47;101R
Any clue how to make it work?
Following the readme, I was trying to play with the library and write a test simulating the command line input and output.
My main.go looks like:
I did go build main.go to create a Go executible
I wrote a test as:
When I run this test as 'go test'
I get the following output:
Not sure why it prints "^[[24;80R".
Shouldn't it be printing the three questions above followed by answers specified above? Am I missing something or my understanding is wrong on how it should work?