fuzzy-pkg-finder/fpf

124 lines
5.7 KiB
Bash

#!/usr/bin/bash
### Help
Help() {
echo
echo "Use fzf to search and install with Pacman and Yay"
echo "Defaults to Pacman if no options passed"
echo
echo "Syntax: fpf -[a|l|la|R|o|h]"
echo
echo "options:"
echo
echo "a Search/List and install from AUR with Yay"
echo
echo "l Search/List installed packages from official repo"
echo
echo "la Search/List installed packages only from AUR repo"
echo
echo "R Search/List installed packages for removal"
echo
echo "o Search/List orphaned packages for removal"
echo
echo "h Print this help screen."
echo
}
### Official Repo
# Check things are up to date
UpdateInfos() {
[ -f /var/lib/pacman/sync/core.files ] || { echo -e "Syncing files database"; sudo pacman -Fy; }
d1=$(stat -c %y /var/lib/pacman/sync/core.files)
d2=$(stat -c %y /var/lib/pacman/sync/core.db)
d1="${d1:0:10}"
d2="${d2:0:10}"
[[ "${d2///-/}" > "${d1//-/}" ]] && { echo -e "Files database is out of date\nSyncing now..."; sudo pacman -Fy; }
}
# Get Official package list, sort, mark installed, preview infos and finally hand off to pacman for install
Official() {
echo "Setting things up..."
sort <(comm -23 <(pacman -Slq | sort) <(pacman -Qq | sort)) <(comm -12 <(pacman -Slq | sort) <(pacman -Qq | sort) | awk '{print $0" \033[32m*\033[0m"}') > /tmp/packages
echo "$(echo -e ' Select packages to install\n (use TAB to toggle selection)'; cat /tmp/packages)" > /tmp/packages
cat /tmp/packages |fzf -q "$1" -e -m --preview 'cat <(pacman -Si {1}) <(pacman -Fl {1} | awk "{print \$2}")' --preview-window=65%:wrap --layout=reverse --marker='>>' --header-lines=2 --info=hidden --ansi --margin="2%,1%,2%,1%" --cycle | sed -e 's/\*$//' | xargs -ro sudo pacman -S
}
# List installed pkgs
Installed() {
sort <(pacman -Qqs) > /tmp/installed_packages
echo "$(echo -e ' Select packages to print info\n (use TAB to toggle selection)'; cat /tmp/installed_packages)" > /tmp/installed_packages
cat /tmp/installed_packages |fzf -q "$1" -e -m --preview 'cat <(pacman -Qik {1}) <(echo "") <(pacman -Fl {1} | awk "{print \$2}")' --preview-window=65%:wrap --layout=reverse --marker='>>' --header-lines=2 --info=hidden --ansi --margin="2%,1%,2%,1%" --cycle | xargs -ro pacman -Qik
}
# Remove installed pkgs
Remove() {
sort <(pacman -Qqs) > /tmp/installed_packages
echo "$(echo -e ' Select packages to remove\n (use TAB to toggle selection)'; cat /tmp/installed_packages)" > /tmp/installed_packages
cat /tmp/installed_packages |fzf -q "$1" -e -m --preview 'cat <(pacman -Si {1} 2>/dev/null || yay -Qi {1}) <(pacman -Ql {1} | awk "{print \$2}")' --preview-window=65%:wrap --layout=reverse --marker='>>' --header-lines=2 --info=hidden --ansi --margin="2%,1%,2%,1%" --cycle | xargs -ro sudo pacman -Rsn
}
### AUR
# Get AUR package database, remove unwanted lines, sort, mark installed, preview infos and finally hand off to yay for install
Aur() {
echo "Setting things up..."
[ -f /tmp/aur/packages ] || { echo -e "Syncing AUR package database..."; wget -P /tmp/aur/ https://aur.archlinux.org/packages.gz >/dev/null 2>&1 && gunzip -f /tmp/aur/packages.gz; echo "$(tail -n +2 /tmp/aur/packages)" > /tmp/aur/packages; }
d1=$(stat -c %y /tmp/aur/packages)
d1="${d1:0:10}"
d2=$(date -I'date')
[[ "${d2///-/}" > "${d1//-/}" ]] && { echo -e "Syncing AUR package database..."; wget -P /tmp/aur/ https://aur.archlinux.org/packages.gz >/dev/null 2>&1 && gunzip -f /tmp/aur/packages.gz; echo "$(tail -n +2 /tmp/aur/packages)" > /tmp/aur/packages; }
sort <(comm -23 <(sort /tmp/aur/packages) <(pacman -Qq | sort)) <(comm -12 <(sort /tmp/aur/packages) <(pacman -Qq | sort) | awk '{print $0" \033[32m*\033[0m"}') > /tmp/aur_packages
echo "$(echo -e ' Select packages to install\n (use TAB to toggle selection)'; cat /tmp/aur_packages)" > /tmp/aur_packages
cat /tmp/aur_packages | fzf -q "$1" -e -m --preview 'cat <(yay -Si {1}) <(pacman -Ql {1} 2>/dev/null | awk "{print \$2}")' --preview-window=65%:wrap --layout=reverse --marker='>>' --header-lines=2 --info=hidden --ansi --margin="2%,1%,2%,1%" --cycle | sed -e 's/\*$//' | xargs -ro yay -S
}
# List installed pkgs only from AUR
AurInstalled() {
sort <(pacman -Qqm) > /tmp/aur_installed_packages
echo "$(echo -e ' Select packages to print info\n (use TAB to toggle selection)'; cat /tmp/aur_installed_packages)" > /tmp/aur_installed_packages
cat /tmp/aur_installed_packages |fzf -q "$1" -e -m --preview 'cat <(yay -Qi {1}) <(echo "") <(pacman -Ql {1} | awk "{print \$2}")' --preview-window=65%:wrap --layout=reverse --marker='>>' --header-lines=2 --info=hidden --ansi --margin="2%,1%,2%,1%" --cycle | xargs -ro pacman -Qik
}
### ORPHANS
# List orphaned packages and remove selected
Orphans() {
sort <(pacman -Qtdq) > /tmp/orphaned
echo "$(echo -e ' Select packages to remove\n (use TAB to toggle selection)'; cat /tmp/orphaned)" > /tmp/orphaned
cat /tmp/orphaned |fzf -q "$1" -e -m --preview 'cat <(pacman -Si {1} 2>/dev/null || yay -Qi {1}) <(pacman -Ql {1} | awk "{print \$2}")' --preview-window=65%:wrap --layout=reverse --marker='>>' --header-lines=2 --info=hidden --ansi --margin="2%,1%,2%,1%" --cycle | xargs -ro sudo pacman -Rsn
}
### MAIN
# Update the files database
UpdateInfos
# Test for AUR option, if not run with pacman
if [[ ! "$1" =~ "-" ]]; then
Official "$1"
else
for opt in "$@"; do
case $opt in
-a|--aur)
Aur "$2"
;;
-l|--list-installed)
Installed "$2"
;;
-la|--list-aur-installed)
AurInstalled "$2"
;;
-o|--orphans)
Orphans "$2"
;;
-R|--remove)
Remove "$2"
;;
-h|--help)
Help
;;
-*)
echo "Invalid Usage"
Help
;;
esac
done
fi