m/fzf
1
0
mirror of https://github.com/junegunn/fzf.git synced 2025-11-16 23:33:39 -05:00

Updated Examples (markdown)

Ulysse Buonomo
2022-11-09 02:05:25 +01:00
parent 1a919a1c10
commit bb97b996f1

@@ -1235,66 +1235,91 @@ bcp() {
```
```sh
# Search and Install a formula or cask (enter to install)
# needs zsh, jq, curl
brewi() {
brewi_fetch() {
local url="$1"
local cache_key="$2"
local cache_folder="$HOME/.brewi.cache"
local file="$cache_folder/$cache_key"
if [[ "$file"(Nm-14) != "$file" ]]; then
2>& echo "Older than 2 weeks, cache reloading..."
mkdir -p $cache_folder
curl -s "$url" > "$file"
fi
echo "$file"
}
# Fully manage brew installation and suppression, and then some.
# needs zsh, jq, fzf, bat
# Inspired by:
# - https://github.com/raycast/extensions/tree/main/extensions/brew
# - https://github.com/junegunn/fzf/wiki/examples#dnf
local formula_file=$(brewi_fetch 'https://formulae.brew.sh/api/formula.json' 'formula')
local cask_file=$(brewi_fetch 'https://formulae.brew.sh/api/cask.json' 'cask')
readonly wait_click="echo $'\n\e[34mPress any key to continue...' && read -rsk 1"
readonly jq_all='
(. | map(.cask_tokens) | flatten | map(split("/")[-1] + " (cask)"))[]
, (. | map(.formula_names) | flatten)[]
'
{
jq --raw-output '.[] | .name' $formula_file
jq --raw-output '.[] | .token + " (cask)"' $cask_file
} | sort | \
fzf --reverse \
--prompt="Brew Install " \
--bind="enter:execute(
if [[ '{2}' == '(cask)' ]]; then
brew install --cask {1}
else
brew install {1}
fi
echo $'\n\e[34mPress any key to continue...' && read -rsk 1)+first+clear-query" \
--preview='brew info {1} | bat --color=always --language=Markdown --style=plain' \
--preview-window='bottom,wrap,<13(right)'
}
```
readonly jq_installed='(.formulae[] | .name), (.casks[] | .token + " (cask)")'
readonly tmp_file="$(mktemp)"
trap "rm -f $tmp_file" EXIT
readonly reload="reload%case \$(cat $tmp_file) in
install) { echo Install mode; brew tap-info --json --installed | jq --raw-output '$jq_all' | sort } ;;
*) { echo Remove mode; brew info --json=v2 --installed | jq --raw-output '$jq_installed' | sort } ;;
esac%"
readonly state="cat $tmp_file"
readonly nextstate="execute-silent%case \$(cat $tmp_file) in install) echo rm > $tmp_file ;; *) echo install > $tmp_file ;; esac%"
readonly bold="\e[1m"
readonly reset="\e[0m"
readonly italic="\e[3m"
readonly gray="\e[30m"
readonly c="\e[1;36m"
readonly d="\e[1;37m"
readonly help="\
\
${bold}${c}[${d}B${c}]${d}rew ${c}[${d}I${c}]${d}nteractive${reset}
${italic}Tab${reset} Switch between install mode and remove mode
${italic}Enter${reset} Select formula or cask for installation or deletion (depends on mode)
${italic}ctrl-s${reset} Show formula or cask installation [s]ource code
${italic}ctrl-j${reset} Show formula or cask [J]SON information
${italic}crtl-e${reset} [E]dit formula or cask source code
${italic}?${reset} Help (this page)
${italic}ESC${reset} Quit
It is also advised you use auto-updates, this can be done with:
brew autoupdate start --upgrade --cleanup --enable-notification
"
echo install > $tmp_file
{ echo "Install mode (? for help)"; brew tap-info --json --installed | jq --raw-output "$jq_all" | sort } |
fzf --reverse --header-lines=1 --header-first --prompt="$ " \
--bind="enter:execute(
if [[ '{2}' == '(cask)' ]]; then
brew \$($state) --cask {1}
else
brew \$($state) {1}
fi
$wait_click)+$reload" \
--bind='ctrl-s:preview(
bat --color=always $(brew edit --print-path {1}) --style=header
)' \
--bind="ctrl-j:preview:brew info --json=v2 {1} | jq '
(.formulae + .casks)[0] | with_entries(select(try (.value | length > 0)))
' | bat --plain --language=json --color=always" \
--bind="ctrl-e:execute:
EDITOR='code --wait' brew edit {1}
bat --color=always --language=markdown --plain <<-MD
To install the formulae (or cask) you edited with your changes, use:
brew reinstall --build-from-source {1}
MD
$wait_click" \
--bind="tab:$nextstate+$reload" \
--bind="?:preview:printf '$help'" \
--preview='brew info {1} | bat --color=always --language=Markdown --style=plain' \
--preview-window='bottom,wrap,<13(right)'
```sh
# Uninstall a formula or cask (on enter)
# needs zsh, jq
brewr() {
brew info --json=v2 --installed |
jq --raw-output '
(.formulae[] | .name),
(.casks[] | .token + " (cask)")
' | \
sort | \
fzf --reverse \
--prompt="Brew Remove " \
--bind="enter:execute(
if [[ '{2}' == '(cask)' ]]; then
brew uninstall --cask {1}
else
brew uninstall {1}
fi
echo $'\n\e[34mPress any key to continue...' && read -rsk 1 && printf '\n'
)+first+clear-query" \
--preview='brew info {1} | bat --color=always --language=Markdown --style=plain' \
--preview-window='bottom,wrap,<13(right)'
}
```