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

Add option to show/hide cursor when prompting user (#345) #348

Merged
merged 1 commit into from
Apr 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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