forked from graalvm/mx
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmx
More file actions
91 lines (83 loc) · 2.55 KB
/
mx
File metadata and controls
91 lines (83 loc) · 2.55 KB
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
_mx() {
local cur prev opts commands
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# read options & commands
IFS='|' read opts commands <<< $(_mx_completion)
# complete command arguments
for command in $commands; do
if [[ "${prev}" == "${command}" ]]; then
# unusable output
#local help=$(mx help ${command})
local help=""
COMPREPLY=($(compgen -W "${help}" -- $cur))
return 0
fi
done;
# complete the arguments
if [[ "${cur}" == -* ]]; then
COMPREPLY=($(compgen -W "${opts}" -- $cur))
return 0
fi
COMPREPLY=($(compgen -W "${commands}" -- $cur))
return 0
}
_mx_completion() {
local project_hash=$(echo -n $PWD | sha1sum | awk '{print $1}')
local tmp_file="/tmp/mx-bash-completion-${project_hash}"
if [[ ! -f $tmp_file ]]; then
mx help 2>/dev/null | awk '
BEGIN {
# state
state_options=0;
state_commands=0;
# counts
nr_options=0;
nr_commands=0;
}
{
# reset
if ($0 == "") {
state_options=0;
next;
}
# arguments
if ($1 == "optional" && $2 == "arguments:") {
state_options=1;
next;
}
if (state_options) {
for (i = 1; i <= NF; i++) {
if (index($i, "-") == 1) {
options[nr_options] = $i;
nr_options++;
}
}
next;
}
# commands
if ($1 == "available" && $2 == "commands:") {
state_commands=1;
next;
};
if (state_commands && $1 != "") {
commands[nr_commands] = $1;
nr_commands++;
}
}
END {
for (i = 0; i < nr_options; i++) {
printf("%s ", options[i]);
}
printf("%s ", "|");
for (i = 0; i < nr_commands; i++) {
printf("%s ", commands[i]);
}
}' > $tmp_file
fi
cat $tmp_file
}
# Use default bash autocomplete behavior if mx autocomplete fails.
complete -F _mx -o default mx
# vi: syntax=bash