feat: Improve dependency management with user prompt for installation

- Added functionality to detect missing dependencies and list them all at once.
- Prompted the user to confirm installation of all missing dependencies in a single step.
- Enhanced the  function to handle installation based on the Linux distribution.
- Ensured the script exits gracefully if the user declines to install dependencies.
- Improved user experience by reducing repetitive prompts for each missing dependency.
This commit is contained in:
Dalosuuu
2025-04-04 04:12:26 -04:00
parent f3504fb0ce
commit 284f351391

View File

@@ -14,36 +14,65 @@
# #{ CHECK DEPENDENCIES # #{ CHECK DEPENDENCIES
VIM_BIN="$(whereis -b vim | awk '{print $2}')" # Detect Linux distribution
NVIM_BIN="$(whereis -b nvim | awk '{print $2}')" DISTRO=$(lsb_release -is 2>/dev/null || echo "Unkown")
JQ_BIN="$(whereis -b jq | awk '{print $2}')"
XDOTOOL_BIN="$(whereis -b xdotool | awk '{print $2}')"
XRANDR_BIN="$(whereis -b xrandr | awk '{print $2}')"
ROFI_BIN="$(whereis -b rofi | awk '{print $2}')"
if [ -z "$NVIM_BIN" ] && [ -z "$VIM_BIN" ]; then # Function to install a package based on the detected DISTRO
echo missing vim or neovim, please install dependencies install_package(){
exit 1 PACKAGE=$1
case "$DISTRO" in
Ubuntu|Debian)
sudo apt update && sudo apt install -y "$PACKAGE"
;;
Fedora)
sudo dnf install -y "$PACKAGE"
;;
openSUSE|SUSE)
sudo zypper install -y "$PACKAGE"
;;
Arch)
sudo pacman -Syu "$PACKAGE" --noconfirm
;;
*)
echo "Unsupported distribution: $DISTRO"
exit 1
;;
esac
}
# Check dependencies and collect missing ones
MISSING_DEPENDENCIES=()
check_dependency() {
BIN_PATH=$(whereis -b "$1" | awk '{print $2}')
if [ -z "$BIN_PATH" ]; then
MISSING_DEPENDENCIES+=("$2")
fi
}
# List of dependencies and their package names
check_dependency "vim" "vim"
check_dependency "nvim" "neovim"
check_dependency "jq" "jq"
check_dependency "xdotool" "xdotool"
check_dependency "xrandr" "xrandr"
check_dependency "rofi" "rofi"
# Ensure at least one editor is available
if [ -z "$(whereis -b vim | awk '{print $2}')" ] && [ -z "$(whereis -b nvim | awk '{print $2}')" ]; then
MISSING_DEPENDENCIES+=("vim")
fi fi
if [ -z "$JQ_BIN" ]; then # Prompt to install missing dependencies
echo missing jq, please install dependencies if [ ${#MISSING_DEPENDENCIES[@]} -gt 0 ]; then
exit 1 echo "The following dependencies are missing: ${MISSING_DEPENDENCIES[*]}"
fi read -p "Do you want to install them? [y/N]: " RESPONSE
if [[ "$RESPONSE" =~ ^[Yy]$ ]]; then
if [ -z "$XDOTOOL_BIN" ]; then for PACKAGE in "${MISSING_DEPENDENCIES[@]}"; do
echo missing xdotool, please install dependencies install_package "$PACKAGE"
exit 1 done
fi else
echo "Skipping installation of dependencies. The script may not work as expected."
if [ -z "$XRANDR_BIN" ]; then exit 1
echo missing xrandr, please install dependencies
exit 1
fi
if [ -z "$ROFI_BIN" ]; then
echo missing rofi, please install dependencies
exit 1
fi fi
# #} # #}