Skip to content

Instantly share code, notes, and snippets.

@Ashark
Last active September 1, 2019 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Ashark/e0e031545f5b397bfbb047d14ce352c9 to your computer and use it in GitHub Desktop.
Save Ashark/e0e031545f5b397bfbb047d14ce352c9 to your computer and use it in GitHub Desktop.
Bash completion for yay -Uc feature request - wip
# This file is in the public domain.
_arch_compgen() {
local i r
COMPREPLY=($(compgen -W '$*' -- "$cur"))
for ((i=1; i < ${#COMP_WORDS[@]}-1; i++)); do
for r in ${!COMPREPLY[@]}; do
if [[ ${COMP_WORDS[i]} = ${COMPREPLY[r]} ]]; then
unset 'COMPREPLY[r]'; break
fi
done
done
}
_arch_ptr2comp() {
local list= x y
for x; do
for y in '0 --' '1 -'; do
eval 'set -- ${'$x'[${y% *}]}'
list+=\ ${@/#/${y#* }}
done
done
_arch_compgen $list
}
_arch_incomp() {
local r="\s-(-${1#* }\s|\w*${1% *})"; [[ $COMP_LINE =~ $r ]]
}
_pacman_pkg() {
_arch_compgen "$(
if [[ $2 ]]; then
\pacman -$1 2>/dev/null | \cut -d' ' -f1 | \sort -u
else
\pacman -$1 2>/dev/null
fi
)"
}
_yay_pkg() {
[ -z "$cur" ] && return
_arch_compgen "$(yay -Pc)"
}
_yay_uc()
{
_foundFiles=($(compgen -W "$(find $HOME/.cache/yay/ -name "*.pkg.tar.*" -printf '%f\n' 2>/dev/null)" "${COMP_WORDS[$COMP_CWORD]}"))
_stripped_found_files=()
for file in ${_foundFiles[@]}; do
# file name example: vlc-3.0.7.1-2-x86_64.pkg.tar.xz
_stripped_found_files+=($(echo $file | sed -r 's/(.*)-(.*)-(.*)-(.*)$/\1/'))
done
COMPREPLY=(${_stripped_found_files[@]})
}
_pacman_repo_list() {
_arch_compgen "$(pacman-conf --repo-list)"
}
_yay() {
local common core cur database files prev query remove sync upgrade o
local yays show getpkgbuild
COMPREPLY=()
_get_comp_words_by_ref cur prev
database=('asdeps asexplicit')
files=('list machinereadable owns search refresh regex' 'l o s x y')
query=('changelog check deps explicit file foreign groups info list native owns
search unrequired upgrades' 'c e g i k l m n o p s t u')
remove=('cascade dbonly nodeps assume-installed nosave print recursive unneeded' 'c n p s u')
sync=('asdeps asexplicit clean dbonly downloadonly force groups ignore ignoregroup
info list needed nodeps assume-installed print refresh recursive search sysupgrade'
'c g i l p s u w y')
upgrade=('asdeps asexplicit force needed nodeps assume-installed print recursive' 'p')
common=('arch cachedir color config confirm dbpath debug gpgdir help hookdir logfile
noconfirm noprogressbar noscriptlet quiet root verbose
#yay stuff
makepkg pacman tar git gpg gpgflags config requestsplitn sudoloop nosudoloop
redownload noredownload redownloadall rebuild rebuildall rebuildtree norebuild
sortby answerclean answerdiff answeredit answerupgrade noanswerclean noanswerdiff
noansweredit noanswerupgrade cleanmenu diffmenu editmenu upgrademenu cleanafter nocleanafter
nocleanmenu nodiffmenu noupgrademenu provides noprovides pgpfetch nopgpfetch
useask nouseask combinedupgrade nocombinedupgrade aur repo makepkgconf
nomakepkgconf askremovemake removemake noremovemake completioninterval aururl'
'b d h q r v')
core=('database files help query remove sync upgrade version' 'D F Q R S U V h')
##yay stuff
yays=('clean gendb' 'c')
show=('complete defaultconfig currentconfig stats news' 'c d g s w')
getpkgbuild=('force' 'f')
for o in 'D database' 'F files' 'Q query' 'R remove' 'S sync' 'U upgrade' 'Y yays' 'P show' 'G getpkgbuild'; do
_arch_incomp "$o" && break
done
if [[ $? != 0 ]]; then
_arch_ptr2comp core
elif [[ ! $prev =~ ^-\w*[Vbhr] &&
! $prev = --@(cachedir|color|config|dbpath|help|hookdir|gpgdir|logfile|root|version) ]]
then
[[ $cur = -* ]] && _arch_ptr2comp ${o#* } common ||
case ${o% *} in
D|R)
_pacman_pkg Qq;;
F)
_arch_incomp 'l list' && _pacman_pkg Slq;
;;
Q)
{ _arch_incomp 'g groups' && _pacman_pkg Qg sort; } ||
{ _arch_incomp 'p file' && _pacman_file; } ||
_arch_incomp 'o owns' || _arch_incomp 'u upgrades' ||
_pacman_pkg Qq;;
S)
{ _arch_incomp 'g groups' && _pacman_pkg Sg; } ||
{ _arch_incomp 'l list' && _pacman_repo_list; } ||
_yay_pkg;;
G)
_yay_pkg;;
U)
{ _arch_incomp 'c' && _yay_uc; } ||
_pacman_file;;
esac
fi
true
}
_pacman_file() {
compopt -o filenames; _filedir 'pkg.tar*'
}
complete -F _yay -o default yay
# ex:et ts=2 sw=2 ft=sh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment