xif: complete command

This commit is contained in:
fanyx 2024-02-28 16:23:22 +01:00
parent cbe21d0f6a
commit 3ed6e5b8e2
1 changed files with 27 additions and 11 deletions

38
xif
View File

@ -1,37 +1,53 @@
#!/bin/sh
# vim: set ts=4 sw=4 et:
[ -z "$@" ] && echo "xif: empty search term" && exit 1
command -v fzf >/dev/null || sudo xbps-install -S fzf
[ -z "$*" ] && echo "xif: empty search term" && exit 1
filter='/^.[i*]. /d'
filter1='s/^...\? //'
filter2='s/-[^- ]* / /'
# when available, use xrs(1)
# fallback to xbps-query -Rs
XRS="xbps-query -Rs"
command -v xrs >/dev/null && XRS="xrs"
# when available, use xi(1)
# fallback to xbps-install
XI="xbps-install"
command -v xi >/dev/null && XI="xi"
which_sudo() {
if [ "$(id -u)" = "0" ]; then
return
elif command -v sudo >/dev/null && sudo -l | grep -q -e ' ALL$' -e xbps-install; then
echo sudo
elif command -v doas >/dev/null && [ -f /etc/doas.conf ]; then
echo doas
else
echo su
fi
}
install() {
sudo $XI "$@"
echo "xif: $SUDO $XI $@"
echo
$SUDO $XI $@
}
pkgs() {
$XRS "$@" | sed "$filter;$filter1;$filter2" | fzf --layout=reverse -m |\
echo $@ | tr ' ' '\n' | xargs -i $XRS {} | uniq |\
sed "$filter;$filter1;$filter2" | fzf --layout=reverse -m |\
cut -d' ' -f1 | tr '\n' ' ' | sed "s/ $//"
}
PKGS="$(pkgs "$@")"
[ -z $PKGS ] && exit 130
PKGS="$(pkgs $@)"
[ -z "$PKGS" ] && exit 130
SUDO=$(which_sudo)
install -S $PKGS
install -S "$PKGS"
status=$?
if [ $status -eq 16 ]; then
install -u xbps &&
install -S $(pkgs "$@")
install -S $PKGS
else
exit $status
fi