Skip to content

Commit

Permalink
add shell completion build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjonbrazil committed Jul 5, 2022
1 parent d0bde14 commit eeee776
Show file tree
Hide file tree
Showing 6 changed files with 424 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG
@@ -1,6 +1,6 @@
jc changelog

20220630 v1.20.2
20220705 v1.20.2
- Add `gpg --with-colons` parser tested on linux
- Add DER and PEM encoded X.509 Certificate parser
- Add Bash and Zsh completion scripts to DEB and RPM packages
Expand Down
9 changes: 9 additions & 0 deletions build-completions.py
@@ -0,0 +1,9 @@
#!/usr/bin/env python3
# build Bash and Zsh completion scripts and add to the completions folder
from jc.shell_completions import bash_completion, zsh_completion

with open('completions/jc_bash_completion.sh', 'w') as f:
print(bash_completion(), file=f)

with open('completions/jc_zsh_completion.sh', 'w') as f:
print(zsh_completion(), file=f)
84 changes: 84 additions & 0 deletions completions/jc_bash_completion.sh
@@ -0,0 +1,84 @@
_jc()
{
local cur prev words cword jc_commands jc_parsers jc_options \
jc_about_options jc_about_mod_options jc_help_options jc_special_options

jc_commands=(acpi airport arp blkid chage cksum crontab date df dig dmidecode dpkg du env file finger free git gpg hciconfig id ifconfig iostat iptables iw jobs last lastb ls lsblk lsmod lsof lsusb md5 md5sum mount mpstat netstat nmcli ntpq pidstat ping ping6 pip pip3 postconf printenv ps route rpm rsync sfdisk sha1sum sha224sum sha256sum sha384sum sha512sum shasum ss stat sum sysctl systemctl systeminfo timedatectl top tracepath tracepath6 traceroute traceroute6 ufw uname update-alternatives upower uptime vdir vmstat w wc who xrandr zipinfo)
jc_parsers=(--acpi --airport --airport-s --arp --asciitable --asciitable-m --blkid --chage --cksum --crontab --crontab-u --csv --csv-s --date --df --dig --dir --dmidecode --dpkg-l --du --env --file --finger --free --fstab --git-log --git-log-s --gpg --group --gshadow --hash --hashsum --hciconfig --history --hosts --id --ifconfig --ini --iostat --iostat-s --iptables --iw-scan --jar-manifest --jobs --kv --last --ls --ls-s --lsblk --lsmod --lsof --lsusb --mount --mpstat --mpstat-s --netstat --nmcli --ntpq --passwd --pidstat --pidstat-s --ping --ping-s --pip-list --pip-show --postconf --ps --route --rpm-qi --rsync --rsync-s --sfdisk --shadow --ss --stat --stat-s --sysctl --systemctl --systemctl-lj --systemctl-ls --systemctl-luf --systeminfo --time --timedatectl --top --top-s --tracepath --traceroute --ufw --ufw-appinfo --uname --update-alt-gs --update-alt-q --upower --uptime --vmstat --vmstat-s --w --wc --who --x509-cert --xml --xrandr --yaml --zipinfo)
jc_options=(--force-color -C --debug -d --monochrome -m --pretty -p --quiet -q --raw -r --unbuffer -u --yaml-out -y)
jc_about_options=(--about -a)
jc_about_mod_options=(--pretty -p --yaml-out -y --monochrome -m --force-color -C)
jc_help_options=(--help -h)
jc_special_options=(--version -v --bash-comp -B --zsh-comp -Z)

COMPREPLY=()
_get_comp_words_by_ref cur prev words cword

# if jc_about_options are found anywhere in the line, then only complete from jc_about_mod_options
for i in "${words[@]::${#words[@]}-1}"; do
if [[ " ${jc_about_options[*]} " =~ " ${i} " ]]; then
COMPREPLY=( $( compgen -W "${jc_about_mod_options[*]}" \
-- "${cur}" ) )
return 0
fi
done

# if jc_help_options and a parser are found anywhere in the line, then no more completions
if
(
for i in "${words[@]::${#words[@]}-1}"; do
if [[ " ${jc_help_options[*]} " =~ " ${i} " ]]; then
return 0
fi
done
return 1
) && (
for i in "${words[@]::${#words[@]}-1}"; do
if [[ " ${jc_parsers[*]} " =~ " ${i} " ]]; then
return 0
fi
done
return 1
); then
return 0
fi

# if jc_help_options are found anywhere in the line, then only complete with parsers
for i in "${words[@]::${#words[@]}-1}"; do
if [[ " ${jc_help_options[*]} " =~ " ${i} " ]]; then
COMPREPLY=( $( compgen -W "${jc_parsers[*]}" \
-- "${cur}" ) )
return 0
fi
done

# if special options are found anywhere in the line, then no more completions
for i in "${words[@]::${#words[@]}-1}"; do
if [[ " ${jc_special_options[*]} " =~ " ${i} " ]]; then
return 0
fi
done

# if magic command is found anywhere in the line, use called command's autocompletion
for i in "${words[@]::${#words[@]}-1}"; do
if [[ " ${jc_commands[*]} " =~ " ${i} " ]]; then
_command
return 0
fi
done

# if a parser arg is found anywhere in the line, only show options and help options
for i in "${words[@]::${#words[@]}-1}"; do
if [[ " ${jc_parsers[*]} " =~ " ${i} " ]]; then
COMPREPLY=( $( compgen -W "${jc_options[*]} ${jc_help_options[*]}" \
-- "${cur}" ) )
return 0
fi
done

# default completion
COMPREPLY=( $( compgen -W "${jc_options[*]} ${jc_about_options[*]} ${jc_help_options[*]} ${jc_special_options[*]} ${jc_parsers[*]} ${jc_commands[*]}" \
-- "${cur}" ) )
} &&
complete -F _jc jc

0 comments on commit eeee776

Please sign in to comment.