Skip to content

Commit 6e92a7e

Browse files
authored
Merge pull request #11 from Iilun/v2.5
V2.5
2 parents 98a88a3 + b365cf1 commit 6e92a7e

File tree

13 files changed

+614
-7
lines changed

13 files changed

+614
-7
lines changed

README.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,32 @@ prompt := &survey.Input{
125125
}
126126
survey.AskOne(prompt, &file)
127127
```
128+
#### Default
129+
130+
<img src="img/input-with-default.gif" alt="Input with default demonstration" width="100%">
131+
132+
```golang
133+
library := ""
134+
prompt := &survey.Input{
135+
Message: "My favorite go library:",
136+
Default: "survey"
137+
}
138+
survey.AskOne(prompt, &library)
139+
```
140+
141+
#### Prefill
142+
143+
<img src="img/input-with-prefill.gif" alt="Input with prefill demonstration" width="100%">
144+
145+
```golang
146+
library := ""
147+
prompt := &survey.Input{
148+
Message: "My favorite go library:",
149+
Prefill: true,
150+
Default: "survey",
151+
}
152+
survey.AskOne(prompt, &library)
153+
```
128154

129155
### Multiline
130156

@@ -244,6 +270,21 @@ prompt := &survey.MultiSelect{..., PageSize: 10}
244270
survey.AskOne(prompt, &days, survey.WithPageSize(10))
245271
```
246272

273+
### Slider
274+
275+
<img src="img/slider.gif" alt="Slider input demonstration" width="100%">
276+
277+
```golang
278+
pies := 0
279+
prompt := &survey.Slider{
280+
Message: "How many pies do you want?",
281+
Max: 50,
282+
}
283+
survey.AskOne(prompt, &pies)
284+
```
285+
286+
A slider allow to retrieve an int value from the user. It can be configured using `Min` and `Max` value. The default size is a 26 characters long slider, this can be configured using `MaxSize`.
287+
247288
### Editor
248289

