Skip to content

Commit 6c07757

Browse files
committed
Linked zsh completion with 'scw _completion' command (#45)
1 parent b28568a commit 6c07757

File tree

1 file changed

+12
-72
lines changed
  • contrib/completion/zsh

1 file changed

+12
-72
lines changed

contrib/completion/zsh/_scw

Lines changed: 12 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -5,94 +5,37 @@
55
# Inspired by https://github.com/felixr/docker-zsh-completion
66

77
__scw_get_servers() {
8-
local kind expl
9-
declare -a running stopped lines args
10-
11-
kind=$1
12-
shift
13-
[[ $kind = (stopped|all) ]] && args=($args -a)
14-
15-
lines=(${(f)"$(_call_program commands scw ps ${args})"})
16-
17-
# Parse header line to find columns
18-
local i=1 j=1 k header=${lines[1]}
19-
declare -A begin end
20-
while (( $j < ${#header} - 1 )) {
21-
i=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 1))
22-
j=$(( $i + ${${header[$i,-1]}[(i) ]} - 1))
23-
k=$(( $j + ${${header[$j,-1]}[(i)[^ ]]} - 2))
24-
begin[${header[$i,$(($j-1))]}]=$i
25-
end[${header[$i,$(($j-1))]}]=$k
26-
}
27-
lines=(${lines[2,-1]})
28-
29-
# Server ID
30-
local line
31-
local s
32-
for line in $lines; do
33-
s="${line[${begin[SERVER ID]},${end[SERVER ID]}]%% ##}"
34-
s="$s:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}"
35-
s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}"
36-
if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then
37-
stopped=($stopped $s)
38-
else
39-
running=($running $s)
40-
fi
41-
done
42-
43-
# Names
44-
local name
45-
local -a names
46-
for line in $lines; do
47-
names=(${(ps:,:)${${line[${begin[NAMES]},-1]}%% *}})
48-
for name in $names; do
49-
s="${name}:${(l:15:: :::)${${line[${begin[CREATED]},${end[CREATED]}]/ ago/}%% ##}}"
50-
s="$s, ${${${line[$begin[IMAGE],$end[IMAGE]]}/:/\\:}%% ##}"
51-
if [[ ${line[${begin[STATUS]},${end[STATUS]}]} = Exit* ]]; then
52-
stopped=($stopped $s)
53-
else
54-
running=($running $s)
55-
fi
56-
done
57-
done
58-
59-
[[ $kind = (running|all) ]] && _describe -t servers-running "running servers" running
60-
[[ $kind = (stopped|all) ]] && _describe -t servers-stopped "stopped servers" stopped
8+
local expl
9+
declare -a servers
10+
servers=(${(f)"$(_call_program commands scw _completion servers-all)"})
11+
_describe -t servers "servers" servers
6112
}
6213

6314
__scw_stoppedservers() {
64-
__scw_get_servers stopped "$@"
15+
__scw_get_servers
6516
}
6617

6718
__scw_runningservers() {
68-
__scw_get_servers running "$@"
19+
__scw_get_servers
6920
}
7021

7122
__scw_servers () {
72-
__scw_get_servers all "$@"
23+
__scw_get_servers
7324
}
7425

7526
__scw_images () {
7627
local expl
7728
declare -a images
78-
images=(${${${${(f)"$(_call_program commands scw images)"}[2,-1]}/ ##/\\:}%% *})
79-
images=(${${images%\\:<none>}#<none>} ${${${(f)"$(_call_program commands scw images)"}[2,-1]}/(#b)([^ ]##) ##([^ ]##) ##([^ ]##)*/${match[3]}:${(r:15:: :::)match[2]} in ${match[1]}})
80-
_describe -t scw-images "images" images
29+
images=(${(f)"$(_call_program commands scw _completion images-all)"})
30+
_describe -t images "images" images
8131
}
8232

8333
__scw_tags() {
84-
local expl
85-
declare -a tags
86-
tags=(${${${${${(f)"$(_call_program commands scw images)"}#* }## #}%% *}[2,-1]})
87-
_describe -t scw-tags "tags" tags
34+
__scw_images
8835
}
8936

9037
__scw_repositories_with_tags() {
91-
if compset -P '*:'; then
92-
__scw_tags
93-
else
94-
__scw_repositories -qS ":"
95-
fi
38+
__scw_images
9639
}
9740

9841
__scw_search() {
@@ -126,10 +69,7 @@ __scw_caching_policy()
12669

12770

12871
__scw_repositories () {
129-
local expl
130-
declare -a repos
131-
repos=(${${${(f)"$(_call_program commands scw images)"}%% *}[2,-1]})
132-
_describe -t scw-repos "repositories" repos "$@"
72+
__scw_images
13373
}
13474

13575
__scw_commands () {

0 commit comments

Comments
 (0)