@@ -3,22 +3,33 @@ _pg_databases()
3
3
# -w was introduced in 8.4, https://launchpad.net/bugs/164772
4
4
# "Access privileges" in output may contain linefeeds, hence the NF > 1
5
5
COMPREPLY=( $( compgen -W " $( psql -AtqwlF $' \t ' 2> /dev/null | \
6
- awk ' NF > 1 { print $1 }' ) " -- " $cur " ) )
6
+ awk ' NF > 1 { print $1 }' ) " -- " $cur " ) )
7
7
}
8
-
8
+
9
9
_pg_users ()
10
10
{
11
11
# -w was introduced in 8.4, https://launchpad.net/bugs/164772
12
12
COMPREPLY=( $( compgen -W " $( psql -Atqwc ' select usename from pg_user' \
13
13
template1 2> /dev/null ) " -- " $cur " ) )
14
14
[[ ${# COMPREPLY[@]} -eq 0 ]] && COMPREPLY=( $( compgen -u -- " $cur " ) )
15
15
}
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
+
17
28
_pgcli ()
18
29
{
19
30
local cur prev words cword
20
31
_init_completion -s || return
21
-
32
+
22
33
case $prev in
23
34
-h|--host)
24
35
_known_hosts_real " $cur "
@@ -39,23 +50,27 @@ _pgcli()
39
50
esac
40
51
41
52
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
+ * )
57
72
# return list of available databases
58
- _pg_databases
73
+ _pg_databases
59
74
esac
60
- } &&
75
+ } &&
61
76
complete -F _pgcli pgcli
0 commit comments