From 726e7cbd78944dfb20e0907ae4cfa89331246c58 Mon Sep 17 00:00:00 2001 From: David Martin <13771411+dmartin35@users.noreply.github.com> Date: Thu, 8 Apr 2021 11:31:35 +0200 Subject: [PATCH] Add option to show/hide cursor when prompting user (#345) --- examples/cursor.go | 32 ++++++++++++++++++++++++++++++++ input.go | 6 ++++-- survey.go | 13 +++++++++++++ 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 examples/cursor.go 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