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

Allow editable default for Input prompt #421

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
4 changes: 2 additions & 2 deletions examples/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ var simpleQs = []*survey.Question{
Name: "name",
Prompt: &survey.Input{
Message: "What is your name?",
Default: "Jordan",
},
Validate: survey.Required,
},
}

Expand All @@ -28,5 +28,5 @@ func main() {
return
}
// print the answers
fmt.Printf("Your name is %s.\n", ansmap["name"])
fmt.Printf("Your name is %q.\n", ansmap["name"])
}
17 changes: 2 additions & 15 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ var InputQuestionTemplate = `
{{- if and .Help (not .ShowHelp)}}{{ print .Config.HelpInput }} for help {{- if and .Suggest}}, {{end}}{{end -}}
{{- if and .Suggest }}{{color "cyan"}}{{ print .Config.SuggestInput }} for suggestions{{end -}}
]{{color "reset"}} {{end}}
{{- if .Default}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
{{- end}}`

func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
Expand Down Expand Up @@ -162,7 +161,7 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
defer cursor.Show() // show the cursor when we're done
}

var line []rune
line := []rune(i.Default)

for {
if i.options != nil {
Expand Down Expand Up @@ -192,12 +191,6 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
return i.Prompt(config)
}

// if the line is empty
if len(i.answer) == 0 {
// use the default value
return i.Default, err
}

lineStr := i.answer

i.AppendRenderedText(lineStr)
Expand All @@ -207,20 +200,14 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
}

func (i *Input) Cleanup(config *PromptConfig, val interface{}) error {
// use the default answer when cleaning up the prompt if necessary
ans := i.answer
if ans == "" && i.Default != "" {
ans = i.Default
}

// render the cleanup
return i.Render(
InputQuestionTemplate,
InputTemplateData{
Input: *i,
ShowAnswer: true,
Config: config,
Answer: ans,
Answer: i.answer,
},
)
}