Skip to content

Instantly share code, notes, and snippets.

@DanielFGray DanielFGray/pkgsearch.sh
Last active Dec 6, 2018

Embed
What would you like to do?
package searching and installing with https://github.com/junegunn/fzf
#!/usr/bin/env bash
declare esc=$(printf '\033')
declare c_reset="${esc}[0m"
declare c_red="${esc}[31m"
declare c_green="${esc}[32m"
declare c_blue="${esc}[34m"
err() {
echo -e "${c_red}$@${c_reset}" >&2
}
die() {
[[ -n "$1" ]] && err "$1"
exit 1
}
has() {
command -v "$1" &> /dev/null
}
getCommandFallbacks() {
for arg in "$@"; do arg="${arg%% *}"
if has "$arg"; then
echo "$arg"
return 0
fi
done
return 1
}
declare search
has -v fzf || die
fzf() {
command fzf -e +s --multi --cycle --ansi --no-hscroll --inline-info "$@"
}
if [[ -f /etc/debian_version ]]; then
out=$(fzf --query="$*" < <( \
apt-cache search '' |
sed -u -r "s|^([^ ]+)|${c_green}\1${c_reset}|" ) )
mapfile -t pkgs < <( awk '{print $1}' <<< "$out" )
(( ${#pkgs} > 0 )) || exit
sudo $(getCommandFallbacks apt apt-get) install "${pkgs[@]}"
elif [[ -f /etc/arch-release ]]; then
search='pacman'
(( $# > 0)) && search=$(getCommandFallbacks pacaur yaourt packer apacman pacman)
out=$(fzf < <( $search -Ss "$@" |
awk '{
getline descr;
sub(/ */,"", descr);
repo = "[" blue gensub(/\/.*/, "", 1) reset "]";
name = green gensub(/.*\//, "", 1, $1) reset;
info = gensub(/[^ ]* /, "", 1);
print repo, name, info, descr;
}' blue="$c_blue" green="$c_green" reset="$c_reset"
)
)
mapfile -t pkgs < <(awk '{print $2}' <<< "$out")
(( ${#pkgs} > 0 )) || exit
echo "installing ${pkgs[*]}"
${search/pacman/sudo pacman} -S "${pkgs[@]}"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.