@clerc/plugin-completions 1.0.2 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -16
- package/dist/{index.d.ts → index.d.mts} +1 -1
- package/dist/index.mjs +1028 -0
- package/package.json +8 -18
- package/dist/index.js +0 -70
package/README.md
CHANGED
|
@@ -2,23 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
[](https://www.npmjs.com/package/@clerc/plugin-completions)
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Documenation
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
```bash
|
|
10
|
-
$ npm install @clerc/plugin-completions
|
|
11
|
-
$ yarn add @clerc/plugin-completions
|
|
12
|
-
$ pnpm add @clerc/plugin-completions
|
|
13
|
-
```
|
|
14
|
-
|
|
15
|
-
## 🚀 Usage
|
|
16
|
-
|
|
17
|
-
```ts
|
|
18
|
-
import { completionsPlugin } from "@clerc/plugin-completions";
|
|
19
|
-
|
|
20
|
-
cli.use(completionsPlugin());
|
|
21
|
-
```
|
|
7
|
+
Read the [documentation](https://clerc.so1ve.dev/official-plugins/plugin-completions.html) for more details.
|
|
22
8
|
|
|
23
9
|
## 📝 License
|
|
24
10
|
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,1028 @@
|
|
|
1
|
+
import e from "node:assert";
|
|
2
|
+
import { Types, definePlugin, extractParameterInfo, normalizeFlagValue, normalizeParameterValue } from "@clerc/core";
|
|
3
|
+
|
|
4
|
+
//#region ../../node_modules/.pnpm/@bomb.sh+tab@0.0.10_cac@6.7.14_citty@0.1.6/node_modules/@bomb.sh/tab/dist/t-Chww2rEs.js
|
|
5
|
+
function t(e$1, t$1) {
|
|
6
|
+
return `#compdef ${e$1}
|
|
7
|
+
compdef _${e$1} ${e$1}
|
|
8
|
+
|
|
9
|
+
# zsh completion for ${e$1} -*- shell-script -*-
|
|
10
|
+
|
|
11
|
+
__${e$1}_debug() {
|
|
12
|
+
local file="$BASH_COMP_DEBUG_FILE"
|
|
13
|
+
if [[ -n \${file} ]]; then
|
|
14
|
+
echo "$*" >> "\${file}"
|
|
15
|
+
fi
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
_${e$1}() {
|
|
19
|
+
local shellCompDirectiveError=${a.ShellCompDirectiveError}
|
|
20
|
+
local shellCompDirectiveNoSpace=${a.ShellCompDirectiveNoSpace}
|
|
21
|
+
local shellCompDirectiveNoFileComp=${a.ShellCompDirectiveNoFileComp}
|
|
22
|
+
local shellCompDirectiveFilterFileExt=${a.ShellCompDirectiveFilterFileExt}
|
|
23
|
+
local shellCompDirectiveFilterDirs=${a.ShellCompDirectiveFilterDirs}
|
|
24
|
+
local shellCompDirectiveKeepOrder=${a.ShellCompDirectiveKeepOrder}
|
|
25
|
+
|
|
26
|
+
local lastParam lastChar flagPrefix requestComp out directive comp lastComp noSpace keepOrder
|
|
27
|
+
local -a completions
|
|
28
|
+
|
|
29
|
+
__${e$1}_debug "\\n========= starting completion logic =========="
|
|
30
|
+
__${e$1}_debug "CURRENT: \${CURRENT}, words[*]: \${words[*]}"
|
|
31
|
+
|
|
32
|
+
# The user could have moved the cursor backwards on the command-line.
|
|
33
|
+
# We need to trigger completion from the $CURRENT location, so we need
|
|
34
|
+
# to truncate the command-line ($words) up to the $CURRENT location.
|
|
35
|
+
# (We cannot use $CURSOR as its value does not work when a command is an alias.)
|
|
36
|
+
words=( "\${=words[1,CURRENT]}" )
|
|
37
|
+
__${e$1}_debug "Truncated words[*]: \${words[*]},"
|
|
38
|
+
|
|
39
|
+
lastParam=\${words[-1]}
|
|
40
|
+
lastChar=\${lastParam[-1]}
|
|
41
|
+
__${e$1}_debug "lastParam: \${lastParam}, lastChar: \${lastChar}"
|
|
42
|
+
|
|
43
|
+
# For zsh, when completing a flag with an = (e.g., ${e$1} -n=<TAB>)
|
|
44
|
+
# completions must be prefixed with the flag
|
|
45
|
+
setopt local_options BASH_REMATCH
|
|
46
|
+
if [[ "\${lastParam}" =~ '-.*=' ]]; then
|
|
47
|
+
# We are dealing with a flag with an =
|
|
48
|
+
flagPrefix="-P \${BASH_REMATCH}"
|
|
49
|
+
fi
|
|
50
|
+
|
|
51
|
+
# Prepare the command to obtain completions, ensuring arguments are quoted for eval
|
|
52
|
+
local -a args_to_quote=("\${(@)words[2,-1]}")
|
|
53
|
+
if [ "\${lastChar}" = "" ]; then
|
|
54
|
+
# If the last parameter is complete (there is a space following it)
|
|
55
|
+
# We add an extra empty parameter so we can indicate this to the go completion code.
|
|
56
|
+
__${e$1}_debug "Adding extra empty parameter"
|
|
57
|
+
args_to_quote+=("")
|
|
58
|
+
fi
|
|
59
|
+
|
|
60
|
+
# Use Zsh's (q) flag to quote each argument safely for eval
|
|
61
|
+
local quoted_args=("\${(@q)args_to_quote}")
|
|
62
|
+
|
|
63
|
+
# Join the main command and the quoted arguments into a single string for eval
|
|
64
|
+
requestComp="${t$1} complete -- \${quoted_args[*]}"
|
|
65
|
+
|
|
66
|
+
__${e$1}_debug "About to call: eval \${requestComp}"
|
|
67
|
+
|
|
68
|
+
# Use eval to handle any environment variables and such
|
|
69
|
+
out=$(eval \${requestComp} 2>/dev/null)
|
|
70
|
+
__${e$1}_debug "completion output: \${out}"
|
|
71
|
+
|
|
72
|
+
# Extract the directive integer following a : from the last line
|
|
73
|
+
local lastLine
|
|
74
|
+
while IFS='\n' read -r line; do
|
|
75
|
+
lastLine=\${line}
|
|
76
|
+
done < <(printf "%s\n" "\${out[@]}")
|
|
77
|
+
__${e$1}_debug "last line: \${lastLine}"
|
|
78
|
+
|
|
79
|
+
if [ "\${lastLine[1]}" = : ]; then
|
|
80
|
+
directive=\${lastLine[2,-1]}
|
|
81
|
+
# Remove the directive including the : and the newline
|
|
82
|
+
local suffix
|
|
83
|
+
(( suffix=\${#lastLine}+2))
|
|
84
|
+
out=\${out[1,-$suffix]}
|
|
85
|
+
else
|
|
86
|
+
# There is no directive specified. Leave $out as is.
|
|
87
|
+
__${e$1}_debug "No directive found. Setting to default"
|
|
88
|
+
directive=0
|
|
89
|
+
fi
|
|
90
|
+
|
|
91
|
+
__${e$1}_debug "directive: \${directive}"
|
|
92
|
+
__${e$1}_debug "completions: \${out}"
|
|
93
|
+
__${e$1}_debug "flagPrefix: \${flagPrefix}"
|
|
94
|
+
|
|
95
|
+
if [ $((directive & shellCompDirectiveError)) -ne 0 ]; then
|
|
96
|
+
__${e$1}_debug "Completion received error. Ignoring completions."
|
|
97
|
+
return
|
|
98
|
+
fi
|
|
99
|
+
|
|
100
|
+
local activeHelpMarker="%"
|
|
101
|
+
local endIndex=\${#activeHelpMarker}
|
|
102
|
+
local startIndex=$((\${#activeHelpMarker}+1))
|
|
103
|
+
local hasActiveHelp=0
|
|
104
|
+
while IFS='\n' read -r comp; do
|
|
105
|
+
# Check if this is an activeHelp statement (i.e., prefixed with $activeHelpMarker)
|
|
106
|
+
if [ "\${comp[1,$endIndex]}" = "$activeHelpMarker" ];then
|
|
107
|
+
__${e$1}_debug "ActiveHelp found: $comp"
|
|
108
|
+
comp="\${comp[$startIndex,-1]}"
|
|
109
|
+
if [ -n "$comp" ]; then
|
|
110
|
+
compadd -x "\${comp}"
|
|
111
|
+
__${e$1}_debug "ActiveHelp will need delimiter"
|
|
112
|
+
hasActiveHelp=1
|
|
113
|
+
fi
|
|
114
|
+
continue
|
|
115
|
+
fi
|
|
116
|
+
|
|
117
|
+
if [ -n "$comp" ]; then
|
|
118
|
+
# If requested, completions are returned with a description.
|
|
119
|
+
# The description is preceded by a TAB character.
|
|
120
|
+
# For zsh's _describe, we need to use a : instead of a TAB.
|
|
121
|
+
# We first need to escape any : as part of the completion itself.
|
|
122
|
+
comp=\${comp//:/\\:}
|
|
123
|
+
|
|
124
|
+
local tab="$(printf '\\t')"
|
|
125
|
+
comp=\${comp//$tab/:}
|
|
126
|
+
|
|
127
|
+
__${e$1}_debug "Adding completion: \${comp}"
|
|
128
|
+
completions+=\${comp}
|
|
129
|
+
lastComp=$comp
|
|
130
|
+
fi
|
|
131
|
+
done < <(printf "%s\n" "\${out[@]}")
|
|
132
|
+
|
|
133
|
+
# Add a delimiter after the activeHelp statements, but only if:
|
|
134
|
+
# - there are completions following the activeHelp statements, or
|
|
135
|
+
# - file completion will be performed (so there will be choices after the activeHelp)
|
|
136
|
+
if [ $hasActiveHelp -eq 1 ]; then
|
|
137
|
+
if [ \${#completions} -ne 0 ] || [ $((directive & shellCompDirectiveNoFileComp)) -eq 0 ]; then
|
|
138
|
+
__${e$1}_debug "Adding activeHelp delimiter"
|
|
139
|
+
compadd -x "--"
|
|
140
|
+
hasActiveHelp=0
|
|
141
|
+
fi
|
|
142
|
+
fi
|
|
143
|
+
|
|
144
|
+
if [ $((directive & shellCompDirectiveNoSpace)) -ne 0 ]; then
|
|
145
|
+
__${e$1}_debug "Activating nospace."
|
|
146
|
+
noSpace="-S ''"
|
|
147
|
+
fi
|
|
148
|
+
|
|
149
|
+
if [ $((directive & shellCompDirectiveKeepOrder)) -ne 0 ]; then
|
|
150
|
+
__${e$1}_debug "Activating keep order."
|
|
151
|
+
keepOrder="-V"
|
|
152
|
+
fi
|
|
153
|
+
|
|
154
|
+
if [ $((directive & shellCompDirectiveFilterFileExt)) -ne 0 ]; then
|
|
155
|
+
# File extension filtering
|
|
156
|
+
local filteringCmd
|
|
157
|
+
filteringCmd='_files'
|
|
158
|
+
for filter in \${completions[@]}; do
|
|
159
|
+
if [ \${filter[1]} != '*' ]; then
|
|
160
|
+
# zsh requires a glob pattern to do file filtering
|
|
161
|
+
filter="\\*.$filter"
|
|
162
|
+
fi
|
|
163
|
+
filteringCmd+=" -g $filter"
|
|
164
|
+
done
|
|
165
|
+
filteringCmd+=" \${flagPrefix}"
|
|
166
|
+
|
|
167
|
+
__${e$1}_debug "File filtering command: $filteringCmd"
|
|
168
|
+
_arguments '*:filename:'"$filteringCmd"
|
|
169
|
+
elif [ $((directive & shellCompDirectiveFilterDirs)) -ne 0 ]; then
|
|
170
|
+
# File completion for directories only
|
|
171
|
+
local subdir
|
|
172
|
+
subdir="\${completions[1]}"
|
|
173
|
+
if [ -n "$subdir" ]; then
|
|
174
|
+
__${e$1}_debug "Listing directories in $subdir"
|
|
175
|
+
pushd "\${subdir}" >/dev/null 2>&1
|
|
176
|
+
else
|
|
177
|
+
__${e$1}_debug "Listing directories in ."
|
|
178
|
+
fi
|
|
179
|
+
|
|
180
|
+
local result
|
|
181
|
+
_arguments '*:dirname:_files -/'" \${flagPrefix}"
|
|
182
|
+
result=$?
|
|
183
|
+
if [ -n "$subdir" ]; then
|
|
184
|
+
popd >/dev/null 2>&1
|
|
185
|
+
fi
|
|
186
|
+
return $result
|
|
187
|
+
else
|
|
188
|
+
__${e$1}_debug "Calling _describe"
|
|
189
|
+
if eval _describe $keepOrder "completions" completions -Q \${flagPrefix} \${noSpace}; then
|
|
190
|
+
__${e$1}_debug "_describe found some completions"
|
|
191
|
+
|
|
192
|
+
# Return the success of having called _describe
|
|
193
|
+
return 0
|
|
194
|
+
else
|
|
195
|
+
__${e$1}_debug "_describe did not find completions."
|
|
196
|
+
__${e$1}_debug "Checking if we should do file completion."
|
|
197
|
+
if [ $((directive & shellCompDirectiveNoFileComp)) -ne 0 ]; then
|
|
198
|
+
__${e$1}_debug "deactivating file completion"
|
|
199
|
+
|
|
200
|
+
# Return 0 to indicate completion is finished and prevent zsh from
|
|
201
|
+
# trying other completion algorithms (which could cause hanging).
|
|
202
|
+
# We use NoFileComp directive to explicitly disable file completion.
|
|
203
|
+
return 0
|
|
204
|
+
else
|
|
205
|
+
# Perform file completion
|
|
206
|
+
__${e$1}_debug "Activating file completion"
|
|
207
|
+
|
|
208
|
+
# We must return the result of this command, so it must be the
|
|
209
|
+
# last command, or else we must store its result to return it.
|
|
210
|
+
_arguments '*:filename:_files'" \${flagPrefix}"
|
|
211
|
+
fi
|
|
212
|
+
fi
|
|
213
|
+
fi
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
# don't run the completion function when being sourced or eval-ed
|
|
217
|
+
if [ "\${funcstack[1]}" = "_${e$1}" ]; then
|
|
218
|
+
_${e$1}
|
|
219
|
+
fi
|
|
220
|
+
`;
|
|
221
|
+
}
|
|
222
|
+
function n(e$1, t$1) {
|
|
223
|
+
let n$1 = e$1.replace(/[-:]/g, `_`);
|
|
224
|
+
return `# bash completion for ${e$1}
|
|
225
|
+
|
|
226
|
+
# Define shell completion directives
|
|
227
|
+
readonly ShellCompDirectiveError=${a.ShellCompDirectiveError}
|
|
228
|
+
readonly ShellCompDirectiveNoSpace=${a.ShellCompDirectiveNoSpace}
|
|
229
|
+
readonly ShellCompDirectiveNoFileComp=${a.ShellCompDirectiveNoFileComp}
|
|
230
|
+
readonly ShellCompDirectiveFilterFileExt=${a.ShellCompDirectiveFilterFileExt}
|
|
231
|
+
readonly ShellCompDirectiveFilterDirs=${a.ShellCompDirectiveFilterDirs}
|
|
232
|
+
readonly ShellCompDirectiveKeepOrder=${a.ShellCompDirectiveKeepOrder}
|
|
233
|
+
|
|
234
|
+
# Function to debug completion
|
|
235
|
+
__${n$1}_debug() {
|
|
236
|
+
if [[ -n \${BASH_COMP_DEBUG_FILE:-} ]]; then
|
|
237
|
+
echo "$*" >> "\${BASH_COMP_DEBUG_FILE}"
|
|
238
|
+
fi
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
# Function to handle completions
|
|
242
|
+
__${n$1}_complete() {
|
|
243
|
+
local cur prev words cword
|
|
244
|
+
_get_comp_words_by_ref -n "=:" cur prev words cword
|
|
245
|
+
|
|
246
|
+
local requestComp out directive
|
|
247
|
+
|
|
248
|
+
# Build the command to get completions
|
|
249
|
+
requestComp="${t$1} complete -- \${words[@]:1}"
|
|
250
|
+
|
|
251
|
+
# Add an empty parameter if the last parameter is complete
|
|
252
|
+
if [[ -z "$cur" ]]; then
|
|
253
|
+
requestComp="$requestComp ''"
|
|
254
|
+
fi
|
|
255
|
+
|
|
256
|
+
# Get completions from the program
|
|
257
|
+
out=$(eval "$requestComp" 2>/dev/null)
|
|
258
|
+
|
|
259
|
+
# Extract directive if present
|
|
260
|
+
directive=0
|
|
261
|
+
if [[ "$out" == *:* ]]; then
|
|
262
|
+
directive=\${out##*:}
|
|
263
|
+
out=\${out%:*}
|
|
264
|
+
fi
|
|
265
|
+
|
|
266
|
+
# Process completions based on directive
|
|
267
|
+
if [[ $((directive & $ShellCompDirectiveError)) -ne 0 ]]; then
|
|
268
|
+
# Error, no completion
|
|
269
|
+
return
|
|
270
|
+
fi
|
|
271
|
+
|
|
272
|
+
# Apply directives
|
|
273
|
+
if [[ $((directive & $ShellCompDirectiveNoSpace)) -ne 0 ]]; then
|
|
274
|
+
compopt -o nospace
|
|
275
|
+
fi
|
|
276
|
+
if [[ $((directive & $ShellCompDirectiveKeepOrder)) -ne 0 ]]; then
|
|
277
|
+
compopt -o nosort
|
|
278
|
+
fi
|
|
279
|
+
if [[ $((directive & $ShellCompDirectiveNoFileComp)) -ne 0 ]]; then
|
|
280
|
+
compopt +o default
|
|
281
|
+
fi
|
|
282
|
+
|
|
283
|
+
# Handle file extension filtering
|
|
284
|
+
if [[ $((directive & $ShellCompDirectiveFilterFileExt)) -ne 0 ]]; then
|
|
285
|
+
local filter=""
|
|
286
|
+
for ext in $out; do
|
|
287
|
+
filter="$filter|$ext"
|
|
288
|
+
done
|
|
289
|
+
filter="\\.($filter)"
|
|
290
|
+
compopt -o filenames
|
|
291
|
+
COMPREPLY=( $(compgen -f -X "!$filter" -- "$cur") )
|
|
292
|
+
return
|
|
293
|
+
fi
|
|
294
|
+
|
|
295
|
+
# Handle directory filtering
|
|
296
|
+
if [[ $((directive & $ShellCompDirectiveFilterDirs)) -ne 0 ]]; then
|
|
297
|
+
compopt -o dirnames
|
|
298
|
+
COMPREPLY=( $(compgen -d -- "$cur") )
|
|
299
|
+
return
|
|
300
|
+
fi
|
|
301
|
+
|
|
302
|
+
# Process completions
|
|
303
|
+
local IFS=$'\\n'
|
|
304
|
+
local tab=$(printf '\\t')
|
|
305
|
+
|
|
306
|
+
# Parse completions with descriptions
|
|
307
|
+
local completions=()
|
|
308
|
+
while read -r comp; do
|
|
309
|
+
if [[ "$comp" == *$tab* ]]; then
|
|
310
|
+
# Split completion and description
|
|
311
|
+
local value=\${comp%%$tab*}
|
|
312
|
+
local desc=\${comp#*$tab}
|
|
313
|
+
completions+=("$value")
|
|
314
|
+
else
|
|
315
|
+
completions+=("$comp")
|
|
316
|
+
fi
|
|
317
|
+
done <<< "$out"
|
|
318
|
+
|
|
319
|
+
# Return completions
|
|
320
|
+
COMPREPLY=( $(compgen -W "\${completions[*]}" -- "$cur") )
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
# Register completion function
|
|
324
|
+
complete -F __${n$1}_complete ${e$1}
|
|
325
|
+
`;
|
|
326
|
+
}
|
|
327
|
+
function r(e$1, t$1) {
|
|
328
|
+
let n$1 = e$1.replace(/[-:]/g, `_`);
|
|
329
|
+
return `# fish completion for ${e$1} -*- shell-script -*-
|
|
330
|
+
|
|
331
|
+
# Define shell completion directives
|
|
332
|
+
set -l ShellCompDirectiveError ${a.ShellCompDirectiveError}
|
|
333
|
+
set -l ShellCompDirectiveNoSpace ${a.ShellCompDirectiveNoSpace}
|
|
334
|
+
set -l ShellCompDirectiveNoFileComp ${a.ShellCompDirectiveNoFileComp}
|
|
335
|
+
set -l ShellCompDirectiveFilterFileExt ${a.ShellCompDirectiveFilterFileExt}
|
|
336
|
+
set -l ShellCompDirectiveFilterDirs ${a.ShellCompDirectiveFilterDirs}
|
|
337
|
+
set -l ShellCompDirectiveKeepOrder ${a.ShellCompDirectiveKeepOrder}
|
|
338
|
+
|
|
339
|
+
function __${n$1}_debug
|
|
340
|
+
set -l file "$BASH_COMP_DEBUG_FILE"
|
|
341
|
+
if test -n "$file"
|
|
342
|
+
echo "$argv" >> $file
|
|
343
|
+
end
|
|
344
|
+
end
|
|
345
|
+
|
|
346
|
+
function __${n$1}_perform_completion
|
|
347
|
+
__${n$1}_debug "Starting __${n$1}_perform_completion"
|
|
348
|
+
|
|
349
|
+
# Extract all args except the completion flag
|
|
350
|
+
set -l args (string match -v -- "--completion=" (commandline -opc))
|
|
351
|
+
|
|
352
|
+
# Extract the current token being completed
|
|
353
|
+
set -l current_token (commandline -ct)
|
|
354
|
+
|
|
355
|
+
# Check if current token starts with a dash
|
|
356
|
+
set -l flag_prefix ""
|
|
357
|
+
if string match -q -- "-*" $current_token
|
|
358
|
+
set flag_prefix "--flag="
|
|
359
|
+
end
|
|
360
|
+
|
|
361
|
+
__${n$1}_debug "Current token: $current_token"
|
|
362
|
+
__${n$1}_debug "All args: $args"
|
|
363
|
+
|
|
364
|
+
# Call the completion program and get the results
|
|
365
|
+
set -l requestComp "${t$1} complete -- $args"
|
|
366
|
+
__${n$1}_debug "Calling $requestComp"
|
|
367
|
+
set -l results (eval $requestComp 2> /dev/null)
|
|
368
|
+
|
|
369
|
+
# Some programs may output extra empty lines after the directive.
|
|
370
|
+
# Let's ignore them or else it will break completion.
|
|
371
|
+
# Ref: https://github.com/spf13/cobra/issues/1279
|
|
372
|
+
for line in $results[-1..1]
|
|
373
|
+
if test (string sub -s 1 -l 1 -- $line) = ":"
|
|
374
|
+
# The directive
|
|
375
|
+
set -l directive (string sub -s 2 -- $line)
|
|
376
|
+
set -l directive_num (math $directive)
|
|
377
|
+
break
|
|
378
|
+
end
|
|
379
|
+
end
|
|
380
|
+
|
|
381
|
+
# No directive specified, use default
|
|
382
|
+
if not set -q directive_num
|
|
383
|
+
set directive_num 0
|
|
384
|
+
end
|
|
385
|
+
|
|
386
|
+
__${n$1}_debug "Directive: $directive_num"
|
|
387
|
+
|
|
388
|
+
# Process completions based on directive
|
|
389
|
+
if test $directive_num -eq $ShellCompDirectiveError
|
|
390
|
+
# Error code. No completion.
|
|
391
|
+
__${n$1}_debug "Received error directive: aborting."
|
|
392
|
+
return 1
|
|
393
|
+
end
|
|
394
|
+
|
|
395
|
+
# Filter out the directive (last line)
|
|
396
|
+
if test (count $results) -gt 0 -a (string sub -s 1 -l 1 -- $results[-1]) = ":"
|
|
397
|
+
set results $results[1..-2]
|
|
398
|
+
end
|
|
399
|
+
|
|
400
|
+
# No completions, let fish handle file completions unless forbidden
|
|
401
|
+
if test (count $results) -eq 0
|
|
402
|
+
if test $directive_num -ne $ShellCompDirectiveNoFileComp
|
|
403
|
+
__${n$1}_debug "No completions, performing file completion"
|
|
404
|
+
return 1
|
|
405
|
+
end
|
|
406
|
+
__${n$1}_debug "No completions, but file completion forbidden"
|
|
407
|
+
return 0
|
|
408
|
+
end
|
|
409
|
+
|
|
410
|
+
# Filter file extensions
|
|
411
|
+
if test $directive_num -eq $ShellCompDirectiveFilterFileExt
|
|
412
|
+
__${n$1}_debug "File extension filtering"
|
|
413
|
+
set -l file_extensions
|
|
414
|
+
for item in $results
|
|
415
|
+
if test -n "$item" -a (string sub -s 1 -l 1 -- $item) != "-"
|
|
416
|
+
set -a file_extensions "*$item"
|
|
417
|
+
end
|
|
418
|
+
end
|
|
419
|
+
__${n$1}_debug "File extensions: $file_extensions"
|
|
420
|
+
|
|
421
|
+
# Use the file extensions as completions
|
|
422
|
+
set -l completions
|
|
423
|
+
for ext in $file_extensions
|
|
424
|
+
# Get all files matching the extension
|
|
425
|
+
set -a completions (string replace -r '^.*/' '' -- $ext)
|
|
426
|
+
end
|
|
427
|
+
|
|
428
|
+
for item in $completions
|
|
429
|
+
echo -e "$item\t"
|
|
430
|
+
end
|
|
431
|
+
return 0
|
|
432
|
+
end
|
|
433
|
+
|
|
434
|
+
# Filter directories
|
|
435
|
+
if test $directive_num -eq $ShellCompDirectiveFilterDirs
|
|
436
|
+
__${n$1}_debug "Directory filtering"
|
|
437
|
+
set -l dirs
|
|
438
|
+
for item in $results
|
|
439
|
+
if test -d "$item"
|
|
440
|
+
set -a dirs "$item/"
|
|
441
|
+
end
|
|
442
|
+
end
|
|
443
|
+
|
|
444
|
+
for item in $dirs
|
|
445
|
+
echo -e "$item\t"
|
|
446
|
+
end
|
|
447
|
+
return 0
|
|
448
|
+
end
|
|
449
|
+
|
|
450
|
+
# Process remaining completions
|
|
451
|
+
for item in $results
|
|
452
|
+
if test -n "$item"
|
|
453
|
+
# Check if the item has a description
|
|
454
|
+
if string match -q "*\t*" -- "$item"
|
|
455
|
+
set -l completion_parts (string split \t -- "$item")
|
|
456
|
+
set -l comp $completion_parts[1]
|
|
457
|
+
set -l desc $completion_parts[2]
|
|
458
|
+
|
|
459
|
+
# Add the completion and description
|
|
460
|
+
echo -e "$comp\t$desc"
|
|
461
|
+
else
|
|
462
|
+
# Add just the completion
|
|
463
|
+
echo -e "$item\t"
|
|
464
|
+
end
|
|
465
|
+
end
|
|
466
|
+
end
|
|
467
|
+
|
|
468
|
+
# If directive contains NoSpace, tell fish not to add a space after completion
|
|
469
|
+
if test (math "$directive_num & $ShellCompDirectiveNoSpace") -ne 0
|
|
470
|
+
return 2
|
|
471
|
+
end
|
|
472
|
+
|
|
473
|
+
return 0
|
|
474
|
+
end
|
|
475
|
+
|
|
476
|
+
# Set up the completion for the ${e$1} command
|
|
477
|
+
complete -c ${e$1} -f -a "(eval __${n$1}_perform_completion)"
|
|
478
|
+
`;
|
|
479
|
+
}
|
|
480
|
+
function i(e$1, t$1) {
|
|
481
|
+
let n$1 = e$1.replace(/[-:]/g, `_`);
|
|
482
|
+
return `# powershell completion for ${e$1} -*- shell-script -*-
|
|
483
|
+
|
|
484
|
+
[Console]::OutputEncoding = [System.Text.Encoding]::UTF8
|
|
485
|
+
function __${e$1}_debug {
|
|
486
|
+
if ($env:BASH_COMP_DEBUG_FILE) {
|
|
487
|
+
"$args" | Out-File -Append -FilePath "$env:BASH_COMP_DEBUG_FILE"
|
|
488
|
+
}
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
filter __${e$1}_escapeStringWithSpecialChars {
|
|
492
|
+
$_ -replace '\\s|#|@|\\$|;|,|''|\\{|\\}|\\(|\\)|"|\\||<|>|&','\`$&'
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
[scriptblock]$__${n$1}CompleterBlock = {
|
|
496
|
+
param(
|
|
497
|
+
$WordToComplete,
|
|
498
|
+
$CommandAst,
|
|
499
|
+
$CursorPosition
|
|
500
|
+
)
|
|
501
|
+
|
|
502
|
+
# Get the current command line and convert into a string
|
|
503
|
+
$Command = $CommandAst.CommandElements
|
|
504
|
+
$Command = "$Command"
|
|
505
|
+
|
|
506
|
+
__${e$1}_debug ""
|
|
507
|
+
__${e$1}_debug "========= starting completion logic =========="
|
|
508
|
+
__${e$1}_debug "WordToComplete: $WordToComplete Command: $Command CursorPosition: $CursorPosition"
|
|
509
|
+
|
|
510
|
+
# The user could have moved the cursor backwards on the command-line.
|
|
511
|
+
# We need to trigger completion from the $CursorPosition location, so we need
|
|
512
|
+
# to truncate the command-line ($Command) up to the $CursorPosition location.
|
|
513
|
+
# Make sure the $Command is longer then the $CursorPosition before we truncate.
|
|
514
|
+
# This happens because the $Command does not include the last space.
|
|
515
|
+
if ($Command.Length -gt $CursorPosition) {
|
|
516
|
+
$Command = $Command.Substring(0, $CursorPosition)
|
|
517
|
+
}
|
|
518
|
+
__${e$1}_debug "Truncated command: $Command"
|
|
519
|
+
|
|
520
|
+
$ShellCompDirectiveError=${a.ShellCompDirectiveError}
|
|
521
|
+
$ShellCompDirectiveNoSpace=${a.ShellCompDirectiveNoSpace}
|
|
522
|
+
$ShellCompDirectiveNoFileComp=${a.ShellCompDirectiveNoFileComp}
|
|
523
|
+
$ShellCompDirectiveFilterFileExt=${a.ShellCompDirectiveFilterFileExt}
|
|
524
|
+
$ShellCompDirectiveFilterDirs=${a.ShellCompDirectiveFilterDirs}
|
|
525
|
+
$ShellCompDirectiveKeepOrder=${a.ShellCompDirectiveKeepOrder}
|
|
526
|
+
|
|
527
|
+
# Prepare the command to request completions for the program.
|
|
528
|
+
# Split the command at the first space to separate the program and arguments.
|
|
529
|
+
$Program, $Arguments = $Command.Split(" ", 2)
|
|
530
|
+
|
|
531
|
+
$RequestComp = "& ${t$1} complete -- $Arguments"
|
|
532
|
+
__${e$1}_debug "RequestComp: $RequestComp"
|
|
533
|
+
|
|
534
|
+
# we cannot use $WordToComplete because it
|
|
535
|
+
# has the wrong values if the cursor was moved
|
|
536
|
+
# so use the last argument
|
|
537
|
+
if ($WordToComplete -ne "" ) {
|
|
538
|
+
$WordToComplete = $Arguments.Split(" ")[-1]
|
|
539
|
+
}
|
|
540
|
+
__${e$1}_debug "New WordToComplete: $WordToComplete"
|
|
541
|
+
|
|
542
|
+
|
|
543
|
+
# Check for flag with equal sign
|
|
544
|
+
$IsEqualFlag = ($WordToComplete -Like "--*=*" )
|
|
545
|
+
if ( $IsEqualFlag ) {
|
|
546
|
+
__${e$1}_debug "Completing equal sign flag"
|
|
547
|
+
# Remove the flag part
|
|
548
|
+
$Flag, $WordToComplete = $WordToComplete.Split("=", 2)
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
if ( $WordToComplete -eq "" -And ( -Not $IsEqualFlag )) {
|
|
552
|
+
# If the last parameter is complete (there is a space following it)
|
|
553
|
+
# We add an extra empty parameter so we can indicate this to the go method.
|
|
554
|
+
__${e$1}_debug "Adding extra empty parameter"
|
|
555
|
+
# PowerShell 7.2+ changed the way how the arguments are passed to executables,
|
|
556
|
+
# so for pre-7.2 or when Legacy argument passing is enabled we need to use
|
|
557
|
+
if ($PSVersionTable.PsVersion -lt [version]'7.2.0' -or
|
|
558
|
+
($PSVersionTable.PsVersion -lt [version]'7.3.0' -and -not [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -or
|
|
559
|
+
(($PSVersionTable.PsVersion -ge [version]'7.3.0' -or [ExperimentalFeature]::IsEnabled("PSNativeCommandArgumentPassing")) -and
|
|
560
|
+
$PSNativeCommandArgumentPassing -eq 'Legacy')) {
|
|
561
|
+
$RequestComp="$RequestComp" + ' \`"\`"'
|
|
562
|
+
} else {
|
|
563
|
+
$RequestComp = "$RequestComp" + ' ""'
|
|
564
|
+
}
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
__${e$1}_debug "Calling $RequestComp"
|
|
568
|
+
# First disable ActiveHelp which is not supported for Powershell
|
|
569
|
+
$env:ActiveHelp = 0
|
|
570
|
+
|
|
571
|
+
# call the command store the output in $out and redirect stderr and stdout to null
|
|
572
|
+
# $Out is an array contains each line per element
|
|
573
|
+
Invoke-Expression -OutVariable out "$RequestComp" 2>&1 | Out-Null
|
|
574
|
+
|
|
575
|
+
# get directive from last line
|
|
576
|
+
[int]$Directive = $Out[-1].TrimStart(':')
|
|
577
|
+
if ($Directive -eq "") {
|
|
578
|
+
# There is no directive specified
|
|
579
|
+
$Directive = 0
|
|
580
|
+
}
|
|
581
|
+
__${e$1}_debug "The completion directive is: $Directive"
|
|
582
|
+
|
|
583
|
+
# remove directive (last element) from out
|
|
584
|
+
$Out = $Out | Where-Object { $_ -ne $Out[-1] }
|
|
585
|
+
__${e$1}_debug "The completions are: $Out"
|
|
586
|
+
|
|
587
|
+
if (($Directive -band $ShellCompDirectiveError) -ne 0 ) {
|
|
588
|
+
# Error code. No completion.
|
|
589
|
+
__${e$1}_debug "Received error from custom completion go code"
|
|
590
|
+
return
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
$Longest = 0
|
|
594
|
+
[Array]$Values = $Out | ForEach-Object {
|
|
595
|
+
# Split the output in name and description
|
|
596
|
+
$Name, $Description = $_.Split("\`t", 2)
|
|
597
|
+
__${e$1}_debug "Name: $Name Description: $Description"
|
|
598
|
+
|
|
599
|
+
# Look for the longest completion so that we can format things nicely
|
|
600
|
+
if ($Longest -lt $Name.Length) {
|
|
601
|
+
$Longest = $Name.Length
|
|
602
|
+
}
|
|
603
|
+
|
|
604
|
+
# Set the description to a one space string if there is none set.
|
|
605
|
+
# This is needed because the CompletionResult does not accept an empty string as argument
|
|
606
|
+
if (-Not $Description) {
|
|
607
|
+
$Description = " "
|
|
608
|
+
}
|
|
609
|
+
@{ Name = "$Name"; Description = "$Description" }
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
|
|
613
|
+
$Space = " "
|
|
614
|
+
if (($Directive -band $ShellCompDirectiveNoSpace) -ne 0 ) {
|
|
615
|
+
# remove the space here
|
|
616
|
+
__${e$1}_debug "ShellCompDirectiveNoSpace is called"
|
|
617
|
+
$Space = ""
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
if ((($Directive -band $ShellCompDirectiveFilterFileExt) -ne 0 ) -or
|
|
621
|
+
(($Directive -band $ShellCompDirectiveFilterDirs) -ne 0 )) {
|
|
622
|
+
__${e$1}_debug "ShellCompDirectiveFilterFileExt ShellCompDirectiveFilterDirs are not supported"
|
|
623
|
+
|
|
624
|
+
# return here to prevent the completion of the extensions
|
|
625
|
+
return
|
|
626
|
+
}
|
|
627
|
+
|
|
628
|
+
$Values = $Values | Where-Object {
|
|
629
|
+
# filter the result
|
|
630
|
+
$_.Name -like "$WordToComplete*"
|
|
631
|
+
|
|
632
|
+
# Join the flag back if we have an equal sign flag
|
|
633
|
+
if ( $IsEqualFlag ) {
|
|
634
|
+
__${e$1}_debug "Join the equal sign flag back to the completion value"
|
|
635
|
+
$_.Name = $Flag + "=" + $_.Name
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
# we sort the values in ascending order by name if keep order isn't passed
|
|
640
|
+
if (($Directive -band $ShellCompDirectiveKeepOrder) -eq 0 ) {
|
|
641
|
+
$Values = $Values | Sort-Object -Property Name
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
if (($Directive -band $ShellCompDirectiveNoFileComp) -ne 0 ) {
|
|
645
|
+
__${e$1}_debug "ShellCompDirectiveNoFileComp is called"
|
|
646
|
+
|
|
647
|
+
if ($Values.Length -eq 0) {
|
|
648
|
+
# Just print an empty string here so the
|
|
649
|
+
# shell does not start to complete paths.
|
|
650
|
+
# We cannot use CompletionResult here because
|
|
651
|
+
# it does not accept an empty string as argument.
|
|
652
|
+
""
|
|
653
|
+
return
|
|
654
|
+
}
|
|
655
|
+
}
|
|
656
|
+
|
|
657
|
+
# Get the current mode
|
|
658
|
+
$Mode = (Get-PSReadLineKeyHandler | Where-Object { $_.Key -eq "Tab" }).Function
|
|
659
|
+
__${e$1}_debug "Mode: $Mode"
|
|
660
|
+
|
|
661
|
+
$Values | ForEach-Object {
|
|
662
|
+
|
|
663
|
+
# store temporary because switch will overwrite $_
|
|
664
|
+
$comp = $_
|
|
665
|
+
|
|
666
|
+
# PowerShell supports three different completion modes
|
|
667
|
+
# - TabCompleteNext (default windows style - on each key press the next option is displayed)
|
|
668
|
+
# - Complete (works like bash)
|
|
669
|
+
# - MenuComplete (works like zsh)
|
|
670
|
+
# You set the mode with Set-PSReadLineKeyHandler -Key Tab -Function <mode>
|
|
671
|
+
|
|
672
|
+
# CompletionResult Arguments:
|
|
673
|
+
# 1) CompletionText text to be used as the auto completion result
|
|
674
|
+
# 2) ListItemText text to be displayed in the suggestion list
|
|
675
|
+
# 3) ResultType type of completion result
|
|
676
|
+
# 4) ToolTip text for the tooltip with details about the object
|
|
677
|
+
|
|
678
|
+
switch ($Mode) {
|
|
679
|
+
|
|
680
|
+
# bash like
|
|
681
|
+
"Complete" {
|
|
682
|
+
|
|
683
|
+
if ($Values.Length -eq 1) {
|
|
684
|
+
__${e$1}_debug "Only one completion left"
|
|
685
|
+
|
|
686
|
+
# insert space after value
|
|
687
|
+
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e$1}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
|
|
688
|
+
|
|
689
|
+
} else {
|
|
690
|
+
# Add the proper number of spaces to align the descriptions
|
|
691
|
+
while($comp.Name.Length -lt $Longest) {
|
|
692
|
+
$comp.Name = $comp.Name + " "
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
# Check for empty description and only add parentheses if needed
|
|
696
|
+
if ($($comp.Description) -eq " " ) {
|
|
697
|
+
$Description = ""
|
|
698
|
+
} else {
|
|
699
|
+
$Description = " ($($comp.Description))"
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
[System.Management.Automation.CompletionResult]::new("$($comp.Name)$Description", "$($comp.Name)$Description", 'ParameterValue', "$($comp.Description)")
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
# zsh like
|
|
707
|
+
"MenuComplete" {
|
|
708
|
+
# insert space after value
|
|
709
|
+
# MenuComplete will automatically show the ToolTip of
|
|
710
|
+
# the highlighted value at the bottom of the suggestions.
|
|
711
|
+
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e$1}_escapeStringWithSpecialChars) + $Space, "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
|
|
712
|
+
}
|
|
713
|
+
|
|
714
|
+
# TabCompleteNext and in case we get something unknown
|
|
715
|
+
Default {
|
|
716
|
+
# Like MenuComplete but we don't want to add a space here because
|
|
717
|
+
# the user need to press space anyway to get the completion.
|
|
718
|
+
# Description will not be shown because that's not possible with TabCompleteNext
|
|
719
|
+
[System.Management.Automation.CompletionResult]::new($($comp.Name | __${e$1}_escapeStringWithSpecialChars), "$($comp.Name)", 'ParameterValue', "$($comp.Description)")
|
|
720
|
+
}
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
|
|
726
|
+
Register-ArgumentCompleter -CommandName '${e$1}' -ScriptBlock $__${n$1}CompleterBlock
|
|
727
|
+
`;
|
|
728
|
+
}
|
|
729
|
+
const a = {
|
|
730
|
+
ShellCompDirectiveError: 1,
|
|
731
|
+
ShellCompDirectiveNoSpace: 2,
|
|
732
|
+
ShellCompDirectiveNoFileComp: 4,
|
|
733
|
+
ShellCompDirectiveFilterFileExt: 8,
|
|
734
|
+
ShellCompDirectiveFilterDirs: 16,
|
|
735
|
+
ShellCompDirectiveKeepOrder: 32,
|
|
736
|
+
shellCompDirectiveMaxValue: 64,
|
|
737
|
+
ShellCompDirectiveDefault: 0
|
|
738
|
+
};
|
|
739
|
+
var o = class {
|
|
740
|
+
name;
|
|
741
|
+
variadic;
|
|
742
|
+
command;
|
|
743
|
+
handler;
|
|
744
|
+
constructor(e$1, t$1, n$1, r$1 = !1) {
|
|
745
|
+
this.command = e$1, this.name = t$1, this.handler = n$1, this.variadic = r$1;
|
|
746
|
+
}
|
|
747
|
+
}, s = class {
|
|
748
|
+
value;
|
|
749
|
+
description;
|
|
750
|
+
command;
|
|
751
|
+
handler;
|
|
752
|
+
alias;
|
|
753
|
+
isBoolean;
|
|
754
|
+
constructor(e$1, t$1, n$1, r$1, i$1, a$1) {
|
|
755
|
+
this.command = e$1, this.value = t$1, this.description = n$1, this.handler = r$1, this.alias = i$1, this.isBoolean = a$1;
|
|
756
|
+
}
|
|
757
|
+
}, c = class {
|
|
758
|
+
value;
|
|
759
|
+
description;
|
|
760
|
+
options = /* @__PURE__ */ new Map();
|
|
761
|
+
arguments = /* @__PURE__ */ new Map();
|
|
762
|
+
parent;
|
|
763
|
+
constructor(e$1, t$1) {
|
|
764
|
+
this.value = e$1, this.description = t$1;
|
|
765
|
+
}
|
|
766
|
+
option(e$1, t$1, n$1, r$1) {
|
|
767
|
+
let i$1, a$1, o$1;
|
|
768
|
+
typeof n$1 == `function` ? (i$1 = n$1, a$1 = r$1, o$1 = !1) : typeof n$1 == `string` ? (i$1 = void 0, a$1 = n$1, o$1 = !0) : (i$1 = void 0, a$1 = void 0, o$1 = !0);
|
|
769
|
+
let c$1 = new s(this, e$1, t$1, i$1, a$1, o$1);
|
|
770
|
+
return this.options.set(e$1, c$1), this;
|
|
771
|
+
}
|
|
772
|
+
argument(e$1, t$1, n$1 = !1) {
|
|
773
|
+
let r$1 = new o(this, e$1, t$1, n$1);
|
|
774
|
+
return this.arguments.set(e$1, r$1), this;
|
|
775
|
+
}
|
|
776
|
+
}, l = class extends c {
|
|
777
|
+
commands = /* @__PURE__ */ new Map();
|
|
778
|
+
completions = [];
|
|
779
|
+
directive = a.ShellCompDirectiveDefault;
|
|
780
|
+
constructor() {
|
|
781
|
+
super(``, ``);
|
|
782
|
+
}
|
|
783
|
+
command(e$1, t$1) {
|
|
784
|
+
let n$1 = new c(e$1, t$1);
|
|
785
|
+
return this.commands.set(e$1, n$1), n$1;
|
|
786
|
+
}
|
|
787
|
+
stripOptions(e$1) {
|
|
788
|
+
let t$1 = [], n$1 = 0;
|
|
789
|
+
for (; n$1 < e$1.length;) {
|
|
790
|
+
let r$1 = e$1[n$1];
|
|
791
|
+
if (r$1.startsWith(`-`)) {
|
|
792
|
+
n$1++;
|
|
793
|
+
let t$2 = !1, i$1 = this.findOption(this, r$1);
|
|
794
|
+
if (i$1) t$2 = i$1.isBoolean ?? !1;
|
|
795
|
+
else for (let [, e$2] of this.commands) {
|
|
796
|
+
let n$2 = this.findOption(e$2, r$1);
|
|
797
|
+
if (n$2) {
|
|
798
|
+
t$2 = n$2.isBoolean ?? !1;
|
|
799
|
+
break;
|
|
800
|
+
}
|
|
801
|
+
}
|
|
802
|
+
!t$2 && n$1 < e$1.length && !e$1[n$1].startsWith(`-`) && n$1++;
|
|
803
|
+
} else t$1.push(r$1), n$1++;
|
|
804
|
+
}
|
|
805
|
+
return t$1;
|
|
806
|
+
}
|
|
807
|
+
matchCommand(e$1) {
|
|
808
|
+
e$1 = this.stripOptions(e$1);
|
|
809
|
+
let t$1 = [], n$1 = [], r$1 = null;
|
|
810
|
+
for (let i$1 = 0; i$1 < e$1.length; i$1++) {
|
|
811
|
+
let a$1 = e$1[i$1];
|
|
812
|
+
t$1.push(a$1);
|
|
813
|
+
let o$1 = this.commands.get(t$1.join(` `));
|
|
814
|
+
if (o$1) r$1 = o$1;
|
|
815
|
+
else {
|
|
816
|
+
n$1 = e$1.slice(i$1, e$1.length);
|
|
817
|
+
break;
|
|
818
|
+
}
|
|
819
|
+
}
|
|
820
|
+
return [r$1 || this, n$1];
|
|
821
|
+
}
|
|
822
|
+
shouldCompleteFlags(e$1, t$1) {
|
|
823
|
+
if (t$1.startsWith(`-`)) return !0;
|
|
824
|
+
if (e$1?.startsWith(`-`)) {
|
|
825
|
+
let t$2 = this.findOption(this, e$1);
|
|
826
|
+
if (!t$2) {
|
|
827
|
+
for (let [, n$1] of this.commands) if (t$2 = this.findOption(n$1, e$1), t$2) break;
|
|
828
|
+
}
|
|
829
|
+
return !(t$2 && t$2.isBoolean);
|
|
830
|
+
}
|
|
831
|
+
return !1;
|
|
832
|
+
}
|
|
833
|
+
shouldCompleteCommands(e$1) {
|
|
834
|
+
return !e$1.startsWith(`-`);
|
|
835
|
+
}
|
|
836
|
+
handleFlagCompletion(e$1, t$1, n$1, r$1) {
|
|
837
|
+
let i$1;
|
|
838
|
+
if (n$1.includes(`=`)) {
|
|
839
|
+
let [e$2] = n$1.split(`=`);
|
|
840
|
+
i$1 = e$2;
|
|
841
|
+
} else r$1?.startsWith(`-`) && (i$1 = r$1);
|
|
842
|
+
if (i$1) {
|
|
843
|
+
let t$2 = this.findOption(e$1, i$1);
|
|
844
|
+
if (t$2?.handler) {
|
|
845
|
+
let n$2 = [];
|
|
846
|
+
t$2.handler.call(t$2, (e$2, t$3) => n$2.push({
|
|
847
|
+
value: e$2,
|
|
848
|
+
description: t$3
|
|
849
|
+
}), e$1.options), this.completions = n$2;
|
|
850
|
+
}
|
|
851
|
+
return;
|
|
852
|
+
}
|
|
853
|
+
if (n$1.startsWith(`-`)) {
|
|
854
|
+
let t$2 = n$1.startsWith(`-`) && !n$1.startsWith(`--`), r$2 = n$1.replace(/^-+/, ``);
|
|
855
|
+
for (let [i$2, a$1] of e$1.options) t$2 && a$1.alias && `-${a$1.alias}`.startsWith(n$1) ? this.completions.push({
|
|
856
|
+
value: `-${a$1.alias}`,
|
|
857
|
+
description: a$1.description
|
|
858
|
+
}) : !t$2 && i$2.startsWith(r$2) && this.completions.push({
|
|
859
|
+
value: `--${i$2}`,
|
|
860
|
+
description: a$1.description
|
|
861
|
+
});
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
findOption(e$1, t$1) {
|
|
865
|
+
let n$1 = e$1.options.get(t$1);
|
|
866
|
+
if (n$1 || (n$1 = e$1.options.get(t$1.replace(/^-+/, ``)), n$1)) return n$1;
|
|
867
|
+
for (let [n$2, r$1] of e$1.options) if (r$1.alias && `-${r$1.alias}` === t$1) return r$1;
|
|
868
|
+
}
|
|
869
|
+
handleCommandCompletion(e$1, t$1) {
|
|
870
|
+
let n$1 = this.stripOptions(e$1);
|
|
871
|
+
for (let [e$2, r$1] of this.commands) {
|
|
872
|
+
if (e$2 === ``) continue;
|
|
873
|
+
let i$1 = e$2.split(` `);
|
|
874
|
+
i$1.slice(0, n$1.length).every((e$3, t$2) => e$3 === n$1[t$2]) && i$1[n$1.length]?.startsWith(t$1) && this.completions.push({
|
|
875
|
+
value: i$1[n$1.length],
|
|
876
|
+
description: r$1.description
|
|
877
|
+
});
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
handlePositionalCompletion(e$1, t$1) {
|
|
881
|
+
let n$1 = e$1.value.split(` `).length, r$1 = Math.max(0, t$1.length - n$1), i$1 = Array.from(e$1.arguments.entries());
|
|
882
|
+
if (i$1.length > 0) {
|
|
883
|
+
let t$2;
|
|
884
|
+
if (r$1 < i$1.length) {
|
|
885
|
+
let [e$2, n$2] = i$1[r$1];
|
|
886
|
+
t$2 = n$2;
|
|
887
|
+
} else {
|
|
888
|
+
let e$2 = i$1[i$1.length - 1][1];
|
|
889
|
+
e$2.variadic && (t$2 = e$2);
|
|
890
|
+
}
|
|
891
|
+
if (t$2 && t$2.handler && typeof t$2.handler == `function`) {
|
|
892
|
+
let n$2 = [];
|
|
893
|
+
t$2.handler.call(t$2, (e$2, t$3) => n$2.push({
|
|
894
|
+
value: e$2,
|
|
895
|
+
description: t$3
|
|
896
|
+
}), e$1.options), this.completions.push(...n$2);
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
}
|
|
900
|
+
complete(e$1) {
|
|
901
|
+
this.directive = a.ShellCompDirectiveNoFileComp;
|
|
902
|
+
let t$1 = /* @__PURE__ */ new Set();
|
|
903
|
+
this.completions.filter((e$2) => t$1.has(e$2.value) ? !1 : (t$1.add(e$2.value), !0)).filter((t$2) => {
|
|
904
|
+
if (e$1.includes(`=`)) {
|
|
905
|
+
let [, n$1] = e$1.split(`=`);
|
|
906
|
+
return t$2.value.startsWith(n$1);
|
|
907
|
+
}
|
|
908
|
+
return t$2.value.startsWith(e$1);
|
|
909
|
+
}).forEach((e$2) => console.log(`${e$2.value}\t${e$2.description ?? ``}`)), console.log(`:${this.directive}`);
|
|
910
|
+
}
|
|
911
|
+
parse(e$1) {
|
|
912
|
+
this.completions = [];
|
|
913
|
+
let t$1 = e$1[e$1.length - 1] === ``;
|
|
914
|
+
t$1 && e$1.pop();
|
|
915
|
+
let n$1 = e$1[e$1.length - 1] || ``, r$1 = e$1.slice(0, -1);
|
|
916
|
+
t$1 && (n$1 !== `` && r$1.push(n$1), n$1 = ``);
|
|
917
|
+
let [i$1] = this.matchCommand(r$1), a$1 = r$1[r$1.length - 1];
|
|
918
|
+
if (this.shouldCompleteFlags(a$1, n$1)) this.handleFlagCompletion(i$1, r$1, n$1, a$1);
|
|
919
|
+
else {
|
|
920
|
+
if (a$1?.startsWith(`-`) && n$1 === `` && t$1) {
|
|
921
|
+
let e$2 = this.findOption(this, a$1);
|
|
922
|
+
if (!e$2) {
|
|
923
|
+
for (let [, t$2] of this.commands) if (e$2 = this.findOption(t$2, a$1), e$2) break;
|
|
924
|
+
}
|
|
925
|
+
if (e$2 && e$2.isBoolean) {
|
|
926
|
+
this.complete(n$1);
|
|
927
|
+
return;
|
|
928
|
+
}
|
|
929
|
+
}
|
|
930
|
+
this.shouldCompleteCommands(n$1) && this.handleCommandCompletion(r$1, n$1), i$1 && i$1.arguments.size > 0 && this.handlePositionalCompletion(i$1, r$1);
|
|
931
|
+
}
|
|
932
|
+
this.complete(n$1);
|
|
933
|
+
}
|
|
934
|
+
setup(a$1, o$1, s$1) {
|
|
935
|
+
switch (e(s$1 === `zsh` || s$1 === `bash` || s$1 === `fish` || s$1 === `powershell`, `Unsupported shell`), s$1) {
|
|
936
|
+
case `zsh`: {
|
|
937
|
+
let e$1 = t(a$1, o$1);
|
|
938
|
+
console.log(e$1);
|
|
939
|
+
break;
|
|
940
|
+
}
|
|
941
|
+
case `bash`: {
|
|
942
|
+
let e$1 = n(a$1, o$1);
|
|
943
|
+
console.log(e$1);
|
|
944
|
+
break;
|
|
945
|
+
}
|
|
946
|
+
case `fish`: {
|
|
947
|
+
let e$1 = r(a$1, o$1);
|
|
948
|
+
console.log(e$1);
|
|
949
|
+
break;
|
|
950
|
+
}
|
|
951
|
+
case `powershell`: {
|
|
952
|
+
let e$1 = i(a$1, o$1);
|
|
953
|
+
console.log(e$1);
|
|
954
|
+
break;
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
};
|
|
959
|
+
const u = new l();
|
|
960
|
+
|
|
961
|
+
//#endregion
|
|
962
|
+
//#region src/t.ts
|
|
963
|
+
function registerFlag(tc, flagName, def) {
|
|
964
|
+
const normalized = normalizeFlagValue(def);
|
|
965
|
+
const desc = normalized.description ?? "";
|
|
966
|
+
if (normalized.type === Boolean) tc.option(flagName, desc, normalized.short);
|
|
967
|
+
else {
|
|
968
|
+
const handler = normalized.completions?.handler ?? (async () => {});
|
|
969
|
+
tc.option(flagName, desc, handler, normalized.short);
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
function registerGlobalFlags(globalFlags, tc) {
|
|
973
|
+
for (const [flagName, def] of Object.entries(globalFlags)) registerFlag(tc, flagName, def);
|
|
974
|
+
}
|
|
975
|
+
function buildTabModel(t$1, globalFlags, commands) {
|
|
976
|
+
registerGlobalFlags(globalFlags, t$1);
|
|
977
|
+
for (const cmd of commands.values()) {
|
|
978
|
+
if (cmd.completions?.show === false) continue;
|
|
979
|
+
let command = t$1;
|
|
980
|
+
if (cmd.name !== "") {
|
|
981
|
+
command = t$1.command(cmd.name, cmd.description ?? "");
|
|
982
|
+
registerGlobalFlags(globalFlags, command);
|
|
983
|
+
}
|
|
984
|
+
cmd.completions?.handler?.(command);
|
|
985
|
+
for (const def of cmd.parameters ?? []) {
|
|
986
|
+
const normalized = normalizeParameterValue(def);
|
|
987
|
+
const { name, isVariadic } = extractParameterInfo(normalized.key);
|
|
988
|
+
command.argument(name, normalized.completions?.handler, isVariadic);
|
|
989
|
+
}
|
|
990
|
+
for (const [flagName, def] of Object.entries(cmd.flags ?? {})) registerFlag(command, flagName, def);
|
|
991
|
+
}
|
|
992
|
+
}
|
|
993
|
+
|
|
994
|
+
//#endregion
|
|
995
|
+
//#region src/index.ts
|
|
996
|
+
const completionsPlugin = () => definePlugin({ setup: (cli) => {
|
|
997
|
+
const t$1 = new l();
|
|
998
|
+
const supportedShellEnum = Types.Enum("zsh", "bash", "fish", "powershell");
|
|
999
|
+
cli.command("completions", "Generate shell completion scripts", {
|
|
1000
|
+
flags: { shell: {
|
|
1001
|
+
description: "Shell type",
|
|
1002
|
+
type: supportedShellEnum
|
|
1003
|
+
} },
|
|
1004
|
+
parameters: [{
|
|
1005
|
+
key: "[shell]",
|
|
1006
|
+
description: "Shell type",
|
|
1007
|
+
type: supportedShellEnum
|
|
1008
|
+
}]
|
|
1009
|
+
}).on("completions", async (ctx) => {
|
|
1010
|
+
const shell = ctx.parameters.shell ?? ctx.flags.shell;
|
|
1011
|
+
if (!shell) throw new Error("Shell type is required. Please provide it via --shell flag or [shell] parameter.");
|
|
1012
|
+
buildTabModel(t$1, cli._globalFlags, cli._commands);
|
|
1013
|
+
t$1.setup(cli._scriptName, cli._scriptName, shell);
|
|
1014
|
+
});
|
|
1015
|
+
cli.command("complete", {
|
|
1016
|
+
help: { show: false },
|
|
1017
|
+
completions: { show: false },
|
|
1018
|
+
parameters: ["--", "[input...]"]
|
|
1019
|
+
}).on("complete", async (ctx) => {
|
|
1020
|
+
buildTabModel(t$1, cli._globalFlags, cli._commands);
|
|
1021
|
+
const { input } = ctx.parameters;
|
|
1022
|
+
t$1.parse(input);
|
|
1023
|
+
});
|
|
1024
|
+
return cli;
|
|
1025
|
+
} });
|
|
1026
|
+
|
|
1027
|
+
//#endregion
|
|
1028
|
+
export { completionsPlugin };
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/plugin-completions",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc plugin completions",
|
|
@@ -25,34 +25,24 @@
|
|
|
25
25
|
"license": "MIT",
|
|
26
26
|
"sideEffects": false,
|
|
27
27
|
"exports": {
|
|
28
|
-
".": "./dist/index.
|
|
28
|
+
".": "./dist/index.mjs",
|
|
29
|
+
"./package.json": "./package.json"
|
|
29
30
|
},
|
|
30
31
|
"bin": {
|
|
31
32
|
"my-cli": "./src/a.mjs"
|
|
32
33
|
},
|
|
33
|
-
"main": "./dist/index.
|
|
34
|
-
"module": "./dist/index.
|
|
35
|
-
"types": "dist/index.d.
|
|
36
|
-
"typesVersions": {
|
|
37
|
-
"*": {
|
|
38
|
-
"*": [
|
|
39
|
-
"./dist/*",
|
|
40
|
-
"./dist/index.d.ts"
|
|
41
|
-
]
|
|
42
|
-
}
|
|
43
|
-
},
|
|
34
|
+
"main": "./dist/index.mjs",
|
|
35
|
+
"module": "./dist/index.mjs",
|
|
36
|
+
"types": "./dist/index.d.mts",
|
|
44
37
|
"files": [
|
|
45
38
|
"dist"
|
|
46
39
|
],
|
|
47
40
|
"publishConfig": {
|
|
48
41
|
"access": "public"
|
|
49
42
|
},
|
|
50
|
-
"dependencies": {
|
|
51
|
-
"@bomb.sh/tab": "^0.0.10"
|
|
52
|
-
},
|
|
53
43
|
"devDependencies": {
|
|
54
|
-
"@
|
|
55
|
-
"@clerc/core": "1.0
|
|
44
|
+
"@bomb.sh/tab": "^0.0.10",
|
|
45
|
+
"@clerc/core": "1.1.0"
|
|
56
46
|
},
|
|
57
47
|
"peerDependencies": {
|
|
58
48
|
"@clerc/core": "*"
|
package/dist/index.js
DELETED
|
@@ -1,70 +0,0 @@
|
|
|
1
|
-
import { RootCommand } from "@bomb.sh/tab";
|
|
2
|
-
import { Types, definePlugin, extractParameterInfo, normalizeFlagValue, normalizeParameterValue } from "@clerc/core";
|
|
3
|
-
|
|
4
|
-
//#region src/t.ts
|
|
5
|
-
function registerFlag(tc, flagName, def) {
|
|
6
|
-
const normalized = normalizeFlagValue(def);
|
|
7
|
-
const desc = normalized.description ?? "";
|
|
8
|
-
if (normalized.type === Boolean) tc.option(flagName, desc, normalized.short);
|
|
9
|
-
else {
|
|
10
|
-
const handler = normalized.completions?.handler ?? (async () => {});
|
|
11
|
-
tc.option(flagName, desc, handler, normalized.short);
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
function registerGlobalFlags(globalFlags, tc) {
|
|
15
|
-
for (const [flagName, def] of Object.entries(globalFlags)) registerFlag(tc, flagName, def);
|
|
16
|
-
}
|
|
17
|
-
function buildTabModel(t, globalFlags, commands) {
|
|
18
|
-
registerGlobalFlags(globalFlags, t);
|
|
19
|
-
for (const cmd of commands.values()) {
|
|
20
|
-
if (cmd.completions?.show === false) continue;
|
|
21
|
-
let command = t;
|
|
22
|
-
if (cmd.name !== "") {
|
|
23
|
-
command = t.command(cmd.name, cmd.description ?? "");
|
|
24
|
-
registerGlobalFlags(globalFlags, command);
|
|
25
|
-
}
|
|
26
|
-
cmd.completions?.handler?.(command);
|
|
27
|
-
for (const def of cmd.parameters ?? []) {
|
|
28
|
-
const normalized = normalizeParameterValue(def);
|
|
29
|
-
const { name, isVariadic } = extractParameterInfo(normalized.key);
|
|
30
|
-
command.argument(name, normalized.completions?.handler, isVariadic);
|
|
31
|
-
}
|
|
32
|
-
for (const [flagName, def] of Object.entries(cmd.flags ?? {})) registerFlag(command, flagName, def);
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
//#endregion
|
|
37
|
-
//#region src/index.ts
|
|
38
|
-
const completionsPlugin = () => definePlugin({ setup: (cli) => {
|
|
39
|
-
const t = new RootCommand();
|
|
40
|
-
const supportedShellEnum = Types.Enum("zsh", "bash", "fish", "powershell");
|
|
41
|
-
cli.command("completions", "Generate shell completion scripts", {
|
|
42
|
-
flags: { shell: {
|
|
43
|
-
description: "Shell type",
|
|
44
|
-
type: supportedShellEnum
|
|
45
|
-
} },
|
|
46
|
-
parameters: [{
|
|
47
|
-
key: "[shell]",
|
|
48
|
-
description: "Shell type",
|
|
49
|
-
type: supportedShellEnum
|
|
50
|
-
}]
|
|
51
|
-
}).on("completions", async (ctx) => {
|
|
52
|
-
const shell = ctx.parameters.shell ?? ctx.flags.shell;
|
|
53
|
-
if (!shell) throw new Error("Shell type is required. Please provide it via --shell flag or [shell] parameter.");
|
|
54
|
-
buildTabModel(t, cli._globalFlags, cli._commands);
|
|
55
|
-
t.setup(cli._scriptName, cli._scriptName, shell);
|
|
56
|
-
});
|
|
57
|
-
cli.command("complete", {
|
|
58
|
-
help: { show: false },
|
|
59
|
-
completions: { show: false },
|
|
60
|
-
parameters: ["--", "[input...]"]
|
|
61
|
-
}).on("complete", async (ctx) => {
|
|
62
|
-
buildTabModel(t, cli._globalFlags, cli._commands);
|
|
63
|
-
const { input } = ctx.parameters;
|
|
64
|
-
t.parse(input);
|
|
65
|
-
});
|
|
66
|
-
return cli;
|
|
67
|
-
} });
|
|
68
|
-
|
|
69
|
-
//#endregion
|
|
70
|
-
export { completionsPlugin };
|