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 #!/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' filter='/^.[i*]. /d'
filter1='s/^...\? //' filter1='s/^...\? //'
filter2='s/-[^- ]* / /' filter2='s/-[^- ]* / /'
# when available, use xrs(1)
# fallback to xbps-query -Rs
XRS="xbps-query -Rs" XRS="xbps-query -Rs"
command -v xrs >/dev/null && XRS="xrs" command -v xrs >/dev/null && XRS="xrs"
# when available, use xi(1)
# fallback to xbps-install
XI="xbps-install" XI="xbps-install"
command -v xi >/dev/null && XI="xi" 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() { install() {
sudo $XI "$@" echo "xif: $SUDO $XI $@"
echo
$SUDO $XI $@
} }
pkgs() { 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/ $//" cut -d' ' -f1 | tr '\n' ' ' | sed "s/ $//"
} }
PKGS="$(pkgs "$@")" PKGS="$(pkgs $@)"
[ -z $PKGS ] && exit 130 [ -z "$PKGS" ] && exit 130
SUDO=$(which_sudo)
install -S $PKGS
install -S "$PKGS"
status=$? status=$?
if [ $status -eq 16 ]; then if [ $status -eq 16 ]; then
install -u xbps && install -u xbps &&
install -S $(pkgs "$@") install -S $PKGS
else else
exit $status exit $status
fi fi