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

Commit

Permalink
Add option to show/hide cursor when prompting user (#345) (#348)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmartin35 authored Apr 22, 2021
1 parent 69b2cbe commit b70520c
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 2 deletions.
32 changes: 32 additions & 0 deletions examples/cursor.go
Original file line number Diff line number Diff line change
@@ -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"])
}
6 changes: 4 additions & 2 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func defaultAskOptions() *AskOptions {
return strings.Contains(strings.ToLower(value), filter)
},
KeepFilter: false,
ShowCursor: false,
},
}
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b70520c

Please sign in to comment.