diff --git a/examples/cursor.go b/examples/cursor.go new file mode 100644 index 00000000..fb8876cb --- /dev/null +++ b/examples/cursor.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" + + "github.com/AlecAivazis/survey/v2" +) + +// the questions to ask +var simpleQs = []*survey.Question{ + { + Name: "name", + Prompt: &survey.Input{ + Message: "What is your name?", + }, + Validate: survey.Required, + }, +} + +func main() { + ansmap := make(map[string]interface{}) + + // ask the question + err := survey.Ask(simpleQs, &ansmap, survey.WithShowCursor(true)) + + if err != nil { + fmt.Println(err.Error()) + return + } + // print the answers + fmt.Printf("Your name is %s.\n", ansmap["name"]) +} diff --git a/input.go b/input.go index 2e9ad3ce..9c1a293a 100644 --- a/input.go +++ b/input.go @@ -150,8 +150,10 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) { defer rr.RestoreTermMode() cursor := i.NewCursor() - cursor.Hide() // hide the cursor - defer cursor.Show() // show the cursor when we're done + if !config.ShowCursor { + cursor.Hide() // hide the cursor + defer cursor.Show() // show the cursor when we're done + } // start waiting for input for { diff --git a/survey.go b/survey.go index e004cf30..1944ffc5 100644 --- a/survey.go +++ b/survey.go @@ -55,6 +55,7 @@ func defaultAskOptions() *AskOptions { return strings.Contains(strings.ToLower(value), filter) }, KeepFilter: false, + ShowCursor: false, }, } } @@ -114,6 +115,7 @@ type PromptConfig struct { SuggestInput string Filter func(filter string, option string, index int) bool KeepFilter bool + ShowCursor bool } // Prompt is the primary interface for the objects that can take user input @@ -219,6 +221,17 @@ func WithIcons(setIcons func(*IconSet)) AskOpt { } } +// WithShowCursor sets the show cursor behavior when prompting the user +func WithShowCursor(ShowCursor bool) AskOpt { + return func(options *AskOptions) error { + // set the page size + options.PromptConfig.ShowCursor = ShowCursor + + // nothing went wrong + return nil + } +} + /* AskOne performs the prompt for a single prompt and asks for validation if required. Response types should be something that can be casted from the response type designated