forked from kazhala/dotbare
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdotbare.plugin.bash
164 lines (155 loc) · 4.99 KB
/
dotbare.plugin.bash
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
# shellcheck disable=SC2207
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
[[ :$PATH: != *:"${mydir}":* ]] && export PATH="$PATH:${mydir}"
__dotbare_completion()
{
local IFS=$'\n' subcommands curr prev options verbose_options suggestions
curr="${COMP_WORDS[$COMP_CWORD]}"
prev="${COMP_WORDS[$COMP_CWORD-1]}"
if [[ "$COMP_CWORD" -eq "1" ]]; then
subcommands=$(
"${mydir}"/dotbare -h \
| awk '{
if ($0 ~ /^ f.*/) {
gsub(/^ /, "", $0)
gsub(/\t\t/, " ", $0)
print $0
}
}'
)
options=$(
"${mydir}"/dotbare -h \
| awk '{
if ($0 ~ /^ -.*/) {
gsub(/,/, " ", $0)
gsub(/^ /, "", $0)
gsub(/\t\t/, " ", $0)
$2=""
print $0
}
}'
)
verbose_options=$("${mydir}"/dotbare -h | awk '$0 ~ /^ -.*/ {print $2}')
if [[ "${curr}" == --* ]]; then
suggestions=($(compgen -W "${verbose_options}" -- "${curr}"))
elif [[ "${curr}" == -* ]]; then
suggestions=($(compgen -W "${options}" -- "${curr}"))
else
suggestions=($(compgen -W "${subcommands}" -- "${curr}"))
fi
elif [[ "${COMP_CWORD}" -eq 2 && "${prev}" == "-g" || "${prev}" == "--git" ]]; then
subcommands=$(
"${mydir}"/dotbare -h \
| awk '{
if ($0 ~ /^ f.*/) {
if ($0 ~ /^ f(backup|init|upgrade).*/) {
next
}
gsub(/^ /, "", $0)
gsub(/\t\t/, " ", $0)
print $0
}
}'
)
suggestions=($(compgen -W "${subcommands}" -- "${curr}"))
elif [[ "${COMP_WORDS[1]}" == "-g" || "${COMP_WORDS[1]}" == "--git" ]] && [[ "${COMP_CWORD}" -gt 2 ]]; then
if [[ "${curr}" == --* && "${prev}" != "-h" && "${prev}" != "--help" ]]; then
verbose_options=$(
"${mydir}"/dotbare "${COMP_WORDS[2]}" -h 2> /dev/null \
| awk '{
if ($0 ~ /^ -p PATH/) {
next
} else if ($0 ~ /^ -u URL/) {
next
} else if ($0 ~ /^ -*/) {
print $2
}
}'
)
suggestions=($(compgen -W "${verbose_options}" -- "${curr}"))
elif [[ "${prev}" != "-h" && "${prev}" != "--help" ]]; then
options=$(
"${mydir}"/dotbare "${COMP_WORDS[2]}" -h 2> /dev/null \
| awk '{
gsub(/,/, " ", $0)
if ($0 ~ /^ -p PATH/) {
next
} else if ($0 ~ /^ -u URL/) {
next
} else if ($0 ~ /^ -*/) {
gsub(/^ /, "", $0)
gsub(/\t/, " ", $0)
$2=""
print $0
}
}'
)
suggestions=($(compgen -W "${options}" -- "${curr}"))
else
return
fi
elif [[ "${COMP_WORDS[1]}" == "fbackup" ]] && [[ "${prev}" == "-p" || "${prev}" == "--path" ]]; then
COMPREPLY=($(compgen -d -- "${curr}"))
return
elif [[ "${COMP_WORDS[1]}" == "finit" ]] && [[ "${prev}" == "-u" || "${prev}" == "--url" ]]; then
return
elif [[ "${curr}" == --* && "${prev}" != "-h" && "${prev}" != "--help" ]]; then
verbose_options=$(
"${mydir}"/dotbare "${COMP_WORDS[1]}" -h 2> /dev/null \
| awk '{
if ($0 ~ /^ -p PATH/) {
print "--path"
} else if ($0 ~ /^ -u URL/) {
print "--url"
} else if ($0 ~ /^ -c COL/) {
print "--col"
} else if ($0 ~ /^ -*/) {
print $2
}
}'
)
suggestions=($(compgen -W "${verbose_options}" -- "${curr}"))
elif [[ "${prev}" != "-h" && "${prev}" != "--help" ]]; then
options=$(
"${mydir}"/dotbare "${COMP_WORDS[1]}" -h 2> /dev/null \
| awk '{
gsub(/,/, " ", $0)
if ($0 ~ /^ -p PATH/) {
gsub(/^ -p PATH --path PATH/, "-p", $0)
gsub(/\t/, " ", $0)
print $0
} else if ($0 ~ /^ -u URL/) {
gsub(/^ -u URL --url URL/, "-u", $0)
gsub(/\t/, " ", $0)
print $0
} else if ($0 ~ /^ -c COL/) {
gsub(/^ -c COL --col COL/, "-c", $0)
gsub(/\t/, " ", $0)
print $0
} else if ($0 ~ /^ -*/) {
gsub(/^ /, "", $0)
gsub(/\t/, " ", $0)
$2=""
print $0
}
}'
)
suggestions=($(compgen -W "${options}" -- "${curr}"))
fi
if [[ "${#suggestions[*]}" -eq 1 ]]; then
COMPREPLY=("${suggestions[@]%% *}")
else
for i in "${!suggestions[@]}"; do
suggestions[$i]="$(printf '%*s' "-$COLUMNS" "${suggestions[$i]}")"
done
COMPREPLY=("${suggestions[@]}")
fi
}
_dotbare_completion_cmd() {
local complete_name="${1:-dotbare}"
complete -F __dotbare_completion "${complete_name}"
}
_dotbare_completion_git() {
local complete_name="${1:-dotbare}"
complete -F _git "${complete_name}"
}