From 3c8b87c9c67224558c814480a3998f5ebc376e43 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 22 Apr 2020 11:23:14 -0500 Subject: [PATCH 1/3] respect configured editor --- command/title_body_survey.go | 10 +++++++++- pkg/surveyext/editor.go | 27 ++++++++++++++++++--------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/command/title_body_survey.go b/command/title_body_survey.go index 8d4dd4d47..2f6270c36 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -82,6 +82,13 @@ func selectTemplate(templatePaths []string) (string, error) { } func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, defs defaults, templatePaths []string) (*titleBody, error) { + ctx := contextForCommand(cmd) + cfg, err := ctx.Config() + if err != nil { + return nil, fmt.Errorf("could not read config: %w", err) + } + editorName, _ := cfg.Get(defaultHostname, "editor") + var inProgress titleBody inProgress.Title = defs.Title templateContents := "" @@ -109,6 +116,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, def bodyQuestion := &survey.Question{ Name: "body", Prompt: &surveyext.GhEditor{ + EditorName: editorName, Editor: &survey.Editor{ Message: "Body", FileName: "*.md", @@ -127,7 +135,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, def qs = append(qs, bodyQuestion) } - err := SurveyAsk(qs, &inProgress) + err = SurveyAsk(qs, &inProgress) if err != nil { return nil, fmt.Errorf("could not prompt: %w", err) } diff --git a/pkg/surveyext/editor.go b/pkg/surveyext/editor.go index 8a936f294..4ed0c2844 100644 --- a/pkg/surveyext/editor.go +++ b/pkg/surveyext/editor.go @@ -18,25 +18,34 @@ import ( ) var ( - bom = []byte{0xef, 0xbb, 0xbf} - editor = "nano" // EXTENDED to switch from vim as a default editor + bom = []byte{0xef, 0xbb, 0xbf} + defaultEditor = "nano" // EXTENDED to switch from vim as a default editor ) func init() { if runtime.GOOS == "windows" { - editor = "notepad" + defaultEditor = "notepad" } else if g := os.Getenv("GIT_EDITOR"); g != "" { - editor = g + defaultEditor = g } else if v := os.Getenv("VISUAL"); v != "" { - editor = v + defaultEditor = v } else if e := os.Getenv("EDITOR"); e != "" { - editor = e + defaultEditor = e } } // EXTENDED to enable different prompting behavior type GhEditor struct { *survey.Editor + EditorName string +} + +func (e *GhEditor) editorName() string { + if e.EditorName == "" { + return defaultEditor + } + + return e.EditorName } // EXTENDED to change prompt text @@ -69,7 +78,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int // EXTENDED to support printing editor in prompt EditorTemplateData{ Editor: *e.Editor, - EditorName: filepath.Base(editor), + EditorName: filepath.Base(e.editorName()), Config: config, }, ) @@ -110,7 +119,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int EditorTemplateData{ // EXTENDED to support printing editor in prompt Editor: *e.Editor, - EditorName: filepath.Base(editor), + EditorName: filepath.Base(e.editorName()), ShowHelp: true, Config: config, }, @@ -155,7 +164,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int stdio := e.Stdio() - args, err := shellquote.Split(editor) + args, err := shellquote.Split(e.editorName()) if err != nil { return "", err } From 9641eee38ac253d2468fd7e4f864eb2d1cf4cbd7 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 22 Apr 2020 14:31:09 -0500 Subject: [PATCH 2/3] use more clear name --- command/title_body_survey.go | 4 ++-- pkg/surveyext/editor.go | 36 ++++++++++++++++++------------------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/command/title_body_survey.go b/command/title_body_survey.go index 2f6270c36..c2b68ec8d 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -87,7 +87,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, def if err != nil { return nil, fmt.Errorf("could not read config: %w", err) } - editorName, _ := cfg.Get(defaultHostname, "editor") + editorCommand, _ := cfg.Get(defaultHostname, "editor") var inProgress titleBody inProgress.Title = defs.Title @@ -116,7 +116,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, def bodyQuestion := &survey.Question{ Name: "body", Prompt: &surveyext.GhEditor{ - EditorName: editorName, + EditorCommand: editorCommand, Editor: &survey.Editor{ Message: "Body", FileName: "*.md", diff --git a/pkg/surveyext/editor.go b/pkg/surveyext/editor.go index 4ed0c2844..3121ccf4d 100644 --- a/pkg/surveyext/editor.go +++ b/pkg/surveyext/editor.go @@ -37,15 +37,15 @@ func init() { // EXTENDED to enable different prompting behavior type GhEditor struct { *survey.Editor - EditorName string + EditorCommand string } -func (e *GhEditor) editorName() string { - if e.EditorName == "" { +func (e *GhEditor) editorCommand() string { + if e.EditorCommand == "" { return defaultEditor } - return e.EditorName + return e.EditorCommand } // EXTENDED to change prompt text @@ -58,17 +58,17 @@ var EditorQuestionTemplate = ` {{- 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}} - {{- color "cyan"}}[(e) to launch {{ .EditorName }}, enter to skip] {{color "reset"}} + {{- color "cyan"}}[(e) to launch {{ .EditorCommand }}, enter to skip] {{color "reset"}} {{- end}}` // EXTENDED to pass editor name (to use in prompt) type EditorTemplateData struct { survey.Editor - EditorName string - Answer string - ShowAnswer bool - ShowHelp bool - Config *survey.PromptConfig + EditorCommand string + Answer string + ShowAnswer bool + ShowHelp bool + Config *survey.PromptConfig } // EXTENDED to augment prompt text and keypress handling @@ -77,9 +77,9 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int EditorQuestionTemplate, // EXTENDED to support printing editor in prompt EditorTemplateData{ - Editor: *e.Editor, - EditorName: filepath.Base(e.editorName()), - Config: config, + Editor: *e.Editor, + EditorCommand: filepath.Base(e.editorCommand()), + Config: config, }, ) if err != nil { @@ -118,10 +118,10 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int EditorQuestionTemplate, EditorTemplateData{ // EXTENDED to support printing editor in prompt - Editor: *e.Editor, - EditorName: filepath.Base(e.editorName()), - ShowHelp: true, - Config: config, + Editor: *e.Editor, + EditorCommand: filepath.Base(e.editorCommand()), + ShowHelp: true, + Config: config, }, ) if err != nil { @@ -164,7 +164,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int stdio := e.Stdio() - args, err := shellquote.Split(e.editorName()) + args, err := shellquote.Split(e.editorCommand()) if err != nil { return "", err } From f26690adc9c7dcfcb1236f8d93136f55544ff130 Mon Sep 17 00:00:00 2001 From: vilmibm Date: Wed, 22 Apr 2020 14:33:56 -0500 Subject: [PATCH 3/3] support GH_EDITOR --- command/title_body_survey.go | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/command/title_body_survey.go b/command/title_body_survey.go index c2b68ec8d..397c27bef 100644 --- a/command/title_body_survey.go +++ b/command/title_body_survey.go @@ -2,6 +2,7 @@ package command import ( "fmt" + "os" "github.com/AlecAivazis/survey/v2" "github.com/cli/cli/pkg/githubtemplate" @@ -82,12 +83,15 @@ func selectTemplate(templatePaths []string) (string, error) { } func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, defs defaults, templatePaths []string) (*titleBody, error) { - ctx := contextForCommand(cmd) - cfg, err := ctx.Config() - if err != nil { - return nil, fmt.Errorf("could not read config: %w", err) + editorCommand := os.Getenv("GH_EDITOR") + if editorCommand == "" { + ctx := contextForCommand(cmd) + cfg, err := ctx.Config() + if err != nil { + return nil, fmt.Errorf("could not read config: %w", err) + } + editorCommand, _ = cfg.Get(defaultHostname, "editor") } - editorCommand, _ := cfg.Get(defaultHostname, "editor") var inProgress titleBody inProgress.Title = defs.Title @@ -135,7 +139,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, def qs = append(qs, bodyQuestion) } - err = SurveyAsk(qs, &inProgress) + err := SurveyAsk(qs, &inProgress) if err != nil { return nil, fmt.Errorf("could not prompt: %w", err) }