diff --git a/editor.go b/editor.go index 3242c56a..84ef243c 100644 --- a/editor.go +++ b/editor.go @@ -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 @@ -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}}` @@ -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 @@ -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 } diff --git a/editor_test.go b/editor_test.go index d9908f98..5b698cf1 100644 --- a/editor_test.go +++ b/editor_test.go @@ -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:"}, diff --git a/tests/editor.go b/tests/editor.go index 0e6f182a..20f33746 100644 --- a/tests/editor.go +++ b/tests/editor.go @@ -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() {