Skip to content

Commit

Permalink
Merge pull request #11 from Iilun/v2.5
Browse files Browse the repository at this point in the history
V2.5
  • Loading branch information
Iilun authored Dec 11, 2023
2 parents 98a88a3 + b365cf1 commit 6e92a7e
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 7 deletions.
54 changes: 54 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,32 @@ prompt := &survey.Input{
}
survey.AskOne(prompt, &file)
```
#### Default

<img src="img/input-with-default.gif" alt="Input with default demonstration" width="100%">

```golang
library := ""
prompt := &survey.Input{
Message: "My favorite go library:",
Default: "survey"
}
survey.AskOne(prompt, &library)
```

#### Prefill

<img src="img/input-with-prefill.gif" alt="Input with prefill demonstration" width="100%">

```golang
library := ""
prompt := &survey.Input{
Message: "My favorite go library:",
Prefill: true,
Default: "survey",
}
survey.AskOne(prompt, &library)
```

### Multiline

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

### Slider

<img src="img/slider.gif" alt="Slider input demonstration" width="100%">

```golang
pies := 0
prompt := &survey.Slider{
Message: "How many pies do you want?",
Max: 50,
}
survey.AskOne(prompt, &pies)
```

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`.

### Editor

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

## Disabling the filter

By default the filter is always enabled. To disable it, use the `WithDisableFilter` option

```golang
&Select{
Message: "Choose a color:",
Options: []string{"light-green", "green", "dark-green", "red"},
}

survey.AskOne(prompt, &color, survey.WithDisableFilter())
```

## Validation

Validating individual responses for a particular question can be done by defining a
Expand Down
2 changes: 1 addition & 1 deletion confirm.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ var ConfirmQuestionTemplate = `
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
{{- else }}
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ .Config.HelpInput }} for help]{{color "reset"}} {{end}}
{{- color "white"}}{{if .Default}}(Y/n) {{else}}(y/N) {{end}}{{color "reset"}}
{{- color "gray"}}{{if .Default}}(Y/n) {{else}}(y/N) {{end}}{{color "reset"}}
{{- end}}`

// the regex for answers
Expand Down
33 changes: 32 additions & 1 deletion core/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,22 @@ var DisableColor = false

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

func color(style string) string {
switch style {
case "gray":
// Fails on windows, only affects defaults
if env256ColorSupported() {
return ansi.ColorCode("8")
}
return ansi.ColorCode("default")
default:
return ansi.ColorCode(style)
}
}
func spaces(selectorText string) string {
length := 0
for _, s := range selectorText {
Expand Down Expand Up @@ -52,6 +64,25 @@ func envColorForced() bool {
return ok && val != "0"
}

// Could probably be improved
// env256ColorSupported returns if terminal supports ansi 256 colors - taken from github code: https://github.com/cli/go-gh/blob/trunk/pkg/term/env.go
func env256ColorSupported() bool {
return envTrueColorSupported() ||
strings.Contains(os.Getenv("TERM"), "256") ||
strings.Contains(os.Getenv("COLORTERM"), "256")
}

// envTrueColorSupported returns if terminal supports true color - taken from github code: https://github.com/cli/go-gh/blob/trunk/pkg/term/env.go
func envTrueColorSupported() bool {
term := os.Getenv("TERM")
colorterm := os.Getenv("COLORTERM")

return strings.Contains(term, "24bit") ||
strings.Contains(term, "truecolor") ||
strings.Contains(colorterm, "24bit") ||
strings.Contains(colorterm, "truecolor")
}

// RunTemplate returns two formatted strings given a template and
// the data it requires. The first string returned is generated for
// user-facing output and may or may not contain ANSI escape codes
Expand Down
2 changes: 1 addition & 1 deletion editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ var EditorQuestionTemplate = `
{{- color "cyan"}}{{.Answer}}{{color "reset"}}{{"\n"}}
{{- else }}
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ .Config.HelpInput }} for help]{{color "reset"}} {{end}}
{{- if and .Default (not .HideDefault)}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
{{- if and .Default (not .HideDefault)}}{{color "gray"}}({{.Default}}) {{color "reset"}}{{end}}
{{- color "cyan"}}[Enter to launch editor] {{color "reset"}}
{{- end}}`

Expand Down
Binary file added img/input-with-default.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/input-with-prefill.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/slider.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
8 changes: 6 additions & 2 deletions input.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Input struct {
Renderer
Message string
Default string
Prefill bool
Help string
Suggest func(toComplete string) []string
answer string
Expand Down Expand Up @@ -59,7 +60,7 @@ 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}}
{{- if and .Default (not .Prefill)}}{{color "gray"}}({{.Default}}) {{color "reset"}}{{end}}
{{- end}}`

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

var line []rune
if i.Prefill {
line = []rune(i.Default)
}

for {
if i.options != nil {
Expand Down Expand Up @@ -195,7 +199,7 @@ func (i *Input) Prompt(config *PromptConfig) (interface{}, error) {
}

// if the line is empty
if len(i.answer) == 0 {
if len(i.answer) == 0 && !i.Prefill {
// use the default value
return i.Default, err
}
Expand Down
41 changes: 41 additions & 0 deletions input_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ func TestInputRender(t *testing.T) {
defaultIcons().Question.Text, defaultPromptConfig().Icons.SelectFocus.Text,
),
},
{
"Test Input question output with default prefilled",
Input{Message: "What is your favorite month:", Prefill: true, Default: "January"},
InputTemplateData{},
fmt.Sprintf(
// It is not rendered, the reader has tha value by default
"%s What is your favorite month: ",
defaultIcons().Question.Text,
),
},
}

for _, test := range tests {
Expand Down Expand Up @@ -161,6 +171,37 @@ func TestInputPrompt(t *testing.T) {
},
"Johnny Appleseed",
},
{
"Test Input prompt interaction with default prefilled",
&Input{
Message: "What is your name?",
Default: "Johnny Appleseed",
Prefill: true,
},
nil,
func(c expectConsole) {
c.ExpectString("What is your name?")
c.SendLine("")
c.ExpectEOF()
},
"Johnny Appleseed",
},
{
"Test Input prompt interaction with default prefilled being modified",
&Input{
Message: "What is your name?",
Default: "Johnny Appleseed",
Prefill: true,
},
nil,
func(c expectConsole) {
c.ExpectString("What is your name?")
c.Send(string(terminal.KeyDelete))
c.SendLine("")
c.ExpectEOF()
},
"Johnny Applesee",
},
{
"Test Input prompt interaction overriding default",
&Input{
Expand Down
2 changes: 1 addition & 1 deletion multiline.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var MultilineQuestionTemplate = `
{{- "\n"}}{{color "cyan"}}{{.Answer}}{{color "reset"}}
{{- if .Answer }}{{ "\n" }}{{ end }}
{{- else }}
{{- if .Default}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
{{- if .Default}}{{color "gray"}}({{.Default}}) {{color "reset"}}{{end}}
{{- color "cyan"}}[Enter 2 empty lines to finish]{{color "reset"}}
{{- end}}`

Expand Down
Loading

0 comments on commit 6e92a7e

Please sign in to comment.