249290
Launches the user's preferred editor (defined by the \$VISUAL or \$EDITOR environment variables) on a
@@ -318,6 +359,19 @@ However the user can prevent this from happening and keep the filter active for
318359
survey.AskOne(prompt, &color, survey.WithKeepFilter(true))
319360
```
320361

362+
## Disabling the filter
363+
364+
By default the filter is always enabled. To disable it, use the `WithDisableFilter` option
365+
366+
```golang
367+
&Select{
368+
Message: "Choose a color:",
369+
Options: []string{"light-green", "green", "dark-green", "red"},
370+
}
371+
372+
survey.AskOne(prompt, &color, survey.WithDisableFilter())
373+
```
374+
321375
## Validation
322376

323377
Validating individual responses for a particular question can be done by defining a

confirm.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ var ConfirmQuestionTemplate = `
3030
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
3131
{{- else }}
3232
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ .Config.HelpInput }} for help]{{color "reset"}} {{end}}
33-
{{- color "white"}}{{if .Default}}(Y/n) {{else}}(y/N) {{end}}{{color "reset"}}
33+
{{- color "gray"}}{{if .Default}}(Y/n) {{else}}(y/N) {{end}}{{color "reset"}}
3434
{{- end}}`
3535

3636
// the regex for answers

core/template.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,22 @@ var DisableColor = false
1515

1616
var TemplateFuncsWithColor = map[string]interface{}{
1717
// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
18-
"color": ansi.ColorCode,
18+
"color": color,
1919
"spaces": spaces,
2020
}
2121

22+
func color(style string) string {
23+
switch style {
24+
case "gray":
25+
// Fails on windows, only affects defaults
26+
if env256ColorSupported() {
27+
return ansi.ColorCode("8")
28+
}
29+
return ansi.ColorCode("default")
30+
default:
31+
return ansi.ColorCode(style)
32+
}
33+
}
2234
func spaces(selectorText string) string {
2335
length := 0
2436
for _, s := range selectorText {
@@ -52,6 +64,25 @@ func envColorForced() bool {
5264
return ok && val != "0"
5365
}
5466

67+
// Could probably be improved
68+
// env256ColorSupported returns if terminal supports ansi 256 colors - taken from github code: https://github.com/cli/go-gh/blob/trunk/pkg/term/env.go
69+
func env256ColorSupported() bool {
70+
return envTrueColorSupported() ||
71+
strings.Contains(os.Getenv("TERM"), "256") ||
72+
strings.Contains(os.Getenv("COLORTERM"), "256")
73+
}
74+
75+
// envTrueColorSupported returns if terminal supports true color - taken from github code: https://github.com/cli/go-gh/blob/trunk/pkg/term/env.go
76+
func envTrueColorSupported() bool {
77+
term := os.Getenv("TERM")
78+
colorterm := os.Getenv("COLORTERM")
79+
80+
return strings.Contains(term, "24bit") ||
81+
strings.Contains(term, "truecolor") ||
82+
strings.Contains(colorterm, "24bit") ||
83+
strings.Contains(colorterm, "truecolor")
84+
}
85+
5586
// RunTemplate returns two formatted strings given a template and
5687
// the data it requires. The first string returned is generated for
5788
// user-facing output and may or may not contain ANSI escape codes

editor.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ var EditorQuestionTemplate = `
5353
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
5454
{{- else }}
5555
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ .Config.HelpInput }} for help]{{color "reset"}} {{end}}
56-
{{- if and .Default (not .HideDefault)}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
56+
{{- if and .Default (not .HideDefault)}}{{color "gray"}}({{.Default}}) {{color "reset"}}{{end}}
5757
{{- color "cyan"}}[Enter to launch editor] {{color "reset"}}
5858
{{- end}}`
5959

img/input-with-default.gif

23.5 KB
Loading

img/input-with-prefill.gif

24.2 KB
Loading

img/slider.gif

94.1 KB
Loading

input.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type Input struct {
1919
Renderer
2020
Message string
2121
Default string
22+
Prefill bool
2223
Help string
2324
Suggest func(toComplete string) []string
2425
answer string
@@ -59,7 +60,7 @@ var InputQuestionTemplate = `
5960
{{- if and .Help (not .ShowHelp)}}{{ print .Config.HelpInput }} for help {{- if and .Suggest}}, {{end}}{{end -}}
6061
{{- if and .Suggest }}{{color "cyan"}}{{ print .Config.SuggestInput }} for suggestions{{end -}}
6162
]{{color "reset"}} {{end}}
62-
{{- if .Default}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
63+
{{- if and .Default (not .Prefill)}}{{color "gray"}}({{.Default}}) {{color "reset"}}{{end}}
6364
{{- end}}`
6465

6566
func (i *Input) onRune(config *PromptConfig) terminal.OnRuneFn {
@@ -165,6 +166,9 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
165166
}
166167

167168
var line []rune
169+
if i.Prefill {
170+
line = []rune(i.Default)
171+
}
168172

169173
for {
170174
if i.options != nil {
@@ -195,7 +199,7 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
195199
}
196200

197201
// if the line is empty
198-
if len(i.answer) == 0 {
202+
if len(i.answer) == 0 && !i.Prefill {
199203
// use the default value
200204
return i.Default, err
201205
}

input_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,16 @@ func TestInputRender(t *testing.T) {
102102
defaultIcons().Question.Text, defaultPromptConfig().Icons.SelectFocus.Text,
103103
),
104104
},
105+
{
106+
"Test Input question output with default prefilled",
107+
Input{Message: "What is your favorite month:", Prefill: true, Default: "January"},
108+
InputTemplateData{},
109+
fmt.Sprintf(
110+
// It is not rendered, the reader has tha value by default
111+
"%s What is your favorite month: ",
112+
defaultIcons().Question.Text,
113+
),
114+
},
105115
}
106116

107117
for _, test := range tests {
@@ -161,6 +171,37 @@ func TestInputPrompt(t *testing.T) {
161171
},
162172
"Johnny Appleseed",
163173
},
174+
{
175+
"Test Input prompt interaction with default prefilled",
176+
&Input{
177+
Message: "What is your name?",
178+
Default: "Johnny Appleseed",
179+
Prefill: true,
180+
},
181+
nil,
182+
func(c expectConsole) {
183+
c.ExpectString("What is your name?")
184+
c.SendLine("")
185+
c.ExpectEOF()
186+
},
187+
"Johnny Appleseed",
188+
},
189+
{
190+
"Test Input prompt interaction with default prefilled being modified",
191+
&Input{
192+
Message: "What is your name?",
193+
Default: "Johnny Appleseed",
194+
Prefill: true,
195+
},
196+
nil,
197+
func(c expectConsole) {
198+
c.ExpectString("What is your name?")
199+
c.Send(string(terminal.KeyDelete))
200+
c.SendLine("")
201+
c.ExpectEOF()
202+
},
203+
"Johnny Applesee",
204+
},
164205
{
165206
"Test Input prompt interaction overriding default",
166207
&Input{

multiline.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ var MultilineQuestionTemplate = `
3131
{{- "\n"}}{{color "cyan"}}{{.Answer}}{{color "reset"}}
3232
{{- if .Answer }}{{ "\n" }}{{ end }}
3333
{{- else }}
34-
{{- if .Default}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
34+
{{- if .Default}}{{color "gray"}}({{.Default}}) {{color "reset"}}{{end}}
3535
{{- color "cyan"}}[Enter 2 empty lines to finish]{{color "reset"}}
3636
{{- end}}`
3737

0 commit comments

Comments
 (0)