Debian Codegraph @

install

raw
1 #!/usr/bin/env bash
2
3 set -u
4
5 version=0.74.4
6 auto_completion=
7 key_bindings=
8 update_config=2
9 shells="bash zsh fish nushell"
10 prefix='~/.fzf'
11 prefix_expand=~/.fzf
12 fish_dir=${XDG_CONFIG_HOME:-$HOME/.config}/fish
13
14 help() {
15 cat << EOF
16 usage: $0 [OPTIONS]
17
18 --help Show this message
19 --bin Download fzf binary only; Do not generate ~/.fzf.{bash,zsh}
20 --all Download fzf binary and update configuration files
21 to enable key bindings and fuzzy completion
22 --xdg Generate files under \$XDG_CONFIG_HOME/fzf
23 --[no-]key-bindings Enable/disable key bindings (CTRL-T, CTRL-R, ALT-C)
24 --[no-]completion Enable/disable fuzzy completion (bash & zsh)
25 --[no-]update-rc Whether or not to update shell configuration files
26
27 --no-bash Do not set up bash configuration
28 --no-zsh Do not set up zsh configuration
29 --no-fish Do not set up fish configuration
30 --no-nushell Do not set up nushell configuration
31 EOF
32 }
33
34 for opt in "$@"; do
35 case $opt in
36 --help)
37 help
38 exit 0
39 ;;
40 --all)
41 auto_completion=1
42 key_bindings=1
43 update_config=1
44 ;;
45 --xdg)
46 prefix='"${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf'
47 prefix_expand=${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf
48 mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/fzf"
49 ;;
50 --key-bindings) key_bindings=1 ;;
51 --no-key-bindings) key_bindings=0 ;;
52 --completion) auto_completion=1 ;;
53 --no-completion) auto_completion=0 ;;
54 --update-rc) update_config=1 ;;
55 --no-update-rc) update_config=0 ;;
56 --bin) ;;
57 --no-bash) shells=${shells/bash/} ;;
58 --no-zsh) shells=${shells/zsh/} ;;
59 --no-fish) shells=${shells/fish/} ;;
60 --no-nushell) shells=${shells/nushell/} ;;
61 *)
62 echo "unknown option: $opt"
63 help
64 exit 1
65 ;;
66 esac
67 done
68
69 cd "$(dirname "${BASH_SOURCE[0]}")"
70 fzf_base=$(pwd)
71 fzf_base_esc=$(printf %q "$fzf_base")
72
73 ask() {
74 while true; do
75 read -p "$1 ([y]/n) " -r
76 REPLY=${REPLY:-"y"}
77 if [[ $REPLY =~ ^[Yy]$ ]]; then
78 return 1
79 elif [[ $REPLY =~ ^[Nn]$ ]]; then
80 return 0
81 fi
82 done
83 }
84
85 check_binary() {
86 echo -n " - Checking fzf executable ... "
87 local output
88 output=$(FZF_DEFAULT_OPTS= "$fzf_base"/bin/fzf --version 2>&1)
89 if [ $? -ne 0 ]; then
90 echo "Error: $output"
91 binary_error="Invalid binary"
92 else
93 output=${output/ */}
94 if [ "$version" != "$output" ]; then
95 echo "$output != $version"
96 binary_error="Invalid version"
97 else
98 echo "$output"
99 binary_error=""
100 return 0
101 fi
102 fi
103 rm -f "$fzf_base"/bin/fzf
104 return 1
105 }
106
107 link_fzf_in_path() {
108 if which_fzf="$(command -v fzf)"; then
109 echo ' - Found in $PATH'
110 echo " - Creating symlink: bin/fzf -> $which_fzf"
111 (cd "$fzf_base"/bin && rm -f fzf && ln -sf "$which_fzf" fzf)
112 check_binary && return
113 fi
114 return 1
115 }
116
117 tar_opts="-xzf -"
118 if tar --no-same-owner -tf /dev/null 2> /dev/null; then
119 tar_opts="--no-same-owner $tar_opts"
120 fi
121
122 try_curl() {
123 command -v curl > /dev/null &&
124 if [[ $1 =~ tar.gz$ ]]; then
125 curl -fL $1 | tar $tar_opts
126 else
127 local temp=${TMPDIR:-/tmp}/fzf.zip
128 curl -fLo "$temp" $1 && unzip -o "$temp" && rm -f "$temp"
129 fi
130 }
131
132 try_wget() {
133 command -v wget > /dev/null &&
134 if [[ $1 =~ tar.gz$ ]]; then
135 wget -O - $1 | tar $tar_opts
136 else
137 local temp=${TMPDIR:-/tmp}/fzf.zip
138 wget -O "$temp" $1 && unzip -o "$temp" && rm -f "$temp"
139 fi
140 }
141
142 download() {
143 echo "Downloading bin/fzf ..."
144 if [ -x "$fzf_base"/bin/fzf ]; then
145 echo " - Already exists"
146 check_binary && return
147 fi
148 link_fzf_in_path && return
149 mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
150 if [ $? -ne 0 ]; then
151 binary_error="Failed to create bin directory"
152 return
153 fi
154
155 local url
156 url=https://github.com/junegunn/fzf/releases/download/v$version/${1}
157 set -o pipefail
158 if ! (try_curl $url || try_wget $url); then
159 set +o pipefail
160 binary_error="Failed to download with curl and wget"
161 return
162 fi
163 set +o pipefail
164
165 if [ ! -f fzf ]; then
166 binary_error="Failed to download ${1}"
167 return
168 fi
169
170 chmod +x fzf && check_binary
171 }
172
173 # Try to download binary executable
174 archi=$(uname -smo 2> /dev/null || uname -sm)
175 binary_available=1
176 binary_error=""
177 case "$archi" in
178 Darwin\ arm64*) download fzf-$version-darwin_arm64.tar.gz ;;
179 Darwin\ x86_64*) download fzf-$version-darwin_amd64.tar.gz ;;
180 Linux\ armv5*) download fzf-$version-linux_armv5.tar.gz ;;
181 Linux\ armv6*) download fzf-$version-linux_armv6.tar.gz ;;
182 Linux\ armv7*) download fzf-$version-linux_armv7.tar.gz ;;
183 Linux\ armv8*) download fzf-$version-linux_arm64.tar.gz ;;
184 Linux\ aarch64\ Android) download fzf-$version-android_arm64.tar.gz ;;
185 Linux\ aarch64*) download fzf-$version-linux_arm64.tar.gz ;;
186 Linux\ loongarch64*) download fzf-$version-linux_loong64.tar.gz ;;
187 Linux\ riscv64*) download fzf-$version-linux_riscv64.tar.gz ;;
188 Linux\ ppc64le*) download fzf-$version-linux_ppc64le.tar.gz ;;
189 Linux\ *64*) download fzf-$version-linux_amd64.tar.gz ;;
190 Linux\ s390x*) download fzf-$version-linux_s390x.tar.gz ;;
191 FreeBSD\ *64*) download fzf-$version-freebsd_amd64.tar.gz ;;
192 OpenBSD\ *64*) download fzf-$version-openbsd_amd64.tar.gz ;;
193 CYGWIN*\ *64*) download fzf-$version-windows_amd64.zip ;;
194 MINGW*\ *64*) download fzf-$version-windows_amd64.zip ;;
195 MSYS*\ *64*) download fzf-$version-windows_amd64.zip ;;
196 Windows*\ *64*) download fzf-$version-windows_amd64.zip ;;
197 *) binary_available=0 binary_error=1 ;;
198 esac
199
200 cd "$fzf_base"
201 if [ -n "$binary_error" ]; then
202 if [ $binary_available -eq 0 ]; then
203 echo "No prebuilt binary for $archi ..."
204 else
205 echo " - $binary_error !!!"
206 fi
207 if command -v go > /dev/null; then
208 echo -n "Building binary (go install github.com/junegunn/fzf) ... "
209 if [ -z "${GOPATH-}" ]; then
210 export GOPATH="${TMPDIR:-/tmp}/fzf-gopath"
211 mkdir -p "$GOPATH"
212 fi
213 if go install -ldflags "-s -w -X main.version=$version -X main.revision=go-install" github.com/junegunn/fzf; then
214 echo "OK"
215 cp "$GOPATH/bin/fzf" "$fzf_base/bin/"
216 else
217 echo "Failed to build binary. Installation failed."
218 exit 1
219 fi
220 else
221 echo "go executable not found. Installation failed."
222 exit 1
223 fi
224 fi
225
226 [[ $* =~ "--bin" ]] && exit 0
227
228 for s in $shells; do
229 bin=$s
230 [[ $s == nushell ]] && bin=nu
231 if ! command -v "$bin" > /dev/null; then
232 shells=${shells/$s/}
233 fi
234 done
235
236 if [[ -z ${shells// /} ]]; then
237 echo "No shell configuration to be updated."
238 exit 0
239 fi
240
241 # Auto-completion
242 if [ -z "$auto_completion" ]; then
243 ask "Do you want to enable fuzzy auto-completion?"
244 auto_completion=$?
245 fi
246
247 # Key-bindings
248 if [ -z "$key_bindings" ]; then
249 ask "Do you want to enable key bindings?"
250 key_bindings=$?
251 fi
252
253 echo
254 for shell in $shells; do
255 [[ $shell == nushell ]] && continue
256 fzf_completion="source \"$fzf_base/shell/completion.${shell}\""
257 fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""
258 [[ $shell == fish ]] && continue
259 src=${prefix_expand}.${shell}
260 echo -n "Generate $src ... "
261
262 if [ $auto_completion -eq 0 ]; then
263 fzf_completion="# $fzf_completion"
264 fi
265
266 if [ $key_bindings -eq 0 ]; then
267 fzf_key_bindings="# $fzf_key_bindings"
268 fi
269
270 cat > "$src" << EOF
271 # Setup fzf
272 # ---------
273 if [[ ! "\$PATH" == *$fzf_base_esc/bin* ]]; then
274 PATH="\${PATH:+\${PATH}:}$fzf_base/bin"
275 fi
276
277 EOF
278
279 if [[ $auto_completion -eq 1 ]] && [[ $key_bindings -eq 1 ]]; then
280 if [[ $shell == zsh ]]; then
281 echo "source <(fzf --$shell)" >> "$src"
282 else
283 echo "eval \"\$(fzf --$shell)\"" >> "$src"
284 fi
285 else
286 cat >> "$src" << EOF
287 # Auto-completion
288 # ---------------
289 $fzf_completion
290
291 # Key bindings
292 # ------------
293 $fzf_key_bindings
294 EOF
295 fi
296 echo "OK"
297 done
298
299 # fish
300 if [[ $shells =~ fish ]]; then
301 echo -n "Update fish_user_paths ... "
302 fish << EOF
303 echo \$fish_user_paths | \grep "$fzf_base"/bin > /dev/null
304 or set --universal fish_user_paths \$fish_user_paths "$fzf_base"/bin
305 EOF
306 [ $? -eq 0 ] && echo "OK" || echo "Failed"
307 fi
308
309 append_line() {
310 local update line file pat lines
311 update="$1"
312 line="$2"
313 file="$3"
314 pat="${4:-}"
315 at_lno="${5:-}"
316 lines=""
317
318 echo "Update $file:"
319 echo " - $line"
320 if [ -f "$file" ]; then
321 if [[ -n $pat ]]; then
322 lines=$(\grep -nF "$pat" "$file")
323 else
324 lines=$(\grep -nF "${line#"${line%%[![:space:]]*}"}" "$file")
325 fi
326 fi
327
328 if [ -n "$lines" ]; then
329 echo " - Already exists:"
330 sed 's/^/ Line /' <<< "$lines"
331
332 update=0
333 if ! \grep -qv "^[0-9]*:[[:space:]]*#" <<< "$lines"; then
334 echo " - But they all seem to be commented"
335 ask " - Continue modifying $file?"
336 update=$?
337 fi
338 fi
339
340 set -e
341 if [ "$update" -eq 1 ]; then
342 if [[ -z $at_lno ]]; then
343 [ -f "$file" ] && echo >> "$file"
344 echo "$line" >> "$file"
345 else
346 sed -i.~fzf_bak "${at_lno}a\\"$'\n'"$line" "$file" && rm "$file.~fzf_bak"
347 fi
348 echo " + Added"
349 else
350 echo " ~ Skipped"
351 fi
352
353 echo
354 set +e
355 }
356
357 create_file() {
358 local file="$1"
359 shift
360 echo "Create $file:"
361 for line in "$@"; do
362 echo " $line"
363 echo "$line" >> "$file"
364 done
365 echo
366 }
367
368 if [ $update_config -eq 2 ]; then
369 echo
370 ask "Do you want to update your shell configuration files?"
371 update_config=$?
372 fi
373 echo
374 for shell in $shells; do
375 [[ $shell == fish ]] && continue
376 [[ $shell == nushell ]] && continue
377 [ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
378 append_line $update_config "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" "$dest" "${prefix}.${shell}"
379 done
380
381 if [[ $shells =~ fish ]]; then
382 bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"
383 if [ ! -e "$bind_file" ]; then
384 if [[ $key_bindings -eq 1 || $auto_completion -eq 1 ]]; then
385 mkdir -p "${fish_dir}/functions"
386 if [[ $key_bindings -eq 1 && $auto_completion -eq 1 ]]; then
387 create_file "$bind_file" \
388 'function fish_user_key_bindings' \
389 ' fzf --fish | source' \
390 'end'
391 elif [[ $key_bindings -eq 1 ]]; then
392 create_file "$bind_file" \
393 'function fish_user_key_bindings' \
394 " $fzf_key_bindings" \
395 'end'
396 elif [[ $auto_completion -eq 1 ]]; then
397 create_file "$bind_file" \
398 'function fish_user_key_bindings' \
399 " $fzf_completion" \
400 'end'
401 fi
402 lno_func=$(\grep -nF "function fish_user_key_bindings" "$bind_file" | sed 's/:.*//' | tr '\n' ' ')
403 else
404 lno_func=0
405 fi
406 else
407 echo "Check $bind_file:"
408 lno_func=$(\grep -nF "function fish_user_key_bindings" "$bind_file" | sed 's/:.*//' | tr '\n' ' ')
409 if [[ -z $lno_func ]]; then
410 echo -e "function fish_user_key_bindings\nend" >> "$bind_file"
411 lno_func=$(\grep -nF "function fish_user_key_bindings" "$bind_file" | sed 's/:.*//' | tr '\n' ' ')
412 fi
413 lno_keys=$(\grep -nF "fzf_key_bindings" "$bind_file" | sed 's/:.*//' | tr '\n' ' ')
414 if [[ -n $lno_keys ]]; then
415 echo " ** Found 'fzf_key_bindings' in line #$lno_keys"
416 if [[ $key_bindings -eq 1 && $auto_completion -eq 1 ]]; then
417 echo " ** You have to replace the line to 'fzf --fish | source'"
418 elif [[ $key_bindings -eq 1 ]]; then
419 echo " ** You have to replace the line to '$fzf_key_bindings'"
420 else
421 echo " ** You have to remove the line"
422 fi
423 echo
424 else
425 echo " - Clear"
426 echo
427 if [[ $key_bindings -eq 1 && $auto_completion -eq 1 ]]; then
428 sed -i.~fzf_bak "\#$fzf_completion#d" "$bind_file" && rm "$bind_file.~fzf_bak"
429 sed -i.~fzf_bak "\#$fzf_key_bindings#d" "$bind_file" && rm "$bind_file.~fzf_bak"
430 append_line $update_config " fzf --fish | source" "$bind_file" "" "$lno_func"
431 else
432 sed -i.~fzf_bak '/fzf --fish \| source/d' "$bind_file" && rm "$bind_file.~fzf_bak"
433 if [[ $key_bindings -eq 1 ]]; then
434 sed -i.~fzf_bak "\#$fzf_completion#d" "$bind_file" && rm "$bind_file.~fzf_bak"
435 append_line $update_config " $fzf_key_bindings" "$bind_file" "" "$lno_func"
436 elif [[ $auto_completion -eq 1 ]]; then
437 sed -i.~fzf_bak "\#$fzf_key_bindings#d" "$bind_file" && rm "$bind_file.~fzf_bak"
438 append_line $update_config " $fzf_completion" "$bind_file" "" "$lno_func"
439 fi
440 fi
441 fi
442 fi
443 fi
444
445 if [[ $shells =~ nushell ]]; then
446 if [[ $key_bindings -eq 1 || $auto_completion -eq 1 ]]; then
447 echo "Setting up Nushell integration ..."
448 nushell_autoload_dir=$(nu -c '$nu.user-autoload-dirs | first')
449 mkdir -p "$nushell_autoload_dir"
450 dest="$nushell_autoload_dir/_fzf_integration.nu"
451 echo -n " Generate $dest ... "
452 if [[ $key_bindings -eq 1 && $auto_completion -eq 1 ]]; then
453 "$fzf_base"/bin/fzf --nushell > "$dest"
454 elif [[ $key_bindings -eq 1 ]]; then
455 cp "$fzf_base/shell/key-bindings.nu" "$dest"
456 else
457 cp "$fzf_base/shell/completion.nu" "$dest"
458 fi
459 echo "OK"
460 echo
461 fi
462 fi
463
464 if [ $update_config -eq 1 ]; then
465 echo 'Finished. Restart your shell or reload config file.'
466 if [[ $shells =~ bash ]]; then
467 echo -n ' source ~/.bashrc # bash'
468 [[ $archi =~ Darwin ]] && echo -n ' (.bashrc should be loaded from .bash_profile)'
469 echo
470 fi
471 [[ $shells =~ zsh ]] && echo " source ${ZDOTDIR:-~}/.zshrc # zsh"
472 [[ $shells =~ fish && $lno_func -ne 0 ]] && echo ' fish_user_key_bindings # fish'
473 [[ $shells =~ nushell ]] && echo ' # nushell: files are loaded automatically from autoload directory'
474 echo
475 echo 'Use uninstall script to remove fzf.'
476 echo
477 fi
478 echo 'For more information, see: https://github.com/junegunn/fzf'