forked from ElektraInitiative/libelektra
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kdb-bash-completion
69 lines (60 loc) · 2.22 KB
/
kdb-bash-completion
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
#!/usr/bin/env bash
###########################################################################
##
## This script sets up basic bash completion for the kdb command
##
##
## Put it under /etc/bash_completion.d or load it just for the current
## session with . <scriptname>
##
###########################################################################
_kdb() {
COMPREPLY=()
# assign the currently active word
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD - 1]}"
# initialize
local kdbpath=$(which kdb)
# only kdb was entered yet, print a list of available commands
if [[ $COMP_CWORD -eq 1 ]]; then
local IFS=$'\n'
local commands=($(${kdbpath} list-commands))
COMPREPLY=($(compgen -W '${commands[@]}' -- "${cur}"))
return
fi
# commands which expects a path as next parameter
local pathcommands="export file get getmeta cp ls lsmeta mv rm set setmeta sget"
if [[ $pathcommands =~ $prev ]]; then
local IFS=$'\n'
local paths=($( (${kdbpath} ls system && ${kdbpath} ls user) 2> /dev/null))
COMPREPLY=($(compgen -W '${paths[@]}' -- "${cur}"))
# Now escape special characters (spaces, single and double quotes),
# so that the argument is really regarded a single argument by bash.
# See http://stackoverflow.com/questions/1146098/properly-handling-spaces-and-quotes-in-bash-completion
local i=0
for entry in ${COMPREPLY[*]}; do
if [[ ${cur:0:1} == "'" ]]; then
# started with single quote, escaping only other single quotes
# [']bla'bla"bla\bla bla --> [']bla'\''bla"bla\bla bla
local escaped_single_quote="'\''"
COMPREPLY[$i]="${entry//\'/${escaped_single_quote}}"
elif [[ ${cur:0:1} == '"' ]]; then
# started with double quote, escaping all double quotes and all backslashes
# ["]bla'bla"bla\bla bla --> ["]bla'bla\"bla\\bla bla
entry="${entry//\\/\\\\}"
COMPREPLY[$i]="${entry//\"/\\\"}"
else
# no quotes in front, escaping _everything_
# [ ]bla'bla"bla\bla bla --> [ ]bla\'bla\"bla\\bla\ bla
entry="${entry//\\/\\\\}"
entry="${entry//\'/\'}"
entry="${entry//\"/\\\"}"
COMPREPLY[$i]="${entry// /\\ }"
fi
((i++))
done
return
fi
}
# complete the command with _kdb and fall back to filename completion
complete -o default -F _kdb kdb