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

Commit

Permalink
added additional configuration to editor prompt (#110)
Browse files Browse the repository at this point in the history
* allow to change the editor behavior

Add two fields `HideDefault` and `AppendDefaul` to `Editor`.

If `HideDefault` is `true`, the default value is not shown
before the launch of the editor. By default this value is `false`.

If `AppendDefault` is `true`, the default value is written to the temporary
file before the launch of the editor and `Prompt()` returns the empty
string if the contents of the temporary file is empty.
By default this value is `false`.

* add tests of HideDefault and AppendDefault to tests/editor.go
  • Loading branch information
suzuki-shunsuke authored and AlecAivazis committed Nov 11, 2017
1 parent 441e63f commit 0aa8b6a
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
20 changes: 15 additions & 5 deletions editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ Response type is a string.
*/
type Editor struct {
core.Renderer
Message string
Default string
Help string
Message string
Default string
Help string
HideDefault bool
AppendDefault bool
}

// data available to the templates when processing
Expand All @@ -48,7 +50,7 @@ var EditorQuestionTemplate = `
{{- 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}}
{{- if and .Default (not .HideDefault)}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
{{- color "cyan"}}[Enter to launch editor] {{color "reset"}}
{{- end}}`

Expand Down Expand Up @@ -128,6 +130,14 @@ func (e *Editor) Prompt() (interface{}, error) {
if _, err := f.Write(bom); err != nil {
return "", err
}

// write default value
if e.Default != "" && e.AppendDefault {
if _, err := f.WriteString(e.Default); err != nil {
return "", err
}
}

// close the fd to prevent the editor unable to save file
if err := f.Close(); err != nil {
return "", err
Expand All @@ -153,7 +163,7 @@ func (e *Editor) Prompt() (interface{}, error) {
text := string(bytes.TrimPrefix(raw, bom))

// check length, return default value on empty
if len(text) == 0 {
if len(text) == 0 && !e.AppendDefault {
return e.Default, nil
}

Expand Down
6 changes: 6 additions & 0 deletions editor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ func TestEditorRender(t *testing.T) {
EditorTemplateData{},
"? What is your favorite month: (April) [Enter to launch editor] ",
},
{
"Test Editor question output with HideDefault",
Editor{Message: "What is your favorite month:", Default: "April", HideDefault: true},
EditorTemplateData{},
"? What is your favorite month: [Enter to launch editor] ",
},
{
"Test Editor answer output",
Editor{Message: "What is your favorite month:"},
Expand Down
14 changes: 14 additions & 0 deletions tests/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ var goodTable = []TestUtil.TestTableEntry{
Help: "Does this work?",
}, &answer,
},
{
"should not include the default value in the prompt", &survey.Editor{
Message: "the default value 'Hello World' should not include in the prompt",
HideDefault: true,
Default: "Hello World",
}, &answer,
},
{
"should write the default value to the temporary file before the launch of the editor", &survey.Editor{
Message: "the default value 'Hello World' is written to the temporary file before the launch of the editor",
AppendDefault: true,
Default: "Hello World",
}, &answer,
},
}

func main() {
Expand Down

0 comments on commit 0aa8b6a

Please sign in to comment.