Skip to content

Instantly share code, notes, and snippets.

@hashhar
Last active July 4, 2017 15:21
Show Gist options
  • Save hashhar/83053afdf4c829e8d41f05d321e87ae7 to your computer and use it in GitHub Desktop.
Save hashhar/83053afdf4c829e8d41f05d321e87ae7 to your computer and use it in GitHub Desktop.
Bash Completion Scripts
# Written: Ashhar Hasan (hashhar_dev at outlook dot com) on 13 April 2017
_cache_file="${XDG_CACHE_HOME:=~/.cache}/aurvote/packages.gz"
# If cache file is older than a week or doesn't exist yet, update it.
_setup_cache()
{
local last_update=1
[ -e "${_cache_file}" ] && last_update=$(stat -c %Y "${_cache_file}")
# Compare the epoch times of existing file and current date.
if (( $(date +%s) > last_update + 604800 )); then
# Download without any output, timeout early and create destination.
curl --create-dirs --connect-timeout 5 -s https://aur.archlinux.org/packages.gz -o "${_cache_file}"
fi
}
_aurvote()
{
local cur="${COMP_WORDS[COMP_CWORD]}"
local prev="${COMP_WORDS[COMP_CWORD-1]}"
local opts='-V --version -h --help -c --check -v --vote -u --unvote --configure'
COMPREPLY=()
_setup_cache
case "${prev}" in
-c|--check|-v|--vote|-u|--unvote)
# The tail +2 is there to exclude the comment at the start of file
local aur_pkgs=$(gzip -cd ${_cache_file} | tail +2 | sort )
COMPREPLY=( $(compgen -W "${aur_pkgs}" -- "${cur}") )
return 0
;;
esac
COMPREPLY=( $(compgen -W "${opts}" -- ${cur}) )
return 0
}
complete -F _aurvote aurvote
# vim: et sts=4 ts=4 sw=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment