Skip to content
Myr

install

This file

Path

install

399 lines · 10.2 KiB

Last changed by

0.61.0Junegunn Choibefore adoptione15cba0

History of this file

Take it

1#!/usr/bin/env bash23set -u45version=0.61.06auto_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_DEFAULT_OPTS= "$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 --no-same-owner -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 --no-same-owner -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/v$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\ arm64)      download fzf-$version-darwin_arm64.tar.gz  ;;172  Darwin\ x86_64)     download fzf-$version-darwin_amd64.tar.gz  ;;173  Linux\ armv5*)      download fzf-$version-linux_armv5.tar.gz   ;;174  Linux\ armv6*)      download fzf-$version-linux_armv6.tar.gz   ;;175  Linux\ armv7*)      download fzf-$version-linux_armv7.tar.gz   ;;176  Linux\ armv8*)      download fzf-$version-linux_arm64.tar.gz   ;;177  Linux\ aarch64*)    download fzf-$version-linux_arm64.tar.gz   ;;178  Linux\ loongarch64) download fzf-$version-linux_loong64.tar.gz ;;179  Linux\ ppc64le)     download fzf-$version-linux_ppc64le.tar.gz ;;180  Linux\ *64)         download fzf-$version-linux_amd64.tar.gz   ;;181  Linux\ s390x)       download fzf-$version-linux_s390x.tar.gz   ;;182  FreeBSD\ *64)       download fzf-$version-freebsd_amd64.tar.gz ;;183  OpenBSD\ *64)       download fzf-$version-openbsd_amd64.tar.gz ;;184  CYGWIN*\ *64)       download fzf-$version-windows_amd64.zip    ;;185  MINGW*\ *64)        download fzf-$version-windows_amd64.zip    ;;186  MSYS*\ *64)         download fzf-$version-windows_amd64.zip    ;;187  Windows*\ *64)      download fzf-$version-windows_amd64.zip    ;;188  *)                  binary_available=0 binary_error=1          ;;189esac190191cd "$fzf_base"192if [ -n "$binary_error" ]; then193  if [ $binary_available -eq 0 ]; then194    echo "No prebuilt binary for $archi ..."195  else196    echo "  - $binary_error !!!"197  fi198  if command -v go > /dev/null; then199    echo -n "Building binary (go install github.com/junegunn/fzf) ... "200    if [ -z "${GOPATH-}" ]; then201      export GOPATH="${TMPDIR:-/tmp}/fzf-gopath"202      mkdir -p "$GOPATH"203    fi204    if go install -ldflags "-s -w -X main.version=$version -X main.revision=go-install" github.com/junegunn/fzf; then205      echo "OK"206      cp "$GOPATH/bin/fzf" "$fzf_base/bin/"207    else208      echo "Failed to build binary. Installation failed."209      exit 1210    fi211  else212    echo "go executable not found. Installation failed."213    exit 1214  fi215fi216217[[ "$*" =~ "--bin" ]] && exit 0218219for s in $shells; do220  if ! command -v "$s" > /dev/null; then221    shells=${shells/$s/}222  fi223done224225if [[ ${#shells} -lt 3 ]]; then226  echo "No shell configuration to be updated."227  exit 0228fi229230# Auto-completion231if [ -z "$auto_completion" ]; then232  ask "Do you want to enable fuzzy auto-completion?"233  auto_completion=$?234fi235236# Key-bindings237if [ -z "$key_bindings" ]; then238  ask "Do you want to enable key bindings?"239  key_bindings=$?240fi241242echo243for shell in $shells; do244  [[ "$shell" = fish ]] && continue245  src=${prefix_expand}.${shell}246  echo -n "Generate $src ... "247248  fzf_completion="source \"$fzf_base/shell/completion.${shell}\""249  if [ $auto_completion -eq 0 ]; then250    fzf_completion="# $fzf_completion"251  fi252253  fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""254  if [ $key_bindings -eq 0 ]; then255    fzf_key_bindings="# $fzf_key_bindings"256  fi257258  cat > "$src" << EOF259# Setup fzf260# ---------261if [[ ! "\$PATH" == *$fzf_base_esc/bin* ]]; then262  PATH="\${PATH:+\${PATH}:}$fzf_base/bin"263fi264265EOF266267  if [[ $auto_completion -eq 1 ]] && [[ $key_bindings -eq 1 ]]; then268    if [[ "$shell" = zsh ]]; then269      echo "source <(fzf --$shell)" >> "$src"270    else271      echo "eval \"\$(fzf --$shell)\"" >> "$src"272    fi273  else274    cat >> "$src" << EOF275# Auto-completion276# ---------------277$fzf_completion278279# Key bindings280# ------------281$fzf_key_bindings282EOF283  fi284  echo "OK"285done286287# fish288if [[ "$shells" =~ fish ]]; then289  echo -n "Update fish_user_paths ... "290  fish << EOF291  echo \$fish_user_paths | \grep "$fzf_base"/bin > /dev/null292  or set --universal fish_user_paths \$fish_user_paths "$fzf_base"/bin293EOF294  [ $? -eq 0 ] && echo "OK" || echo "Failed"295fi296297append_line() {298  local update line file pat lines299  update="$1"300  line="$2"301  file="$3"302  pat="${4:-}"303  lines=""304305  echo "Update $file:"306  echo "  - $line"307  if [ -f "$file" ]; then308    if [ $# -lt 4 ]; then309      lines=$(\grep -nF "$line" "$file")310    else311      lines=$(\grep -nF "$pat" "$file")312    fi313  fi314315  if [ -n "$lines" ]; then316    echo "    - Already exists:"317    sed 's/^/        Line /' <<< "$lines"318319    update=0320    if ! \grep -qv "^[0-9]*:[[:space:]]*#" <<< "$lines" ; then321      echo "    - But they all seem to be commented"322      ask  "    - Continue modifying $file?"323      update=$?324    fi325  fi326327  set -e328  if [ "$update" -eq 1 ]; then329    [ -f "$file" ] && echo >> "$file"330    echo "$line" >> "$file"331    echo "    + Added"332  else333    echo "    ~ Skipped"334  fi335336  echo337  set +e338}339340create_file() {341  local file="$1"342  shift343  echo "Create $file:"344  for line in "$@"; do345    echo "    $line"346    echo "$line" >> "$file"347  done348  echo349}350351if [ $update_config -eq 2 ]; then352  echo353  ask "Do you want to update your shell configuration files?"354  update_config=$?355fi356echo357for shell in $shells; do358  [[ "$shell" = fish ]] && continue359  [ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc360  append_line $update_config "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" "$dest" "${prefix}.${shell}"361done362363if [ $key_bindings -eq 1 ] && [[ "$shells" =~ fish ]]; then364  bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"365  if [ ! -e "$bind_file" ]; then366    mkdir -p "${fish_dir}/functions"367    create_file "$bind_file" \368      'function fish_user_key_bindings' \369      '  fzf --fish | source' \370      'end'371  else372    echo "Check $bind_file:"373    lno=$(\grep -nF "fzf_key_bindings" "$bind_file" | sed 's/:.*//' | tr '\n' ' ')374    if [[ -n $lno ]]; then375      echo "  ** Found 'fzf_key_bindings' in line #$lno"376      echo "  ** You have to replace the line to 'fzf --fish | source'"377      echo378    else379      echo "  - Clear"380      echo381      append_line $update_config "fzf --fish | source" "$bind_file"382    fi383  fi384fi385386if [ $update_config -eq 1 ]; then387  echo 'Finished. Restart your shell or reload config file.'388  if [[ "$shells" =~ bash ]]; then389    echo -n '   source ~/.bashrc  # bash'390    [[ "$archi" =~ Darwin ]] && echo -n '  (.bashrc should be loaded from .bash_profile)'391    echo392  fi393  [[ "$shells" =~ zsh ]]  && echo "   source ${ZDOTDIR:-~}/.zshrc   # zsh"394  [[ "$shells" =~ fish ]] && [ $key_bindings -eq 1 ] && echo '   fzf_key_bindings  # fish'395  echo396  echo 'Use uninstall script to remove fzf.'397  echo398fi399echo 'For more information, see: https://github.com/junegunn/fzf'