Skip to content
Myr

install

This file

Path

install

478 lines · 13.4 KiB

Last changed by

landed by oscabrielc0a193e

History of this file

Take it

1#!/usr/bin/env bash23set -u45version=0.74.56auto_completion=7key_bindings=8update_config=29shells="bash zsh fish nushell"10prefix='~/.fzf'11prefix_expand=~/.fzf12fish_dir=${XDG_CONFIG_HOME:-$HOME/.config}/fish1314help() {15  cat << EOF16usage: $0 [OPTIONS]1718    --help               Show this message19    --bin                Download fzf binary only; Do not generate ~/.fzf.{bash,zsh}20    --all                Download fzf binary and update configuration files21                         to enable key bindings and fuzzy completion22    --xdg                Generate files under \$XDG_CONFIG_HOME/fzf23    --[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 files2627    --no-bash            Do not set up bash configuration28    --no-zsh             Do not set up zsh configuration29    --no-fish            Do not set up fish configuration30    --no-nushell         Do not set up nushell configuration31EOF32}3334for opt in "$@"; do35  case $opt in36    --help)37      help38      exit 039      ;;40    --all)41      auto_completion=142      key_bindings=143      update_config=144      ;;45    --xdg)46      prefix='"${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf'47      prefix_expand=${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf48      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      help64      exit 165      ;;66  esac67done6869cd "$(dirname "${BASH_SOURCE[0]}")"70fzf_base=$(pwd)71fzf_base_esc=$(printf %q "$fzf_base")7273ask() {74  while true; do75    read -p "$1 ([y]/n) " -r76    REPLY=${REPLY:-"y"}77    if [[ $REPLY =~ ^[Yy]$ ]]; then78      return 179    elif [[ $REPLY =~ ^[Nn]$ ]]; then80      return 081    fi82  done83}8485check_binary() {86  echo -n "  - Checking fzf executable ... "87  local output88  output=$(FZF_DEFAULT_OPTS= "$fzf_base"/bin/fzf --version 2>&1)89  if [ $? -ne 0 ]; then90    echo "Error: $output"91    binary_error="Invalid binary"92  else93    output=${output/ */}94    if [ "$version" != "$output" ]; then95      echo "$output != $version"96      binary_error="Invalid version"97    else98      echo "$output"99      binary_error=""100      return 0101    fi102  fi103  rm -f "$fzf_base"/bin/fzf104  return 1105}106107link_fzf_in_path() {108  if which_fzf="$(command -v fzf)"; then109    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 && return113  fi114  return 1115}116117tar_opts="-xzf -"118if tar --no-same-owner -tf /dev/null 2> /dev/null; then119  tar_opts="--no-same-owner $tar_opts"120fi121122try_curl() {123  command -v curl > /dev/null &&124    if [[ $1 =~ tar.gz$ ]]; then125      curl -fL $1 | tar $tar_opts126    else127      local temp=${TMPDIR:-/tmp}/fzf.zip128      curl -fLo "$temp" $1 && unzip -o "$temp" && rm -f "$temp"129    fi130}131132try_wget() {133  command -v wget > /dev/null &&134    if [[ $1 =~ tar.gz$ ]]; then135      wget -O - $1 | tar $tar_opts136    else137      local temp=${TMPDIR:-/tmp}/fzf.zip138      wget -O "$temp" $1 && unzip -o "$temp" && rm -f "$temp"139    fi140}141142download() {143  echo "Downloading bin/fzf ..."144  if [ -x "$fzf_base"/bin/fzf ]; then145    echo "  - Already exists"146    check_binary && return147  fi148  link_fzf_in_path && return149  mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin150  if [ $? -ne 0 ]; then151    binary_error="Failed to create bin directory"152    return153  fi154155  local url156  url=https://github.com/junegunn/fzf/releases/download/v$version/${1}157  set -o pipefail158  if ! (try_curl $url || try_wget $url); then159    set +o pipefail160    binary_error="Failed to download with curl and wget"161    return162  fi163  set +o pipefail164165  if [ ! -f fzf ]; then166    binary_error="Failed to download ${1}"167    return168  fi169170  chmod +x fzf && check_binary171}172173# Try to download binary executable174archi=$(uname -smo 2> /dev/null || uname -sm)175binary_available=1176binary_error=""177case "$archi" in178  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 ;;198esac199200cd "$fzf_base"201if [ -n "$binary_error" ]; then202  if [ $binary_available -eq 0 ]; then203    echo "No prebuilt binary for $archi ..."204  else205    echo "  - $binary_error !!!"206  fi207  if command -v go > /dev/null; then208    echo -n "Building binary (go install github.com/junegunn/fzf) ... "209    if [ -z "${GOPATH-}" ]; then210      export GOPATH="${TMPDIR:-/tmp}/fzf-gopath"211      mkdir -p "$GOPATH"212    fi213    if go install -ldflags "-s -w -X main.version=$version -X main.revision=go-install" github.com/junegunn/fzf; then214      echo "OK"215      cp "$GOPATH/bin/fzf" "$fzf_base/bin/"216    else217      echo "Failed to build binary. Installation failed."218      exit 1219    fi220  else221    echo "go executable not found. Installation failed."222    exit 1223  fi224fi225226[[ $* =~ "--bin" ]] && exit 0227228for s in $shells; do229  bin=$s230  [[ $s == nushell ]] && bin=nu231  if ! command -v "$bin" > /dev/null; then232    shells=${shells/$s/}233  fi234done235236if [[ -z ${shells// /} ]]; then237  echo "No shell configuration to be updated."238  exit 0239fi240241# Auto-completion242if [ -z "$auto_completion" ]; then243  ask "Do you want to enable fuzzy auto-completion?"244  auto_completion=$?245fi246247# Key-bindings248if [ -z "$key_bindings" ]; then249  ask "Do you want to enable key bindings?"250  key_bindings=$?251fi252253echo254for shell in $shells; do255  [[ $shell == nushell ]] && continue256  fzf_completion="source \"$fzf_base/shell/completion.${shell}\""257  fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""258  [[ $shell == fish ]] && continue259  src=${prefix_expand}.${shell}260  echo -n "Generate $src ... "261262  if [ $auto_completion -eq 0 ]; then263    fzf_completion="# $fzf_completion"264  fi265266  if [ $key_bindings -eq 0 ]; then267    fzf_key_bindings="# $fzf_key_bindings"268  fi269270  cat > "$src" << EOF271# Setup fzf272# ---------273if [[ ! "\$PATH" == *$fzf_base_esc/bin* ]]; then274  PATH="\${PATH:+\${PATH}:}$fzf_base/bin"275fi276277EOF278279  if [[ $auto_completion -eq 1 ]] && [[ $key_bindings -eq 1 ]]; then280    if [[ $shell == zsh ]]; then281      echo "source <(fzf --$shell)" >> "$src"282    else283      echo "eval \"\$(fzf --$shell)\"" >> "$src"284    fi285  else286    cat >> "$src" << EOF287# Auto-completion288# ---------------289$fzf_completion290291# Key bindings292# ------------293$fzf_key_bindings294EOF295  fi296  echo "OK"297done298299# fish300if [[ $shells =~ fish ]]; then301  echo -n "Update fish_user_paths ... "302  fish << EOF303  echo \$fish_user_paths | \grep "$fzf_base"/bin > /dev/null304  or set --universal fish_user_paths \$fish_user_paths "$fzf_base"/bin305EOF306  [ $? -eq 0 ] && echo "OK" || echo "Failed"307fi308309append_line() {310  local update line file pat lines311  update="$1"312  line="$2"313  file="$3"314  pat="${4:-}"315  at_lno="${5:-}"316  lines=""317318  echo "Update $file:"319  echo "  - $line"320  if [ -f "$file" ]; then321    if [[ -n $pat ]]; then322      lines=$(\grep -nF "$pat" "$file")323    else324      lines=$(\grep -nF "${line#"${line%%[![:space:]]*}"}" "$file")325    fi326  fi327328  if [ -n "$lines" ]; then329    echo "    - Already exists:"330    sed 's/^/        Line /' <<< "$lines"331332    update=0333    if ! \grep -qv "^[0-9]*:[[:space:]]*#" <<< "$lines"; then334      echo "    - But they all seem to be commented"335      ask "    - Continue modifying $file?"336      update=$?337    fi338  fi339340  set -e341  if [ "$update" -eq 1 ]; then342    if [[ -z $at_lno ]]; then343      [ -f "$file" ] && echo >> "$file"344      echo "$line" >> "$file"345    else346      sed -i.~fzf_bak "${at_lno}a\\"$'\n'"$line" "$file" && rm "$file.~fzf_bak"347    fi348    echo "    + Added"349  else350    echo "    ~ Skipped"351  fi352353  echo354  set +e355}356357create_file() {358  local file="$1"359  shift360  echo "Create $file:"361  for line in "$@"; do362    echo "    $line"363    echo "$line" >> "$file"364  done365  echo366}367368if [ $update_config -eq 2 ]; then369  echo370  ask "Do you want to update your shell configuration files?"371  update_config=$?372fi373echo374for shell in $shells; do375  [[ $shell == fish ]] && continue376  [[ $shell == nushell ]] && continue377  [ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc378  append_line $update_config "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" "$dest" "${prefix}.${shell}"379done380381if [[ $shells =~ fish ]]; then382  bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"383  if [ ! -e "$bind_file" ]; then384    if [[ $key_bindings -eq 1 || $auto_completion -eq 1 ]]; then385      mkdir -p "${fish_dir}/functions"386      if [[ $key_bindings -eq 1 && $auto_completion -eq 1 ]]; then387        create_file "$bind_file" \388          'function fish_user_key_bindings' \389          '  fzf --fish | source' \390          'end'391      elif [[ $key_bindings -eq 1 ]]; then392        create_file "$bind_file" \393          'function fish_user_key_bindings' \394          "  $fzf_key_bindings" \395          'end'396      elif [[ $auto_completion -eq 1 ]]; then397        create_file "$bind_file" \398          'function fish_user_key_bindings' \399          "  $fzf_completion" \400          'end'401      fi402      lno_func=$(\grep -nF "function fish_user_key_bindings" "$bind_file" | sed 's/:.*//' | tr '\n' ' ')403    else404      lno_func=0405    fi406  else407    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 ]]; then410      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    fi413    lno_keys=$(\grep -nF "fzf_key_bindings" "$bind_file" | sed 's/:.*//' | tr '\n' ' ')414    if [[ -n $lno_keys ]]; then415      echo "  ** Found 'fzf_key_bindings' in line #$lno_keys"416      if [[ $key_bindings -eq 1 && $auto_completion -eq 1 ]]; then417        echo "  ** You have to replace the line to 'fzf --fish | source'"418      elif [[ $key_bindings -eq 1 ]]; then419        echo "  ** You have to replace the line to '$fzf_key_bindings'"420      else421        echo "  ** You have to remove the line"422      fi423      echo424    else425      echo "  - Clear"426      echo427      if [[ $key_bindings -eq 1 && $auto_completion -eq 1 ]]; then428        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      else432        sed -i.~fzf_bak '/fzf --fish \| source/d' "$bind_file" && rm "$bind_file.~fzf_bak"433        if [[ $key_bindings -eq 1 ]]; then434          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 ]]; then437          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        fi440      fi441    fi442  fi443fi444445if [[ $shells =~ nushell ]]; then446  if [[ $key_bindings -eq 1 || $auto_completion -eq 1 ]]; then447    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 ]]; then453      "$fzf_base"/bin/fzf --nushell > "$dest"454    elif [[ $key_bindings -eq 1 ]]; then455      cp "$fzf_base/shell/key-bindings.nu" "$dest"456    else457      cp "$fzf_base/shell/completion.nu" "$dest"458    fi459    echo "OK"460    echo461  fi462fi463464if [ $update_config -eq 1 ]; then465  echo 'Finished. Restart your shell or reload config file.'466  if [[ $shells =~ bash ]]; then467    echo -n '   source ~/.bashrc  # bash'468    [[ $archi =~ Darwin ]] && echo -n '  (.bashrc should be loaded from .bash_profile)'469    echo470  fi471  [[ $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  echo475  echo 'Use uninstall script to remove fzf.'476  echo477fi478echo 'For more information, see: https://github.com/junegunn/fzf'