Last active
November 18, 2024 03:39
-
-
Save alpipego/ec45d35e1fdc3dc10a5cbe821f588800 to your computer and use it in GitHub Desktop.
Streamline Homebrew search-info-install
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
#!/bin/bash | |
# Function to prompt for package installation | |
prompt_install() { | |
local pkg=$1 | |
brew info "$pkg" | |
read -p "Do you want to install $pkg? [Y/n] " answer | |
if [[ "$answer" == "Y" ]] || [[ "$answer" == "y" ]] || [[ -z "$answer" ]]; then | |
brew install "$pkg" | |
else | |
echo "Installation cancelled for $pkg." | |
return 1 | |
fi | |
} | |
# Fetch the list of installed packages once | |
INSTALLED=($(brew list)) | |
# Get the package name from the argument | |
PKG=$1 | |
# Search for formulae and casks separately | |
SEARCH_FORMULAE=$(brew search --formulae "$PKG" 2>/dev/null) | |
SEARCH_CASKS=$(brew search --casks "$PKG" 2>/dev/null) | |
# Define ANSI color codes | |
RESET_COLOR='\033[0m' | |
GRAY_COLOR='\033[90m' | |
GREEN_COLOR='\033[32m' | |
# Process formulae search results | |
for PACKAGE in $SEARCH_FORMULAE; do | |
if printf '%s\n' "${INSTALLED[@]}" | grep -qx "$PACKAGE"; then | |
PACKAGES+=("$PACKAGE ${GRAY_COLOR}(brew)${RESET_COLOR} ${GREEN_COLOR}✔︎${RESET_COLOR}") | |
else | |
PACKAGES+=("$PACKAGE ${GRAY_COLOR}(brew)${RESET_COLOR}") | |
fi | |
done | |
# Process casks search results | |
for PACKAGE in $SEARCH_CASKS; do | |
if printf '%s\n' "${INSTALLED[@]}" | grep -qx "$PACKAGE"; then | |
PACKAGES+=("$PACKAGE ${GRAY_COLOR}(cask)${RESET_COLOR} ${GREEN_COLOR}✔︎${RESET_COLOR}") | |
else | |
PACKAGES+=("$PACKAGE ${GRAY_COLOR}(cask)${RESET_COLOR}") | |
fi | |
done | |
# If there are no matches, show an error message | |
if [ ${#PACKAGES[@]} -eq 0 ]; then | |
echo "No packages found for $PKG." | |
elif [ ${#PACKAGES[@]} -eq 1 ]; then | |
# If there is only one match, show the info and ask for confirmation | |
prompt_install "${PACKAGES[0]%% *}" | |
else | |
# If there are multiple matches, present a list and let the user choose | |
while true; do | |
echo -e "Multiple packages found. Select a package:" | |
COLUMNS=1 | |
# Manual loop for selection since select doesn't handle ANSI codes well | |
select_index=1 | |
for package in "${PACKAGES[@]}"; do | |
echo -e "$select_index) $package" | |
select_index=$((select_index + 1)) | |
done | |
echo -n "Install number #: " | |
read selection | |
if [[ "$selection" =~ ^[0-9]+$ ]] && [ "$selection" -ge 1 ] && [ "$selection" -le "${#PACKAGES[@]}" ]; then | |
CHOICE="${PACKAGES[$((selection-1))]%% *}" # Remove additional info before installing | |
if prompt_install "$CHOICE"; then | |
break | |
fi | |
else | |
echo -e "Invalid choice. Please enter a valid number." | |
fi | |
done | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this bash script to streamline your Homebrew package installation process. It searches for a package, presents install options, and simplifies batch installations with user confirmation for macOS.
Installation Instructions:
$PATH
:mv /path/to/script.sh ~/.local/bin/
.chmod +x /path/to/script.sh
.script.sh <package-name>
from any terminal window.For detailed usage and explanation, check out the blog post: https://www.alexandergoller.com/journal/14694/streamline-homebrew-search-info-install