forked from keymanapp/keyman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
km-package-install.bash-completion
94 lines (86 loc) · 4.17 KB
/
km-package-install.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# shellcheck disable=SC2148
# No hashbang for bash completion scripts! They are intended to be sourced, not executed.
_km-package-install_completions()
{
local cur prev opts cache pkg_install_path
COMPREPLY=()
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
opts="-h --help -v --verbose -vv --veryverbose --version -p --package -f --file -s --shared -l --bcp47 --force"
cache=${XDG_CACHE_HOME:-~/.cache}/keyman/kmpdirlist
if [[ ${cur} == -* ]] ; then
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -W "${opts}" -- "${cur}") )
return 0
fi
if [[ -e ./km-package-install ]]; then
pkg_install_path='./km-package-install'
get_kmp_path='keyman_config/get_kmp.py'
else
pkg_install_path='/usr/bin/km-package-install'
get_kmp_path='/usr/lib/python3/dist-packages/keyman_config/get_kmp.py'
fi
case "${prev}" in
"-p"|"--package")
words=""
if [[ ! -s $cache ]] ; then
# NOTE: identical code in `km-package-get.bash-completion`.
# Unfortunately with bash completion scripts it's not possible to factor out
# common code.
python3 -c "from importlib.machinery import SourceFileLoader;from importlib.util import module_from_spec, spec_from_loader;loader = SourceFileLoader('km_package_install', '$pkg_install_path');spec = spec_from_loader(loader.name, loader);mod = module_from_spec(spec);loader.exec_module(mod);mod.list_keyboards()"
fi
if [[ -r $cache ]] ; then
while read -r file; do words="${words} ${file}"; done < "$cache"
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "${words}" -- "${cur}"))
return 0
fi
;;
"-f"|"--file")
local IFS=$'\n'
compopt -o filenames
# shellcheck disable=SC2207
COMPREPLY=( $(compgen -f -X "!"*.kmp -- "$cur") $(compgen -d -- "$cur") )
return 0
;;
"-l"|"--bcp47")
local packageId=""
local packageDir
packageDir=$(mktemp -d)
for ((i=0;i<$COMP_CWORD;i++)); do
case ${COMP_WORDS[$i]} in
"-p"|"--package")
packageId="${COMP_WORDS[$i+1]}"
# shellcheck disable=SC2086 # doesn't work with quotes
if [ ! -f ${XDG_CACHE_HOME:-~/.cache}/keyman/$packageId ] && [ ! -f ${XDG_CACHE_HOME:-~/.cache}/keyman/${packageId}.kmp ]; then
python3 -c "from importlib.machinery import SourceFileLoader;from importlib.util import module_from_spec, spec_from_loader;loader = SourceFileLoader('get_kmp', '$get_kmp_path');spec = spec_from_loader(loader.name, loader);mod = module_from_spec(spec);loader.exec_module(mod);mod.get_kmp('$packageId')"
fi
break
;;
"-f"|"--file")
package="${COMP_WORDS[$i+1]}"
packageId=$(basename "$package")
# shellcheck disable=SC2086 # doesn't work with quotes
cp "$package" ${XDG_CACHE_HOME:-~/.cache}/keyman/${packageId}
break
;;
*)
;;
esac
done
if [ -z "$packageId" ]; then
return 0
fi
words=""
while read -r lang; do
words="$words $lang"
done < <(python3 -c "from importlib.machinery import SourceFileLoader;from importlib.util import module_from_spec, spec_from_loader;loader = SourceFileLoader('km_package_install', '$pkg_install_path');spec = spec_from_loader(loader.name, loader);mod = module_from_spec(spec);loader.exec_module(mod);mod.list_languages_for_keyboard('$packageId', '$packageDir')")
# shellcheck disable=SC2207
COMPREPLY=($(compgen -W "${words}" -- "${cur}"))
return 0
;;
*)
;;
esac
}
complete -F _km-package-install_completions km-package-install