Skip to content

Instantly share code, notes, and snippets.

@hashhar

hashhar/_aurvote

Last active Jul 4, 2017
Embed
What would you like to do?
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