-
-
Notifications
You must be signed in to change notification settings - Fork 63
/
_bash_completions_generator.ts
204 lines (171 loc) 路 5.09 KB
/
_bash_completions_generator.ts
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
import type { Command } from "../command.ts";
import type { IArgument } from "../types.ts";
/** Generates bash completions script. */
export class BashCompletionsGenerator {
/** Generates bash completions script for given command. */
public static generate(cmd: Command) {
return new BashCompletionsGenerator(cmd).generate();
}
private constructor(protected cmd: Command) {}
/** Generates bash completions code. */
private generate(): string {
const path = this.cmd.getPath();
const version: string | undefined = this.cmd.getVersion()
? ` v${this.cmd.getVersion()}`
: "";
return `#!/usr/bin/env bash
# bash completion support for ${path}${version}
_${replaceSpecialChars(path)}() {
local word cur prev
local -a opts
COMPREPLY=()
cur="\${COMP_WORDS[COMP_CWORD]}"
prev="\${COMP_WORDS[COMP_CWORD-1]}"
cmd="_"
opts=()
_${replaceSpecialChars(this.cmd.getName())}_complete() {
local action="$1"; shift
mapfile -t values < <( ${this.cmd.getName()} completions complete "\${action}" "\${@}" )
for i in "\${values[@]}"; do
opts+=("$i")
done
}
${this.generateCompletions(this.cmd).trim()}
for word in "\${COMP_WORDS[@]}"; do
case "\${word}" in
-*) ;;
*)
cmd_tmp="\${cmd}_\${word//[^[:alnum:]]/_}"
if type "\${cmd_tmp}" &>/dev/null; then
cmd="\${cmd_tmp}"
fi
esac
done
\${cmd}
if [[ \${#opts[@]} -eq 0 ]]; then
# shellcheck disable=SC2207
COMPREPLY=($(compgen -f "\${cur}"))
return 0
fi
local values
values="$( printf "\\n%s" "\${opts[@]}" )"
local IFS=$'\\n'
# shellcheck disable=SC2207
local result=($(compgen -W "\${values[@]}" -- "\${cur}"))
if [[ \${#result[@]} -eq 0 ]]; then
# shellcheck disable=SC2207
COMPREPLY=($(compgen -f "\${cur}"))
else
# shellcheck disable=SC2207
COMPREPLY=($(printf '%q\\n' "\${result[@]}"))
fi
return 0
}
complete -F _${replaceSpecialChars(path)} -o bashdefault -o default ${path}
`;
}
/** Generates bash completions method for given command and child commands. */
private generateCompletions(command: Command, path = "", index = 1): string {
path = (path ? path + " " : "") + command.getName();
const commandCompletions = this.generateCommandCompletions(
command,
path,
index,
);
const childCommandCompletions: string = command.getCommands(false)
.filter((subCommand: Command) => subCommand !== command)
.map((subCommand: Command) =>
this.generateCompletions(subCommand, path, index + 1)
)
.join("");
return `${commandCompletions}
${childCommandCompletions}`;
}
private generateCommandCompletions(
command: Command,
path: string,
index: number,
): string {
const flags: string[] = this.getFlags(command);
const childCommandNames: string[] = command.getCommands(false)
.map((childCommand: Command) => childCommand.getName());
const completionsPath: string = ~path.indexOf(" ")
? " " + path.split(" ").slice(1).join(" ")
: "";
const optionArguments = this.generateOptionArguments(
command,
completionsPath,
);
const completionsCmd: string = this.generateCommandCompletionsCommand(
command.getArguments(),
completionsPath,
);
return ` __${replaceSpecialChars(path)}() {
opts=(${[...flags, ...childCommandNames].join(" ")})
${completionsCmd}
if [[ \${cur} == -* || \${COMP_CWORD} -eq ${index} ]] ; then
return 0
fi
${optionArguments}
}`;
}
private getFlags(command: Command): string[] {
return command.getOptions(false)
.map((option) => option.flags)
.flat();
}
private generateOptionArguments(
command: Command,
completionsPath: string,
): string {
let opts = "";
const options = command.getOptions(false);
if (options.length) {
opts += 'case "${prev}" in';
for (const option of options) {
const flags: string = option.flags
.map((flag: string) => flag.trim())
.join("|");
const completionsCmd: string = this.generateOptionCompletionsCommand(
option.args,
completionsPath,
{ standalone: option.standalone },
);
opts += `\n ${flags}) ${completionsCmd} ;;`;
}
opts += "\n esac";
}
return opts;
}
private generateCommandCompletionsCommand(
args: IArgument[],
path: string,
) {
if (args.length) {
// @TODO: add support for multiple arguments
return `_${replaceSpecialChars(this.cmd.getName())}_complete ${
args[0].action
}${path}`;
}
return "";
}
private generateOptionCompletionsCommand(
args: IArgument[],
path: string,
opts?: { standalone?: boolean },
) {
if (args.length) {
// @TODO: add support for multiple arguments
return `opts=(); _${replaceSpecialChars(this.cmd.getName())}_complete ${
args[0].action
}${path}`;
}
if (opts?.standalone) {
return "opts=()";
}
return "";
}
}
function replaceSpecialChars(str: string): string {
return str.replace(/[^a-zA-Z0-9]/g, "_");
}