@@ -3,22 +3,33 @@ _pg_databases()
33 # -w was introduced in 8.4, https://launchpad.net/bugs/164772
44 # "Access privileges" in output may contain linefeeds, hence the NF > 1
55 COMPREPLY=( $( compgen -W " $( psql -AtqwlF $' \t ' 2> /dev/null | \
6- awk ' NF > 1 { print $1 }' ) " -- " $cur " ) )
6+ awk ' NF > 1 { print $1 }' ) " -- " $cur " ) )
77}
8-
8+
99_pg_users ()
1010{
1111 # -w was introduced in 8.4, https://launchpad.net/bugs/164772
1212 COMPREPLY=( $( compgen -W " $( psql -Atqwc ' select usename from pg_user' \
1313 template1 2> /dev/null ) " -- " $cur " ) )
1414 [[ ${# COMPREPLY[@]} -eq 0 ]] && COMPREPLY=( $( compgen -u -- " $cur " ) )
1515}
16-
16+
17+ _pg_services ()
18+ {
19+ # return list of available services
20+ local services
21+ if [[ -f " $HOME /.pg_service.conf" ]]; then
22+ services=$( grep -oP ' (?<=^\[).*?(?=\])' " $HOME /.pg_service.conf" )
23+ fi
24+ local suffix=" ${cur#* =} "
25+ COMPREPLY=( $( compgen -W " $services " -- " $suffix " ) )
26+ }
27+
1728_pgcli ()
1829{
1930 local cur prev words cword
2031 _init_completion -s || return
21-
32+
2233 case $prev in
2334 -h|--host)
2435 _known_hosts_real " $cur "
@@ -39,23 +50,27 @@ _pgcli()
3950 esac
4051
4152 case " $cur " in
42- --* )
43- # return list of available options
44- COMPREPLY=( $( compgen -W ' --host --port --user --password --no-password
45- --single-connection --version --dbname --pgclirc --dsn
46- --row-limit --help' -- " $cur " ) )
47- [[ $COMPREPLY == * = ]] && compopt -o nospace
48- return 0
49- ;;
50- -)
51- # only complete long options
52- compopt -o nospace
53- COMPREPLY=( -- )
54- return 0
55- ;;
56- * )
53+ service=* )
54+ _pg_services
55+ return 0
56+ ;;
57+ --* )
58+ # return list of available options
59+ COMPREPLY=( $( compgen -W ' --host --port --user --password --no-password
60+ --single-connection --version --dbname --pgclirc --dsn
61+ --row-limit --help' -- " $cur " ) )
62+ [[ $COMPREPLY == * = ]] && compopt -o nospace
63+ return 0
64+ ;;
65+ -)
66+ # only complete long options
67+ compopt -o nospace
68+ COMPREPLY=( -- )
69+ return 0
70+ ;;
71+ * )
5772 # return list of available databases
58- _pg_databases
73+ _pg_databases
5974 esac
60- } &&
75+ } &&
6176complete -F _pgcli pgcli
0 commit comments