Skip to content

Commit f7aea8e

Browse files
author
Nate Smith
authored
Merge pull request cli#814 from cli/respect-editor
respect configured editor
2 parents 80d7513 + f26690a commit f7aea8e

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

command/title_body_survey.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package command
22

33
import (
44
"fmt"
5+
"os"
56

67
"github.com/AlecAivazis/survey/v2"
78
"github.com/cli/cli/pkg/githubtemplate"
@@ -82,6 +83,16 @@ func selectTemplate(templatePaths []string) (string, error) {
8283
}
8384

8485
func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, defs defaults, templatePaths []string) (*titleBody, error) {
86+
editorCommand := os.Getenv("GH_EDITOR")
87+
if editorCommand == "" {
88+
ctx := contextForCommand(cmd)
89+
cfg, err := ctx.Config()
90+
if err != nil {
91+
return nil, fmt.Errorf("could not read config: %w", err)
92+
}
93+
editorCommand, _ = cfg.Get(defaultHostname, "editor")
94+
}
95+
8596
var inProgress titleBody
8697
inProgress.Title = defs.Title
8798
templateContents := ""
@@ -109,6 +120,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle, providedBody string, def
109120
bodyQuestion := &survey.Question{
110121
Name: "body",
111122
Prompt: &surveyext.GhEditor{
123+
EditorCommand: editorCommand,
112124
Editor: &survey.Editor{
113125
Message: "Body",
114126
FileName: "*.md",

pkg/surveyext/editor.go

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,34 @@ import (
1818
)
1919

2020
var (
21-
bom = []byte{0xef, 0xbb, 0xbf}
22-
editor = "nano" // EXTENDED to switch from vim as a default editor
21+
bom = []byte{0xef, 0xbb, 0xbf}
22+
defaultEditor = "nano" // EXTENDED to switch from vim as a default editor
2323
)
2424

2525
func init() {
2626
if runtime.GOOS == "windows" {
27-
editor = "notepad"
27+
defaultEditor = "notepad"
2828
} else if g := os.Getenv("GIT_EDITOR"); g != "" {
29-
editor = g
29+
defaultEditor = g
3030
} else if v := os.Getenv("VISUAL"); v != "" {
31-
editor = v
31+
defaultEditor = v
3232
} else if e := os.Getenv("EDITOR"); e != "" {
33-
editor = e
33+
defaultEditor = e
3434
}
3535
}
3636

3737
// EXTENDED to enable different prompting behavior
3838
type GhEditor struct {
3939
*survey.Editor
40+
EditorCommand string
41+
}
42+
43+
func (e *GhEditor) editorCommand() string {
44+
if e.EditorCommand == "" {
45+
return defaultEditor
46+
}
47+
48+
return e.EditorCommand
4049
}
4150

4251
// EXTENDED to change prompt text
@@ -49,17 +58,17 @@ var EditorQuestionTemplate = `
4958
{{- else }}
5059
{{- if and .Help (not .ShowHelp)}}{{color "cyan"}}[{{ .Config.HelpInput }} for help]{{color "reset"}} {{end}}
5160
{{- if and .Default (not .HideDefault)}}{{color "white"}}({{.Default}}) {{color "reset"}}{{end}}
52-
{{- color "cyan"}}[(e) to launch {{ .EditorName }}, enter to skip] {{color "reset"}}
61+
{{- color "cyan"}}[(e) to launch {{ .EditorCommand }}, enter to skip] {{color "reset"}}
5362
{{- end}}`
5463

5564
// EXTENDED to pass editor name (to use in prompt)
5665
type EditorTemplateData struct {
5766
survey.Editor
58-
EditorName string
59-
Answer string
60-
ShowAnswer bool
61-
ShowHelp bool
62-
Config *survey.PromptConfig
67+
EditorCommand string
68+
Answer string
69+
ShowAnswer bool
70+
ShowHelp bool
71+
Config *survey.PromptConfig
6372
}
6473

6574
// EXTENDED to augment prompt text and keypress handling
@@ -68,9 +77,9 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int
6877
EditorQuestionTemplate,
6978
// EXTENDED to support printing editor in prompt
7079
EditorTemplateData{
71-
Editor: *e.Editor,
72-
EditorName: filepath.Base(editor),
73-
Config: config,
80+
Editor: *e.Editor,
81+
EditorCommand: filepath.Base(e.editorCommand()),
82+
Config: config,
7483
},
7584
)
7685
if err != nil {
@@ -109,10 +118,10 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int
109118
EditorQuestionTemplate,
110119
EditorTemplateData{
111120
// EXTENDED to support printing editor in prompt
112-
Editor: *e.Editor,
113-
EditorName: filepath.Base(editor),
114-
ShowHelp: true,
115-
Config: config,
121+
Editor: *e.Editor,
122+
EditorCommand: filepath.Base(e.editorCommand()),
123+
ShowHelp: true,
124+
Config: config,
116125
},
117126
)
118127
if err != nil {
@@ -155,7 +164,7 @@ func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (int
155164

156165
stdio := e.Stdio()
157166

158-
args, err := shellquote.Split(editor)
167+
args, err := shellquote.Split(e.editorCommand())
159168
if err != nil {
160169
return "", err
161170
}

0 commit comments

Comments
 (0)