Last active
December 6, 2018 15:57
-
-
Save DanielFGray/c23ecf3c2e2bfd59abea to your computer and use it in GitHub Desktop.
package searching and installing with https://github.com/junegunn/fzf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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