Skip to content

Instantly share code, notes, and snippets.

@varenc
Last active October 22, 2023 11:46
Show Gist options
  • Save varenc/50a8342dd230ef26b4fdcdc0db6d7a10 to your computer and use it in GitHub Desktop.
Save varenc/50a8342dd230ef26b4fdcdc0db6d7a10 to your computer and use it in GitHub Desktop.
Fast local homebrew `brew search` results. Returns first matches in 0.1 seconds instead of 10-30 seconds.
# Emulate `brew search` but make it fast. Search locally taps only.
# Results are returned immediately, but then the `brew info` is ran for each result which is slow.
function brewSearchFast() {
bold=$(tput bold)
normal=$(tput sgr0)
[[ -z $1 ]] && echo "Input missing" && return;
QUERY=$(echo $1 | tr '[:upper:]' '[:lower:]')
## replace that path with `$(brew --repository)/Library/Taps` if you don't mind an extra 30ms
RESULTS=$(fd $QUERY -e rb /usr/local/Homebrew/Library/Taps);
#RESULTS=$(find /usr/local/Homebrew/Library/Taps -iname "*${QUERY}*" -iname "*.rb")
[ -z $RESULTS ] && echo "No results found" && return;
echo "${bold}====> Results:${normal}"
while read line; do
fname="${line##*/}";
printf '%-35s %-20s \n' ${fname%.*} $line;
done <<< "$RESULTS";
sleep 1; ## pause to give time for Ctrl-C if `brew info` is not needed
## this could be faster with one `brew info ......` command, but this way gives each formula a title which I find much clearer
# brew info $(echo $RESULTS | sed -E "s/.*\/(.*)\.rb$/\1/g")
while read line; do
fname="${line##*/}";
echo -e "\n========${bold} ${fname%.*} ${normal}========";
brew info $line;
done <<< "$RESULTS";
}
alias hbs=brewSearchFast
# example:
# $ brewSearchFast wget
# ====> Results:
# wget /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/wget.rb
# wgetpaste /usr/local/Homebrew/Library/Taps/homebrew/homebrew-core/Formula/wgetpaste.rb
# wget2 /usr/local/Homebrew/Library/Taps/robbajorek/homebrew-formulae/wget2.rb
#
# ======== wget ========
# wget: stable 1.21 (bottled), HEAD
# Internet file retriever
# https://www.gnu.org/software/wget/
# /usr/local/Cellar/wget/1.20.3_2 (50 files, 4.0MB) *
# Poured from bottle on 2019-12-15 at 16:36:15
# From: https://github.com/Homebrew/homebrew-core/blob/HEAD/Formula/wget.rb
# License: GPL-3.0-or-later
# ==> Dependencies
# Build: pkg-config ✔
# Required: libidn2 ✔, [email protected]
# ==> Options
# --HEAD
# Install HEAD version
# ==> Analytics
# install: 106,074 (30 days), 353,948 (90 days), 1,565,650 (365 days)
# install-on-request: 105,624 (30 days), 352,568 (90 days), 1,554,964 (365 days)
# build-error: 0 (30 days)
# ======== wgetpaste ========
# ...
# ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment