forked from gumob/fzf-brew
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfzf-brew.sh
150 lines (133 loc) · 5.14 KB
/
fzf-brew.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
function fzf-brew() {
######################
### Option Parser
######################
local __parse_options (){
local prompt="$1" && shift
local option_list
if [[ "$SHELL" == *"/bin/zsh" ]]; then
option_list=("$@")
elif [[ "$SHELL" == *"/bin/bash" ]]; then
local -n arr_ref=$1
option_list=("${arr_ref[@]}")
fi
### Select the option
selected_option=$(printf "%s\n" "${option_list[@]}" | fzf --ansi --prompt="${prompt} > ")
if [[ -z "$selected_option" || "$selected_option" =~ ^[[:space:]]*$ ]]; then
return 1
fi
### Normalize the option list
local option_list_normal=()
for option in "${option_list[@]}"; do
# Remove $(tput bold) and $(tput sgr0) from the string
option_normalized="${option//$(tput bold)/}"
option_normalized="${option_normalized//$(tput sgr0)/}"
# Add the normalized string to the new array
option_list_normal+=("$option_normalized")
done
### Get the index of the selected option
index=$(printf "%s\n" "${option_list_normal[@]}" | grep -nFx "$selected_option" | cut -d: -f1)
if [ -z "$index" ]; then
return 1
fi
### Generate the command
command=""
if [[ "$SHELL" == *"/bin/zsh" ]]; then
command="${option_list_normal[$index]%%:*}"
elif [[ "$SHELL" == *"/bin/bash" ]]; then
command="${option_list_normal[$index-1]%%:*}"
else
echo "Error: Unsupported shell. Please use bash or zsh to use fzf-brew."
return 1
fi
echo $command
return 0
}
######################
### Brew
######################
local FB_FORMULA_PREVIEW='HOMEBREW_COLOR=true brew info {}'
local FB_FORMULA_BIND="ctrl-space:execute-silent(brew home {})"
local FB_CASK_PREVIEW='HOMEBREW_COLOR=true brew info --cask {}'
local FB_CASK_BIND="ctrl-space:execute-silent(brew home --cask {})"
local fzf-brew-install-formula() {
local inst=$(brew formulae | fzf --query="$1" --multi --preview $FB_FORMULA_PREVIEW --bind $FB_FORMULA_BIND --prompt="brew install > ")
if [[ $inst ]]; then
for prog in $(echo $inst); do; brew install $prog; done;
fi
}
local fzf-brew-uninstall-formula() {
local uninst=$(brew leaves | fzf --query="$1" --multi --preview $FB_FORMULA_PREVIEW --bind $FB_FORMULA_BIND --prompt="brew uninstall > ")
if [[ $uninst ]]; then
for prog in $(echo $uninst); do; brew uninstall $prog; done;
fi
}
local fzf-brew-install-cask() {
local inst=$(brew casks | fzf --query="$1" --multi --preview $FB_CASK_PREVIEW --bind $FB_CASK_BIND --prompt="brew install --cask > ")
if [[ $inst ]]; then
for prog in $(echo $inst); do; brew install --cask $prog; done;
fi
}
local fzf-brew-uninstall-cask() {
local inst=$(brew list --cask | fzf --query="$1" --multi --preview $FB_CASK_PREVIEW --bind $FB_CASK_BIND --prompt="brew uninstall --cask > ")
if [[ $inst ]]; then
for prog in $(echo $inst); do; brew uninstall --cask $prog; done;
fi
}
local fzf-brew-upgrade() {
brew update --quiet
local res=$(brew outdated --formula | fzf --query="$1" --multi --preview $FB_FORMULA_PREVIEW --bind $FB_FORMULA_BIND --prompt="brew upgrade > ")
if [[ $res ]]; then
for prog in $(echo $res); do; brew upgrade $prog; done;
fi
}
local fzf-brew-upgrade-cask() {
brew update --quiet
local inst=$(brew outdated --cask | fzf --query="$1" --multi --preview $FB_CASK_PREVIEW --bind $FB_CASK_BIND --prompt="brew upgrade --cask > ")
if [[ $inst ]]; then
for prog in $(echo $inst); do; brew upgrade --cask $prog; done;
fi
}
######################
### Entry Point
######################
local init() {
local option_list=(
"$(tput bold)install:$(tput sgr0) Install a formula."
"$(tput bold)uninstall:$(tput sgr0) Uninstall a formula."
" "
"$(tput bold)install cask:$(tput sgr0) Install a cask."
"$(tput bold)uninstall cask:$(tput sgr0) Uninstall a cask."
" "
"$(tput bold)upgrade:$(tput sgr0) Upgrade a formula."
"$(tput bold)upgrade all:$(tput sgr0) Upgrade all formulae."
" "
"$(tput bold)upgrade cask:$(tput sgr0) Upgrade a cask."
"$(tput bold)upgrade all cask:$(tput sgr0) Upgrade all casks."
" "
"$(tput bold)list:$(tput sgr0) List installed formulae and casks."
)
command=$(__parse_options "brew" ${option_list[@]})
if [ $? -eq 1 ]; then
zle accept-line
zle -R -c
return 1
fi
case "$command" in
"install") fzf-brew-install-formula;;
"uninstall") fzf-brew-uninstall-formula;;
"install cask") fzf-brew-install-cask;;
"uninstall cask") fzf-brew-uninstall-cask;;
"upgrade") fzf-brew-upgrade;;
"upgrade all") brew update --quiet && brew outdated && brew upgrade && brew cleanup;;
"upgrade cask") fzf-brew-upgrade-cask;;
"upgrade all cask") brew update --quiet && brew outdated --cask && brew upgrade --cask --greedy && brew cleanup;;
*) echo "Error: Unknown command '$command'" ;;
esac
zle accept-line
zle -R -c
}
init
}
zle -N fzf-brew
bindkey "${FZF_BREW_KEY_BINDING}" fzf-brew