Last active
June 16, 2025 14:26
-
-
Save pvdb/7d5478b5f3bb261f542adcea4bdc9c07 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#!/usr/bin/env bash | |
# | |
# INSTALLATION | |
# | |
# ln -s ${PWD}/brew-browse $(brew --prefix)/bin/ | |
# sudo ln -s ${PWD}/brew-browse /usr/local/bin/ | |
# | |
# PREREQUISITES | |
# | |
# brew install fzf | |
# | |
# USAGE | |
# | |
# search for Homebrew formulae and casks by description | |
# | |
# $ brew browse [fzf options] | |
# | |
# INSPIRATION | |
# | |
# https://github.com/borestad/dotjitsu/commit/656980488391bf5c36921b2b61607bc7d15b2bd1 | |
# | |
export CACHE_HOME=${XDG_CACHE_HOME:-$HOME/.cache}/brew-browse ; | |
[ -d "${CACHE_HOME}" ] || mkdir -p "${CACHE_HOME}" ; | |
delete_if_outdated() { | |
local one_week ; | |
one_week=$((60 * 60 * 24 * 7)) ; | |
local now ; | |
now=$(date +%s) ; | |
for file ; do | |
[ -f "${file}" ] || break ; | |
birth=$(stat -f '%B' "${file}") ; | |
age=$((now - birth)) ; | |
(( age >= one_week )) && { | |
> /dev/tty echo "Removing cached ${file}..." ; | |
rm -f "${file}" ; | |
} | |
done | |
} | |
FORMULA_CACHE="${CACHE_HOME}/formula.json" ; | |
delete_if_outdated "${FORMULA_CACHE}" ; | |
[ -f "${FORMULA_CACHE}" ] || { | |
> /dev/tty echo "Downloading Homebrew formula descriptions..." ; | |
curl -s https://formulae.brew.sh/api/formula.json > "${FORMULA_CACHE}" ; | |
} | |
CASK_CACHE="${CACHE_HOME}/cask.json" ; | |
delete_if_outdated "${CASK_CACHE}" ; | |
[ -f "${CASK_CACHE}" ] || { | |
> /dev/tty echo "Downloading Homebrew cask descriptions..." ; | |
curl -s https://formulae.brew.sh/api/cask.json > "${CASK_CACHE}" ; | |
} | |
( | |
< "${FORMULA_CACHE}" jq --raw-output '.[]|.tap + "|" + ( .name|.[0:20]) + "|" + .desc' ; | |
< "${CASK_CACHE}" jq --raw-output '.[]|.tap + "|" + (.token|.[0:20]) + "|" + .desc' ; | |
) \ | |
| column -t -s '|' \ | |
| fzf --reverse "$@" ; | |
false && { # fallback to searching local Homebrew Cellar | |
grep -E '^ *desc *".*"$' "$(brew --prefix)"/Cellar/*/*/.brew/*.rb \ | |
| perl -ne 'm{^(.*?)\.rb: *desc *"(.*)"$} and print "$1|$2\n"' \ | |
| column -t -s '|' \ | |
| fzf --reverse "$@" ; | |
} | |
# That's all Folks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment