-
Notifications
You must be signed in to change notification settings - Fork 2
/
option.go
307 lines (289 loc) · 8.86 KB
/
option.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
298
299
300
301
302
303
304
305
306
307
package completer
import (
"strings"
"github.com/c-bata/go-prompt"
)
var globalOptions = []prompt.Suggest{
{Text: "--repo", Description: "Select another repository using the OWNER/REPO format"},
{Text: "-R", Description: "Select another repository using the OWNER/REPO format"},
{Text: "--help", Description: "Show help for command"},
}
func (c *Completer) optionCompleter(args []string, word string) []prompt.Suggest {
l := len(args)
long := strings.HasPrefix(word, "--")
if l <= 2 {
return prompt.FilterHasPrefix(globalOptions, word, false)
}
var suggests []prompt.Suggest
commandArgs, _ := excludeOptions(args)
switch commandArgs[0] {
case "issue":
switch commandArgs[1] {
case "create":
suggests = []prompt.Suggest{
{Text: "-b", Description: "Supply a body. Will prompt for one otherwise."},
{Text: "--body", Description: "Supply a body. Will prompt for one otherwise."},
{Text: "-t", Description: "Supply a title. Will prompt for one otherwise."},
{Text: "--title", Description: "Supply a title. Will prompt for one otherwise."},
{Text: "-w", Description: "Open the browser to create an issue"},
{Text: "--web", Description: "Open the browser to create an issue"},
}
case "list":
suggests = []prompt.Suggest{
{Text: "-a", Description: "Filter by assignee"},
{Text: "--assignee", Description: "Filter by assignee"},
{Text: "-l", Description: "Filter by label"},
{Text: "--label", Description: "Filter by label"},
{Text: "-L", Description: "Maximum number of issues to fetch (default 30)"},
{Text: "--limit", Description: "Maximum number of issues to fetch (default 30)"},
{Text: "-s", Description: "Filter by state: {open|closed|all}"},
{Text: "--state", Description: "Filter by state: {open|closed|all}"},
}
case "status":
suggests = []prompt.Suggest{}
case "view":
suggests = []prompt.Suggest{
{Text: "-p", Description: "Display preview of issue content"},
{Text: "--preview", Description: "Display preview of issue content"},
}
}
case "pr":
switch commandArgs[1] {
case "checkout":
suggests = []prompt.Suggest{}
case "create":
suggests = []prompt.Suggest{
{Text: "-B", Description: "The branch into which you want your code merged"},
{Text: "--base", Description: "The branch into which you want your code merged"},
{Text: "-b", Description: "Supply a body. Will prompt for one otherwise."},
{Text: "--body", Description: "Supply a body. Will prompt for one otherwise."},
{Text: "-d", Description: "Mark pull request as a draft"},
{Text: "--draft", Description: "Mark pull request as a draft"},
{Text: "-t", Description: "Supply a title. Will prompt for one otherwise."},
{Text: "--title", Description: "Supply a title. Will prompt for one otherwise."},
{Text: "-w", Description: "Open the web browser to create a pull request"},
{Text: "--web", Description: "Open the web browser to create a pull request"},
}
case "list":
suggests = []prompt.Suggest{
{Text: "-a", Description: "Filter by assignee"},
{Text: "--assignee", Description: "Filter by assignee"},
{Text: "-B", Description: "Filter by base branch"},
{Text: "--base", Description: "Filter by base branch"},
{Text: "-l", Description: "Filter by label"},
{Text: "--label", Description: "Filter by label"},
{Text: "-L", Description: "Maximum number of items to fetch (default 30)"},
{Text: "--limit", Description: "Maximum number of items to fetch (default 30)"},
{Text: "-s", Description: "Filter by state: {open|closed|merged|all} (default 'open')"},
{Text: "--state", Description: "Filter by state: {open|closed|merged|all} (default 'open')"},
}
case "status":
suggests = []prompt.Suggest{}
case "view":
suggests = []prompt.Suggest{
{Text: "-p", Description: "Display preview of pull request content"},
{Text: "--preview", Description: "Display preview of pull request content"},
}
}
case "repo":
switch commandArgs[1] {
case "clone":
suggests = []prompt.Suggest{}
case "create":
suggests = []prompt.Suggest{
{Text: "-d", Description: "Description of repository"},
{Text: "--description", Description: "Description of repository"},
{Text: "--enable-issues", Description: "Enable issues in the new repository (default true)"},
{Text: "--enable-wiki", Description: "Enable wiki in the new repository (default true)"},
{Text: "-h", Description: "Repository home page URL"},
{Text: "--homepage", Description: "Repository home page URL"},
{Text: "--public", Description: "Make the new repository public"},
{Text: "-t", Description: "The name of the organization team to be granted access"},
{Text: "--team", Description: "The name of the organization team to be granted access"},
}
case "fork":
suggests = []prompt.Suggest{
{Text: "--clone", Description: "Clone fork: {true|false|prompt} (default 'prompt')"},
{Text: "--remote", Description: "Add remote for fork: {true|false|prompt} (default 'prompt')"},
}
}
default:
suggests = []prompt.Suggest{}
}
suggests = append(suggests, globalOptions...)
if long {
return prompt.FilterContains(
prompt.FilterHasPrefix(suggests, "--", false),
strings.TrimLeft(args[l-1], "--"),
true,
)
}
return prompt.FilterContains(suggests, strings.TrimLeft(args[l-1], "-"), true)
}
func getPreviousOption(d prompt.Document) (cmds []string, option string, found bool) {
args := strings.Split(d.TextBeforeCursor(), " ")
l := len(args)
if l >= 2 {
option = args[l-2]
}
cmds, _ = excludeOptions(args)
if strings.HasPrefix(option, "-") {
return cmds, option, true
}
return nil, "", false
}
func (c *Completer) completeOptionArguments(d prompt.Document) ([]prompt.Suggest, bool) {
cmds, option, found := getPreviousOption(d)
if !found {
return []prompt.Suggest{}, false
}
// repository
if option == "-R" || option == "--repo" {
return prompt.FilterHasPrefix(
[]prompt.Suggest{},
d.GetWordBeforeCursor(),
true,
), true
}
switch cmds[0] {
case "issue":
if len(cmds) < 2 {
return []prompt.Suggest{}, false
}
switch cmds[1] {
case "create":
switch option {
case "-b", "--body":
return []prompt.Suggest{}, true
case "-t", "--title":
return []prompt.Suggest{}, true
}
case "list":
switch option {
case "-a", "--assignee":
return []prompt.Suggest{}, true
case "-l", "--label":
// TODO(c-bata): complete label
return []prompt.Suggest{}, true
case "-L", "--limit":
return []prompt.Suggest{}, true
case "-s", "--state":
return prompt.FilterHasPrefix(
[]prompt.Suggest{
{Text: "open"},
{Text: "closed"},
{Text: "all"},
},
d.GetWordBeforeCursor(),
true,
), true
}
}
case "pr":
if len(cmds) < 2 {
return []prompt.Suggest{}, false
}
switch cmds[1] {
case "create":
switch option {
case "-B", "--base":
return []prompt.Suggest{}, true
case "-b", "--body":
return []prompt.Suggest{}, true
case "-t", "--title":
return []prompt.Suggest{}, true
}
case "list":
switch option {
case "-a", "--assignee":
return []prompt.Suggest{}, true
case "-B", "--base":
return []prompt.Suggest{}, true
case "-l", "--label":
// TODO(c-bata): complete label
return []prompt.Suggest{}, true
case "-L", "--limit":
return []prompt.Suggest{}, true
case "-s", "--state":
return prompt.FilterHasPrefix(
[]prompt.Suggest{
{Text: "open"},
{Text: "closed"},
{Text: "merged"},
{Text: "all"},
},
d.GetWordBeforeCursor(),
true,
), true
}
}
case "repo":
if len(cmds) < 2 {
return []prompt.Suggest{}, false
}
switch cmds[1] {
case "fork":
switch option {
case "--clone":
return []prompt.Suggest{
{Text: "true"},
{Text: "false"},
{Text: "prompt"},
}, true
case "--remote":
return []prompt.Suggest{
{Text: "true"},
{Text: "false"},
{Text: "prompt"},
}, true
}
}
}
return []prompt.Suggest{}, false
}
func excludeOptions(args []string) ([]string, bool) {
l := len(args)
if l == 0 {
return nil, false
}
filtered := make([]string, 0, l)
var skipNextArg bool
for i := 0; i < len(args); i++ {
if skipNextArg {
skipNextArg = false
continue
}
for _, s := range []string{
"-b", "--body",
"-t", "--title",
"-a", "--assignee",
"-l", "--label",
"-L", "--limit",
"-s", "--state",
"-B", "--base",
"-R", "--repo",
"-d", "--description",
"--enable-issues",
"--enable-wiki",
"--homepage",
"-t", "--team",
"--clone",
"--remote",
} {
if strings.HasPrefix(args[i], s) {
if strings.Contains(args[i], "=") {
// we can specify option value like '-o=json'
skipNextArg = false
} else {
skipNextArg = true
}
continue
}
}
if strings.HasPrefix(args[i], "-") {
continue
}
filtered = append(filtered, args[i])
}
return filtered, skipNextArg
}