You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

139 lines
3.2 KiB

#!/usr/bin/env bash
declare esc=$'\033'
declare c_reset="${esc}[0m"
declare c_red="${esc}[31m"
declare c_green="${esc}[32m"
declare c_blue="${esc}[34m"
declare reverse
usage() {
LESS=-FEXR less <<HELP
pkgsearch [options] [query]
lists and installs packages from your distro's repositories
-r --reverse reverse the orientation of the layout
-h --help show this help
without any arguments pkgsearch will list all available packages from your cache
note: on Arch Linux you must pass a string to query the AUR
HELP
}
err() {
printf "${c_red}%s${c_reset}\n" "$*" >&2
}
die() {
exit 1
}
has() {
local verbose=0
if [[ $1 == '-v' ]]; then
verbose=1
shift
fi
for c; do c="${c%% *}"
if ! command -v "$c" &> /dev/null; then
(( "$verbose" > 0 )) && err "$c not found"
return 1
fi
done
}
select_from() {
local cmd='command -v'
for a; do
case "$a" in
-c)
cmd="$2"
shift 2
;;
esac
done
for c; do
if $cmd "${c%% *}" &> /dev/null; then
echo "$c"
return 0
fi
done
return 1
}
fzf() {
command fzf -e +s --multi --cycle --ansi \
${reverse:+--reverse} \
--no-hscroll --inline-info \
--header='tab to select multiple packages, Ctrl-X for more info on a package' "$@"
}
install() {
local pkgs count
mapfile -t pkgs
(( ${#pkgs} > 0 )) || exit
count="${#pkgs[@]} package"
(( ${#pkgs[@]} > 1 )) && count+='s'
printf "installing %s: %s\n" "$count" "${pkgs[*]}"
$1 "${pkgs[@]}" < /dev/tty
}
debian() {
fzf --bind="Ctrl-X:execute[
p=\$(cut -d' ' -f1 <<< {});
less < <(apt-cache show \$p) > /dev/tty]" \
--query="$1" \
< <(apt-cache search '' | sort |
sed -u -r "s|^([^ ]+)|${c_green}\1${c_reset}|") |
cut -d' ' -f1 |
install "sudo $(select_from 'apt' 'aptitude' 'apt-get') install"
}
arch() {
local search packages
search='pacman'
[[ -n "$1" ]] && search=$(select_from 'pacaur' 'yaourt' 'packer' 'apacman' 'pacman')
packages=$(fzf --bind="Ctrl-X:execute[
p=\$(cut -d' ' -f2 <<< {});
less < <($search -Si \$p) > /dev/tty]" \
< <( $search -Ss "$1" |
gawk '{
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"
) | cut -d' ' -f2)
[[ "$search" == "pacman" ]] && search="sudo pacman"
install "$search -S" <<< "$packages"
}
void() {
local package_list packagename='{ sub(/-[^\-]*$/, "", $2); print $2 }'
package_list=$(xbps-query -Rs '' | sort)
fzf --bind="Ctrl-X:execute[
p=\$(awk \"$packagename\" <<< {});
less < <(xbps-query -Rx \$p) > /dev/tty]" <<< "$package_list" |
awk "$packagename" |
install 'xbps-install -S'
}
while true; do
case "$1" in
-r|--reverse) reverse=1 ; shift ;;
-h|--help) usage ; exit ;;
*) break
esac
done
has -v fzf gawk || die
request="$*"
osrelease=$(awk -F'=' '/^NAME/{gsub(/"/, "", $2); print tolower($2)}' /etc/os-release)
case "${osrelease%% *}" in
debian|ubuntu) debian "$request" ;;
arch) arch "$request" ;;
void) void "$request" ;;
esac