Skip to content

Commit ffb6b8e

Browse files
committed
move survey extension to its own package and clarify
1 parent 3100187 commit ffb6b8e

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

command/title_body_survey.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package command
33
import (
44
"github.com/AlecAivazis/survey/v2"
55
"github.com/github/gh-cli/pkg/githubtemplate"
6+
"github.com/github/gh-cli/pkg/surveyext"
67
"github.com/pkg/errors"
78
"github.com/spf13/cobra"
89
)
@@ -95,7 +96,7 @@ func titleBodySurvey(cmd *cobra.Command, providedTitle string, providedBody stri
9596
}
9697
bodyQuestion := &survey.Question{
9798
Name: "body",
98-
Prompt: &ghEditor{
99+
Prompt: &surveyext.GhEditor{
99100
Editor: &survey.Editor{
100101
Message: "Body",
101102
FileName: "*.md",
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
package command
1+
package surveyext
22

33
// This file extends survey.Editor to give it more flexible behavior. For more context, read
44
// https://github.com/github/gh-cli/issues/70
5+
// To see what we extended, search through for EXTENDED comments.
56

67
import (
78
"bytes"
@@ -18,7 +19,7 @@ import (
1819

1920
var (
2021
bom = []byte{0xef, 0xbb, 0xbf}
21-
editor = "nano"
22+
editor = "nano" // EXTENDED to switch from vim as a default editor
2223
)
2324

2425
func init() {
@@ -33,11 +34,12 @@ func init() {
3334
}
3435
}
3536

36-
type ghEditor struct {
37+
// EXTENDED to enable different prompting behavior
38+
type GhEditor struct {
3739
*survey.Editor
3840
}
3941

40-
// Templates with Color formatting. See Documentation: https://github.com/mgutz/ansi#style-format
42+
// EXTENDED to change prompt text
4143
var EditorQuestionTemplate = `
4244
{{- if .ShowHelp }}{{- color .Config.Icons.Help.Format }}{{ .Config.Icons.Help.Text }} {{ .Help }}{{color "reset"}}{{"\n"}}{{end}}
4345
{{- color .Config.Icons.Question.Format }}{{ .Config.Icons.Question.Text }} {{color "reset"}}
@@ -50,6 +52,7 @@ var EditorQuestionTemplate = `
5052
{{- color "cyan"}}[e: launch {{ .EditorName }}][enter: skip for now] {{color "reset"}}
5153
{{- end}}`
5254

55+
// EXTENDED to pass editor name (to use in prompt)
5356
type EditorTemplateData struct {
5457
survey.Editor
5558
EditorName string
@@ -59,12 +62,11 @@ type EditorTemplateData struct {
5962
Config *survey.PromptConfig
6063
}
6164

62-
// this is not being called in the embedding case and isn't consulted in the alias case because of
63-
// the incomplete overriding.
64-
func (e *ghEditor) prompt(initialValue string, config *survey.PromptConfig) (interface{}, error) {
65-
// render the template
65+
// EXTENDED to augment prompt text and keypress handling
66+
func (e *GhEditor) prompt(initialValue string, config *survey.PromptConfig) (interface{}, error) {
6667
err := e.Render(
6768
EditorQuestionTemplate,
69+
// EXTENDED to support printing editor in prompt
6870
EditorTemplateData{
6971
Editor: *e.Editor,
7072
EditorName: filepath.Base(editor),
@@ -85,6 +87,7 @@ func (e *ghEditor) prompt(initialValue string, config *survey.PromptConfig) (int
8587
defer cursor.Show()
8688

8789
for {
90+
// EXTENDED to handle the e to edit / enter to skip behavior
8891
r, _, err := rr.ReadRune()
8992
if err != nil {
9093
return "", err
@@ -105,6 +108,7 @@ func (e *ghEditor) prompt(initialValue string, config *survey.PromptConfig) (int
105108
err = e.Render(
106109
EditorQuestionTemplate,
107110
EditorTemplateData{
111+
// EXTENDED to support printing editor in prompt
108112
Editor: *e.Editor,
109113
EditorName: filepath.Base(editor),
110114
ShowHelp: true,
@@ -184,8 +188,8 @@ func (e *ghEditor) prompt(initialValue string, config *survey.PromptConfig) (int
184188
return text, nil
185189
}
186190

187-
// This is straight copypasta from survey to get our overriden prompt called.;
188-
func (e *ghEditor) Prompt(config *survey.PromptConfig) (interface{}, error) {
191+
// EXTENDED This is straight copypasta from survey to get our overriden prompt called.;
192+
func (e *GhEditor) Prompt(config *survey.PromptConfig) (interface{}, error) {
189193
initialValue := ""
190194
if e.Default != "" && e.AppendDefault {
191195
initialValue = e.Default

0 commit comments

Comments
 (0)