-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathprompt.go
297 lines (279 loc) · 6.3 KB
/
prompt.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
package gitcomm
import (
"fmt"
"log"
"os"
"strings"
"unicode"
bb "github.com/karantin2020/promptui"
)
var (
msg = Message{
Type: "feat",
Subject: "",
Body: "",
Foot: "",
}
types = []string{
"feat [new feature]",
"fix [bug fix]",
"docs [changes to documentation]",
"style [format, missing semi colons, etc; no code change]",
"refactor [refactor production code]",
"test [add missing tests, refactor tests; no production code change]",
"chore [update grunt tasks etc; no production code change]",
"version [description of version upgrade]",
}
)
func fillMessage(msg *Message) {
var err error
msg.Type, err = bb.PromptAfterSelect("Choose a type(<scope>)", types)
checkInterrupt(err)
p := bb.Prompt{
BasicPrompt: bb.BasicPrompt{
Label: "Type in the subject",
Formatter: linterSubject,
Validate: validateSubject,
},
}
msg.Subject, err = p.Run()
checkInterrupt(err)
mlBody := bb.MultilinePrompt{
BasicPrompt: bb.BasicPrompt{
Label: "Type in the body",
Default: `# If applied, this commit will...
# [Add/Fix/Remove/Update/Refactor/Document] [issue #id] [summary]
`,
Formatter: linterBody,
Validate: validateBody,
},
}
msg.Body, err = mlBody.Run()
checkInterrupt(err)
// # Why is it necessary? (Bug fix, feature, improvements?)
// -
// # How does the change address the issue?
// -
// # What side effects does this change have?
// -
mlFoot := bb.MultilinePrompt{
BasicPrompt: bb.BasicPrompt{
Label: "Type in the foot",
Formatter: linterFoot,
Validate: func(s string) error {
if s == "" {
return bb.NewValidationError("Foot must not be empty string")
}
if len(s) > 50 {
return bb.NewValidationError("Foot cannot be longer than 50 characters")
}
return nil
},
},
}
msg.Foot, err = mlFoot.Run()
checkInterrupt(err)
}
// Prompt function assignes user input to Message struct
func Prompt() string {
fillMessage(&msg)
gitMsg := msg.String() + "\n"
Info("\nCommit message is:\n%s", gitMsg)
for {
cp := bb.ConfirmPrompt{
BasicPrompt: bb.BasicPrompt{
Label: "Is everything OK? Continue",
Default: "N",
NoIcons: true,
},
ConfirmOpt: "e",
}
c, err := cp.Run()
checkConfirmStatus(c, err)
if c == "Y" {
break
}
if c == "E" {
numlines := len(strings.Split(gitMsg, "\n"))
for ; numlines > -1; numlines-- {
fmt.Print(bb.ClearUpLine())
}
gitMsg, err = bb.Editor("", gitMsg)
checkInterrupt(err)
Info(gitMsg)
// checkConfirmStatus(bb.Confirm("Is everything OK? Continue", "N", true))
// return gitMsg
continue
}
}
return gitMsg
}
// TagPrompt prompting tag version level to upgrade
func TagPrompt() string {
s := bb.Select{
Label: "Choose tag level",
Items: []string{
"patch",
"minor",
"major",
},
Default: 0,
}
_, level, err := s.Run()
checkInterrupt(err)
return level
}
// PromptConfirm is a common function to ask confirm before some action
func PromptConfirm(msg string) bool {
c, err := bb.Confirm(msg, "N", false)
checkInterrupt(err)
if c == "N" {
return false
}
return true
}
func linterSubject(s string) string {
if len(s) == 0 {
return s
}
// Remove all leading and trailing white spaces
s = strings.TrimSpace(s)
// Remove trailing period ...
s = strings.TrimSuffix(s, "...")
// Then strings.Title the first word in string
flds := strings.Fields(s)
flds[0] = strings.Title(flds[0])
return strings.Join(flds, " ")
}
func linterBody(s string) string {
if len(s) == 0 {
return s
}
// remove all leading white space
// doesn't work because there is commented message goes first
// s = strings.TrimLeft(s, "\t\n\v\f\r")
var upl = func(sl string) string {
rs := []rune(sl)
if len(rs) > 0 {
rs[0] = unicode.ToUpper(rs[0])
}
return string(rs)
}
out := []string{}
lines := strings.Split(s, "\n")
for i := range lines {
// if the line is commented with # at the start pass that line
if len(lines[i]) > 0 && lines[i][0] == '#' {
continue
}
if len(lines[i]) > 72 {
nl := wrapLine(lines[i], 72)
out = append(out, nl...)
continue
}
out = append(out, lines[i])
}
for {
if len(out) == 0 {
break
}
if strings.TrimSpace(out[0]) != "" {
out[0] = upl(out[0])
break
}
out = out[1:]
}
return strings.TrimSpace(strings.Join(out, "\n"))
}
func wrapLine(l string, n int) []string {
words := strings.Split(l, " ")
out := []string{}
s := ""
i := 0
for {
if i >= len(words) {
out = append(out, s)
break
}
separator := " "
if s == "" {
separator = ""
}
sv := s + separator + words[i]
if len(sv) >= n {
out = append(out, s)
sv = ""
i--
}
s = sv
i++
}
return out
}
func linterFoot(s string) string {
if len(s) == 0 {
return s
}
s = strings.TrimSpace(s)
// Split string to lines
strs := strings.Split(s, "\n")
out := []string{}
for i := len(strs); i > 0; i-- {
if strings.TrimSpace(strs[i-1]) == "" {
continue
}
if strings.HasPrefix(strs[i-1], "* ") {
strs[i-1] = strings.TrimPrefix(strs[i-1], "* ")
}
strs[i-1] = linterSubject(strs[i-1])
strs[i-1] = "* " + strs[i-1]
out = append(append([]string{}, strs[i-1]), out...)
}
return strings.Join(out, "\n")
}
func validator(n int) func(val interface{}) error {
return func(val interface{}) error {
// since we are validating an Input, the assertion will always succeed
if str, ok := val.(string); !ok || str == "" || len(str) > n {
return fmt.Errorf("This response cannot be longer than %d characters", n)
}
return nil
}
}
func validateBody(s string) error {
if s == "" {
return bb.NewValidationError("Body must not be empty string")
}
ins := strings.Split(s, "\n")
for i := range ins {
if len(ins[i]) > 72 {
return bb.NewValidationError("Body must be wraped at 72 characters")
}
}
return nil
}
func validateSubject(s string) error {
if s == "" {
return bb.NewValidationError("Subject must not be empty string")
}
if len(s) > 72 {
return bb.NewValidationError("Subject cannot be longer than 72 characters")
}
return nil
}
func checkInterrupt(err error) {
if err != nil {
if err != bb.ErrInterrupt {
fmt.Printf("\x1b[31;1m%s\x1b[0m\n", fmt.Sprintf("error: %s", err))
}
log.Printf("interrupted by user\n")
os.Exit(1)
}
}
func checkConfirmStatus(c string, err error) {
checkInterrupt(err)
if c == "N" {
log.Printf("Commit flow interrupted by user\n")
os.Exit(1)
}
}