Skip to content
Myr

install

This file

Path

install

381 lines · 9.7 KiB

Last changed by

0.24.3Junegunn Choibefore adoption722d66e

History of this file

Take it

1#!/usr/bin/env bash23set -u45version=0.24.36auto_completion=7key_bindings=8update_config=29shells="bash zsh fish"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 configuration30EOF31}3233for opt in "$@"; do34  case $opt in35    --help)36      help37      exit 038      ;;39    --all)40      auto_completion=141      key_bindings=142      update_config=143      ;;44    --xdg)45      prefix='"${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf'46      prefix_expand=${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf47      mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/fzf"48      ;;49    --key-bindings)    key_bindings=1    ;;50    --no-key-bindings) key_bindings=0    ;;51    --completion)      auto_completion=1 ;;52    --no-completion)   auto_completion=0 ;;53    --update-rc)       update_config=1   ;;54    --no-update-rc)    update_config=0   ;;55    --bin)             ;;56    --no-bash)         shells=${shells/bash/} ;;57    --no-zsh)          shells=${shells/zsh/} ;;58    --no-fish)         shells=${shells/fish/} ;;59    *)60      echo "unknown option: $opt"61      help62      exit 163      ;;64  esac65done6667cd "$(dirname "${BASH_SOURCE[0]}")"68fzf_base=$(pwd)69fzf_base_esc=$(printf %q "$fzf_base")7071ask() {72  while true; do73    read -p "$1 ([y]/n) " -r74    REPLY=${REPLY:-"y"}75    if [[ $REPLY =~ ^[Yy]$ ]]; then76      return 177    elif [[ $REPLY =~ ^[Nn]$ ]]; then78      return 079    fi80  done81}8283check_binary() {84  echo -n "  - Checking fzf executable ... "85  local output86  output=$("$fzf_base"/bin/fzf --version 2>&1)87  if [ $? -ne 0 ]; then88    echo "Error: $output"89    binary_error="Invalid binary"90  else91    output=${output/ */}92    if [ "$version" != "$output" ]; then93      echo "$output != $version"94      binary_error="Invalid version"95    else96      echo "$output"97      binary_error=""98      return 099    fi100  fi101  rm -f "$fzf_base"/bin/fzf102  return 1103}104105link_fzf_in_path() {106  if which_fzf="$(command -v fzf)"; then107    echo "  - Found in \$PATH"108    echo "  - Creating symlink: bin/fzf -> $which_fzf"109    (cd "$fzf_base"/bin && rm -f fzf && ln -sf "$which_fzf" fzf)110    check_binary && return111  fi112  return 1113}114115try_curl() {116  command -v curl > /dev/null &&117  if [[ $1 =~ tar.gz$ ]]; then118    curl -fL $1 | tar -xzf -119  else120    local temp=${TMPDIR:-/tmp}/fzf.zip121    curl -fLo "$temp" $1 && unzip -o "$temp" && rm -f "$temp"122  fi123}124125try_wget() {126  command -v wget > /dev/null &&127  if [[ $1 =~ tar.gz$ ]]; then128    wget -O - $1 | tar -xzf -129  else130    local temp=${TMPDIR:-/tmp}/fzf.zip131    wget -O "$temp" $1 && unzip -o "$temp" && rm -f "$temp"132  fi133}134135download() {136  echo "Downloading bin/fzf ..."137  if [ -x "$fzf_base"/bin/fzf ]; then138    echo "  - Already exists"139    check_binary && return140  fi141  link_fzf_in_path && return142  mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin143  if [ $? -ne 0 ]; then144    binary_error="Failed to create bin directory"145    return146  fi147148  local url149  url=https://github.com/junegunn/fzf/releases/download/$version/${1}150  set -o pipefail151  if ! (try_curl $url || try_wget $url); then152    set +o pipefail153    binary_error="Failed to download with curl and wget"154    return155  fi156  set +o pipefail157158  if [ ! -f fzf ]; then159    binary_error="Failed to download ${1}"160    return161  fi162163  chmod +x fzf && check_binary164}165166# Try to download binary executable167archi=$(uname -sm)168binary_available=1169binary_error=""170case "$archi" in171  Darwin\ *64)     download fzf-$version-darwin_amd64.tar.gz  ;;172  Linux\ armv5*)   download fzf-$version-linux_armv5.tar.gz   ;;173  Linux\ armv6*)   download fzf-$version-linux_armv6.tar.gz   ;;174  Linux\ armv7*)   download fzf-$version-linux_armv7.tar.gz   ;;175  Linux\ armv8*)   download fzf-$version-linux_arm64.tar.gz   ;;176  Linux\ aarch64*) download fzf-$version-linux_arm64.tar.gz   ;;177  Linux\ *64)      download fzf-$version-linux_amd64.tar.gz   ;;178  FreeBSD\ *64)    download fzf-$version-freebsd_amd64.tar.gz ;;179  OpenBSD\ *64)    download fzf-$version-openbsd_amd64.tar.gz ;;180  CYGWIN*\ *64)    download fzf-$version-windows_amd64.zip    ;;181  MINGW*\ *64)     download fzf-$version-windows_amd64.zip    ;;182  MSYS*\ *64)      download fzf-$version-windows_amd64.zip    ;;183  Windows*\ *64)   download fzf-$version-windows_amd64.zip    ;;184  *)               binary_available=0 binary_error=1 ;;185esac186187cd "$fzf_base"188if [ -n "$binary_error" ]; then189  if [ $binary_available -eq 0 ]; then190    echo "No prebuilt binary for $archi ..."191  else192    echo "  - $binary_error !!!"193  fi194  if command -v go > /dev/null; then195    echo -n "Building binary (go get -u github.com/junegunn/fzf) ... "196    if [ -z "${GOPATH-}" ]; then197      export GOPATH="${TMPDIR:-/tmp}/fzf-gopath"198      mkdir -p "$GOPATH"199    fi200    if go get -ldflags "-s -w -X main.version=$version -X main.revision=go-get" github.com/junegunn/fzf; then201      echo "OK"202      cp "$GOPATH/bin/fzf" "$fzf_base/bin/"203    else204      echo "Failed to build binary. Installation failed."205      exit 1206    fi207  else208    echo "go executable not found. Installation failed."209    exit 1210  fi211fi212213[[ "$*" =~ "--bin" ]] && exit 0214215for s in $shells; do216  if ! command -v "$s" > /dev/null; then217    shells=${shells/$s/}218  fi219done220221if [[ ${#shells} -lt 3 ]]; then222  echo "No shell configuration to be updated."223  exit 0224fi225226# Auto-completion227if [ -z "$auto_completion" ]; then228  ask "Do you want to enable fuzzy auto-completion?"229  auto_completion=$?230fi231232# Key-bindings233if [ -z "$key_bindings" ]; then234  ask "Do you want to enable key bindings?"235  key_bindings=$?236fi237238echo239for shell in $shells; do240  [[ "$shell" = fish ]] && continue241  src=${prefix_expand}.${shell}242  echo -n "Generate $src ... "243244  fzf_completion="[[ \$- == *i* ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null"245  if [ $auto_completion -eq 0 ]; then246    fzf_completion="# $fzf_completion"247  fi248249  fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""250  if [ $key_bindings -eq 0 ]; then251    fzf_key_bindings="# $fzf_key_bindings"252  fi253254  cat > "$src" << EOF255# Setup fzf256# ---------257if [[ ! "\$PATH" == *$fzf_base_esc/bin* ]]; then258  export PATH="\${PATH:+\${PATH}:}$fzf_base/bin"259fi260261# Auto-completion262# ---------------263$fzf_completion264265# Key bindings266# ------------267$fzf_key_bindings268EOF269  echo "OK"270done271272# fish273if [[ "$shells" =~ fish ]]; then274  echo -n "Update fish_user_paths ... "275  fish << EOF276  echo \$fish_user_paths | \grep "$fzf_base"/bin > /dev/null277  or set --universal fish_user_paths \$fish_user_paths "$fzf_base"/bin278EOF279  [ $? -eq 0 ] && echo "OK" || echo "Failed"280281  mkdir -p "${fish_dir}/functions"282  if [ -e "${fish_dir}/functions/fzf.fish" ]; then283    echo -n "Remove unnecessary ${fish_dir}/functions/fzf.fish ... "284    rm -f "${fish_dir}/functions/fzf.fish" && echo "OK" || echo "Failed"285  fi286287  fish_binding="${fish_dir}/functions/fzf_key_bindings.fish"288  if [ $key_bindings -ne 0 ]; then289    echo -n "Symlink $fish_binding ... "290    ln -sf "$fzf_base/shell/key-bindings.fish" \291           "$fish_binding" && echo "OK" || echo "Failed"292  else293    echo -n "Removing $fish_binding ... "294    rm -f "$fish_binding"295    echo "OK"296  fi297fi298299append_line() {300  set -e301302  local update line file pat lno303  update="$1"304  line="$2"305  file="$3"306  pat="${4:-}"307  lno=""308309  echo "Update $file:"310  echo "  - $line"311  if [ -f "$file" ]; then312    if [ $# -lt 4 ]; then313      lno=$(\grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ')314    else315      lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ')316    fi317  fi318  if [ -n "$lno" ]; then319    echo "    - Already exists: line #$lno"320  else321    if [ $update -eq 1 ]; then322      [ -f "$file" ] && echo >> "$file"323      echo "$line" >> "$file"324      echo "    + Added"325    else326      echo "    ~ Skipped"327    fi328  fi329  echo330  set +e331}332333create_file() {334  local file="$1"335  shift336  echo "Create $file:"337  for line in "$@"; do338    echo "    $line"339    echo "$line" >> "$file"340  done341  echo342}343344if [ $update_config -eq 2 ]; then345  echo346  ask "Do you want to update your shell configuration files?"347  update_config=$?348fi349echo350for shell in $shells; do351  [[ "$shell" = fish ]] && continue352  [ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc353  append_line $update_config "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" "$dest" "${prefix}.${shell}"354done355356if [ $key_bindings -eq 1 ] && [[ "$shells" =~ fish ]]; then357  bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"358  if [ ! -e "$bind_file" ]; then359    create_file "$bind_file" \360      'function fish_user_key_bindings' \361      '  fzf_key_bindings' \362      'end'363  else364    append_line $update_config "fzf_key_bindings" "$bind_file"365  fi366fi367368if [ $update_config -eq 1 ]; then369  echo 'Finished. Restart your shell or reload config file.'370  if [[ "$shells" =~ bash ]]; then371    echo -n '   source ~/.bashrc  # bash'372    [[ "$archi" =~ Darwin ]] && echo -n '  (.bashrc should be loaded from .bash_profile)'373    echo374  fi375  [[ "$shells" =~ zsh ]]  && echo "   source ${ZDOTDIR:-~}/.zshrc   # zsh"376  [[ "$shells" =~ fish ]] && [ $key_bindings -eq 1 ] && echo '   fzf_key_bindings  # fish'377  echo378  echo 'Use uninstall script to remove fzf.'379  echo380fi381echo 'For more information, see: https://github.com/junegunn/fzf'