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

Commit b70520c

Browse files
authored
Add option to show/hide cursor when prompting user (#345) (#348)
1 parent 69b2cbe commit b70520c

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

examples/cursor.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/AlecAivazis/survey/v2"
7+
)
8+
9+
// the questions to ask
10+
var simpleQs = []*survey.Question{
11+
{
12+
Name: "name",
13+
Prompt: &survey.Input{
14+
Message: "What is your name?",
15+
},
16+
Validate: survey.Required,
17+
},
18+
}
19+
20+
func main() {
21+
ansmap := make(map[string]interface{})
22+
23+
// ask the question
24+
err := survey.Ask(simpleQs, &ansmap, survey.WithShowCursor(true))
25+
26+
if err != nil {
27+
fmt.Println(err.Error())
28+
return
29+
}
30+
// print the answers
31+
fmt.Printf("Your name is %s.\n", ansmap["name"])
32+
}

input.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,10 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
150150
defer rr.RestoreTermMode()
151151

152152
cursor := i.NewCursor()
153-
cursor.Hide() // hide the cursor
154-
defer cursor.Show() // show the cursor when we're done
153+
if !config.ShowCursor {
154+
cursor.Hide() // hide the cursor
155+
defer cursor.Show() // show the cursor when we're done
156+
}
155157

156158
// start waiting for input
157159
for {

survey.go

+13
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func defaultAskOptions() *AskOptions {
5555
return strings.Contains(strings.ToLower(value), filter)
5656
},
5757
KeepFilter: false,
58+
ShowCursor: false,
5859
},
5960
}
6061
}
@@ -114,6 +115,7 @@ type PromptConfig struct {
114115
SuggestInput string
115116
Filter func(filter string, option string, index int) bool
116117
KeepFilter bool
118+
ShowCursor bool
117119
}
118120

119121
// Prompt is the primary interface for the objects that can take user input
@@ -219,6 +221,17 @@ func WithIcons(setIcons func(*IconSet)) AskOpt {
219221
}
220222
}
221223

224+
// WithShowCursor sets the show cursor behavior when prompting the user
225+
func WithShowCursor(ShowCursor bool) AskOpt {
226+
return func(options *AskOptions) error {
227+
// set the page size
228+
options.PromptConfig.ShowCursor = ShowCursor
229+
230+
// nothing went wrong
231+
return nil
232+
}
233+
}
234+
222235
/*
223236
AskOne performs the prompt for a single prompt and asks for validation if required.
224237
Response types should be something that can be casted from the response type designated

0 commit comments

Comments
 (0)