1 | have port && { |
---|
2 | # helper functions for port completion |
---|
3 | # |
---|
4 | |
---|
5 | # port(1) completion |
---|
6 | # |
---|
7 | _port() |
---|
8 | { |
---|
9 | local cur prev mode count portdir portdiropt i port |
---|
10 | |
---|
11 | COMPREPLY=() |
---|
12 | cur=${COMP_WORDS[COMP_CWORD]} |
---|
13 | prev=${COMP_WORDS[COMP_CWORD-1]} |
---|
14 | |
---|
15 | count=0 |
---|
16 | for i in ${COMP_WORDS[@]}; do |
---|
17 | [ $count -eq $COMP_CWORD ] && break |
---|
18 | # Last parameter was the portdir, now go back to mode selection |
---|
19 | if [ "${COMP_WORDS[((count))]}" == "$portdir" -a "$mode" == "portdir" ]; then |
---|
20 | mode="" |
---|
21 | fi |
---|
22 | if [ -z "$mode" ]; then |
---|
23 | case $i in |
---|
24 | -D) |
---|
25 | mode=portdir |
---|
26 | portdir=${COMP_WORDS[((count+1))]} |
---|
27 | portdiropt="-D $portdir" |
---|
28 | ;; |
---|
29 | install|uninstall|activate|deactivate|installed|\ |
---|
30 | outdated|upgrade|selfupdate|sync|\ |
---|
31 | clean|list|search|info|variants|deps|contents|\ |
---|
32 | unarchive|fetch|extract|patch|configure|build|destroot|test|archive|\ |
---|
33 | pkg|mpkg|dmg|rpmpackage|provides) |
---|
34 | mode=$i |
---|
35 | ;; |
---|
36 | esac |
---|
37 | elif [ -z "$port" ]; then |
---|
38 | case $mode in |
---|
39 | uninstall|upgrade|contents) |
---|
40 | if [ $( port installed | awk ' !/The following ports/ {print $1}' \ |
---|
41 | | uniq | grep '^'$i'$') ]; then |
---|
42 | port=$i |
---|
43 | fi |
---|
44 | ;; |
---|
45 | *) |
---|
46 | if [ $(port $portdiropt list | awk '{ print $1 }' | grep '^'$i'$') ]; then |
---|
47 | port=$i |
---|
48 | fi |
---|
49 | ;; |
---|
50 | esac |
---|
51 | fi |
---|
52 | count=$((++count)) |
---|
53 | done |
---|
54 | |
---|
55 | if [ -n "$port" ]; then |
---|
56 | # complete variants |
---|
57 | case $mode in |
---|
58 | contents|uninstall) |
---|
59 | # installed variants |
---|
60 | COMPREPLY=( $( port installed \ |
---|
61 | | awk '/'$port'/ { print $2 }' | tr '\r' ' ' 2> /dev/null ) ) |
---|
62 | COMPREPLY=( $( compgen -W '${COMPREPLY[@]}' -- $cur ) ) |
---|
63 | return 0 |
---|
64 | ;; |
---|
65 | activate) |
---|
66 | # inactive variants |
---|
67 | COMPREPLY=( $( port installed | grep -v '(active)' \ |
---|
68 | | awk '/'$port'/ { print $2 }' | tr '\r' ' ' 2> /dev/null ) ) |
---|
69 | COMPREPLY=( $( compgen -W '${COMPREPLY[@]}' -- $cur ) ) |
---|
70 | return 0 |
---|
71 | ;; |
---|
72 | deactivate) |
---|
73 | # active variants |
---|
74 | COMPREPLY=( $( port installed | grep '(active)' \ |
---|
75 | | awk '/'$port'/ { print $2 }' | tr '\r' ' ' 2> /dev/null ) ) |
---|
76 | COMPREPLY=( $( compgen -W '${COMPREPLY[@]}' -- $cur ) ) |
---|
77 | return 0 |
---|
78 | ;; |
---|
79 | *) |
---|
80 | # all variants |
---|
81 | COMPREPLY=( $(port $portdiropt variants $port | tr '\r' ' ' | grep -v "has no variants") ) |
---|
82 | COMPREPLY=( $( compgen -P'+' -W '${COMPREPLY[@]}' -- $cur ) ) |
---|
83 | return 0 |
---|
84 | ;; |
---|
85 | esac |
---|
86 | |
---|
87 | fi |
---|
88 | |
---|
89 | if [ -n "$mode" ]; then |
---|
90 | # complete port names |
---|
91 | case $mode in |
---|
92 | uninstall|upgrade|contents) |
---|
93 | # installed ports |
---|
94 | COMPREPLY=( $( port installed \ |
---|
95 | | sed -ne 's|^ \('$cur'[^ ]*\).*$|\1|p' | uniq ) ) |
---|
96 | return 0 |
---|
97 | ;; |
---|
98 | activate) |
---|
99 | # inactive ports |
---|
100 | COMPREPLY=( $( port installed | grep -v '(active)' \ |
---|
101 | | sed -ne 's|^ \('$cur'[^ ]*\).*$|\1|p' | uniq ) ) |
---|
102 | return 0 |
---|
103 | ;; |
---|
104 | deactivate) |
---|
105 | # active ports |
---|
106 | COMPREPLY=( $( port installed | grep '(active)' \ |
---|
107 | | sed -ne 's|^ \('$cur'[^ ]*\).*$|\1|p' | uniq ) ) |
---|
108 | return 0 |
---|
109 | ;; |
---|
110 | provides|portdir) |
---|
111 | _filedir |
---|
112 | return 0 |
---|
113 | ;; |
---|
114 | installed|outdated|list|sync|selfupdate) |
---|
115 | # no port |
---|
116 | return 0 |
---|
117 | ;; |
---|
118 | *) |
---|
119 | # all ports |
---|
120 | COMPREPLY=( $( port $portdiropt list \ |
---|
121 | | awk '/^'$cur'/ { print $1 }' | sort 2> /dev/null ) ) |
---|
122 | return 0 |
---|
123 | ;; |
---|
124 | esac |
---|
125 | fi |
---|
126 | |
---|
127 | COMPREPLY=( $( compgen -W '-v -d -q -c -k -n -a -u -D -f -o -s -b \ |
---|
128 | install uninstall activate deactivate installed \ |
---|
129 | outdated upgrade selfupdate sync \ |
---|
130 | clean list search info variants deps contents \ |
---|
131 | unarchive fetch extract patch configure build \ |
---|
132 | destroot test archive \ |
---|
133 | pkg mpkg dmg rpmpackage provides' -- $cur ) ) |
---|
134 | |
---|
135 | |
---|
136 | return 0 |
---|
137 | } |
---|
138 | complete -F _port $filenames port |
---|
139 | } |
---|