1
0
mirror of https://gitea.com/gitea/tea.git synced 2024-12-04 14:46:40 -05:00
tea/vendor/github.com/AlecAivazis/survey/v2/input.go
6543 7ac3ffcc1b Use Survey For Interactions With User (#186)
fixes

Use Survey For Interactions With User

Add Vendor "github.com/AlecAivazis/survey/v2"

Co-authored-by: Norwin Roosen <git@nroo.de>
Co-authored-by: 6543 <6543@obermui.de>
Reviewed-on: https://gitea.com/gitea/tea/pulls/186
Reviewed-by: techknowlogick <techknowlogick@gitea.io>
Reviewed-by: Norwin <noerw@noreply.gitea.io>
2020-10-03 02:54:09 +00:00

111 lines
2.5 KiB
Go

package survey
/*
Input is a regular text input that prints each character the user types on the screen
and accepts the input with the enter key. Response type is a string.
name := ""
prompt := &survey.Input{ Message: "What is your name?" }
survey.AskOne(prompt, &name)
*/
type Input struct {
Renderer
Message string
Default string
Help string
}
// data available to the templates when processing
type InputTemplateData struct {
Input
Answer string
ShowAnswer bool
ShowHelp bool
Config *PromptConfig
}
// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var InputQuestionTemplate = `
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if .ShowAnswer}}
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
{{- else }}
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ print .Config.HelpInput }} for help]{{color "reset"}} {{end}}
{{- if .Default}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
{{- end}}`
func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
// render the template
err := i.Render(
InputQuestionTemplate,
InputTemplateData{
Input: *i,
Config: config,
},
)
if err != nil {
return "", err
}
// start reading runes from the standard in
rr := i.NewRuneReader()
rr.SetTermMode()
defer rr.RestoreTermMode()
cursor := i.NewCursor()
line := []rune{}
// get the next line
for {
line, err = rr.ReadLine(0)
if err != nil {
return string(line), err
}
// terminal will echo the \n so we need to jump back up one row
cursor.PreviousLine(1)
if string(line) == config.HelpInput && i.Help != "" {
err = i.Render(
InputQuestionTemplate,
InputTemplateData{
Input: *i,
ShowHelp: true,
Config: config,
},
)
if err != nil {
return "", err
}
continue
}
break
}
// if the line is empty
if line == nil || len(line) == 0 {
// use the default value
return i.Default, err
}
lineStr := string(line)
i.AppendRenderedText(lineStr)
// we're done
return lineStr, err
}
func (i *Input) Cleanup(config *PromptConfig, val interface{}) error {
return i.Render(
InputQuestionTemplate,
InputTemplateData{
Input: *i,
Answer: val.(string),
ShowAnswer: true,
Config: config,
},
)
}