forked from AlecAivazis/survey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput.go
98 lines (85 loc) · 2.3 KB
/
input.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package survey
import (
"os"
"gopkg.in/AlecAivazis/survey.v1/core"
"gopkg.in/AlecAivazis/survey.v1/terminal"
)
/*
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, nil)
*/
type Input struct {
core.Renderer
Message string
Default string
Help string
}
// data available to the templates when processing
type InputTemplateData struct {
Input
Answer string
ShowAnswer bool
ShowHelp bool
}
// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
var InputQuestionTemplate = `
{{- if .ShowHelp }}{{- color "cyan"}}{{ HelpIcon }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
{{- color "green+hb"}}{{ QuestionIcon }} {{color "reset"}}
{{- color "default+hb"}}{{ .Message }} {{color "reset"}}
{{- if .ShowAnswer}}
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
{{- else }}
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ HelpInputRune }} for help]{{color "reset"}} {{end}}
{{- if .Default}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
{{- end}}`
func (i *Input) Prompt() (interface{}, error) {
// render the template
err := i.Render(
InputQuestionTemplate,
InputTemplateData{Input: *i},
)
if err != nil {
return "", err
}
// start reading runes from the standard in
rr := terminal.NewRuneReader(os.Stdin)
rr.SetTermMode()
defer rr.RestoreTermMode()
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
terminal.CursorPreviousLine(1)
if string(line) == string(core.HelpInputRune) && i.Help != "" {
err = i.Render(
InputQuestionTemplate,
InputTemplateData{Input: *i, ShowHelp: true},
)
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
}
// we're done
return string(line), err
}
func (i *Input) Cleanup(val interface{}) error {
return i.Render(
InputQuestionTemplate,
InputTemplateData{Input: *i, Answer: val.(string), ShowAnswer: true},
)
}