summaryrefslogtreecommitdiffstats
path: root/install
blob: de16b1802fcb2afb78b167e94576f33a739c333f (plain)
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env bash

set -u

version=0.16.5
auto_completion=
key_bindings=
update_config=2
binary_arch=
allow_legacy=

help() {
  cat << EOF
usage: $0 [OPTIONS]

    --help               Show this message
    --bin                Download fzf binary only; Do not generate ~/.fzf.{bash,zsh}
    --all                Download fzf binary and update configuration files
                         to enable key bindings and fuzzy completion
    --[no-]key-bindings  Enable/disable key bindings (CTRL-T, CTRL-R, ALT-C)
    --[no-]completion    Enable/disable fuzzy completion (bash & zsh)
    --[no-]update-rc     Whether or not to update shell configuration files

    --32                 Download 32-bit binary
    --64                 Download 64-bit binary
EOF
}

for opt in "$@"; do
  case $opt in
    --help)
      help
      exit 0
      ;;
    --all)
      auto_completion=1
      key_bindings=1
      update_config=1
      allow_legacy=1
      ;;
    --key-bindings)    key_bindings=1    ;;
    --no-key-bindings) key_bindings=0    ;;
    --completion)      auto_completion=1 ;;
    --no-completion)   auto_completion=0 ;;
    --update-rc)       update_config=1   ;;
    --no-update-rc)    update_config=0   ;;
    --32)              binary_arch=386   ;;
    --64)              binary_arch=amd64 ;;
    --bin)             ;;
    *)
      echo "unknown option: $opt"
      help
      exit 1
      ;;
  esac
done

cd "$(dirname "${BASH_SOURCE[0]}")"
fzf_base="$(pwd)"

ask() {
  # If stdin is a tty, we are "interactive".
  # non-interactive shell: wait for a linefeed
  #     interactive shell: continue after a single keypress
  read_n=$([ -t 0 ] && echo "-n 1")

  read -p "$1 ([y]/n) " $read_n -r
  echo
  [[ $REPLY =~ ^[Nn]$ ]]
}

check_binary() {
  echo -n "  - Checking fzf executable ... "
  local output
  output=$("$fzf_base"/bin/fzf --version 2>&1)
  if [ $? -ne 0 ]; then
    echo "Error: $output"
    binary_error="Invalid binary"
  elif [ "$version" != "$output" ]; then
    echo "$output != $version"
    binary_error="Invalid version"
  else
    echo "$output"
    binary_error=""
    return 0
  fi
  rm -f "$fzf_base"/bin/fzf
  return 1
}

link_fzf_in_path() {
  if which_fzf="$(command -v fzf)"; then
    echo "  - Found in \$PATH"
    echo "  - Creating symlink: $which_fzf -> bin/fzf"
    (cd "$fzf_base"/bin && rm -f fzf && ln -sf "$which_fzf" fzf)
    check_binary && return
  fi
  return 1
}

try_curl() {
  command -v curl > /dev/null && curl -fL $1 | tar -xz
}

try_wget() {
  command -v wget > /dev/null && wget -O - $1 | tar -xz
}

download() {
  echo "Downloading bin/fzf ..."
  if [[ ! "$version" =~ alpha ]]; then
    if [ -x "$fzf_base"/bin/fzf ]; then
      echo "  - Already exists"
      check_binary && return
    fi
    link_fzf_in_path && return
  fi
  mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
  if [ $? -ne 0 ]; then
    binary_error="Failed to create bin directory"
    return
  fi

  local url
  [[ "$version" =~ alpha ]] &&
    url=https://github.com/junegunn/fzf-bin/releases/download/alpha/${1}.tgz ||
    url=https://github.com/junegunn/fzf-bin/releases/download/$version/${1}.tgz
  set -o pipefail
  if ! (try_curl $url || try_wget $url); then
    set +o pipefail
    binary_error="Failed to download with curl and wget"
    return
  fi
  set +o pipefail

  if [ ! -f fzf ]; then
    binary_error="Failed to download ${1}"
    return
  fi

  chmod +x fzf && check_binary
}

# Try to download binary executable
archi=$(uname -sm)
binary_available=1
binary_error=""
case "$archi" in
  Darwin\ *64)   download fzf-$version-darwin_${binary_arch:-amd64}  ;;
  Darwin\ *86)   download fzf-$version-darwin_${binary_arch:-386}    ;;
  Linux\ *64)    download fzf-$version-linux_${binary_arch:-amd64}   ;;
  Linux\ *86)    download fzf-$version-linux_${binary_arch:-386}     ;;
  Linux\ armv5*) download fzf-$version-linux_${binary_arch:-arm5}    ;;
  Linux\ armv6*) download fzf-$version-linux_${binary_arch:-arm6}    ;;
  Linux\ armv7*) download fzf-$version-linux_${binary_arch:-arm7}    ;;
  Linux\ armv8*) download fzf-$version-linux_${binary_arch:-arm8}    ;;
  FreeBSD\ *64)  download fzf-$version-freebsd_${binary_arch:-amd64} ;;
  FreeBSD\ *86)  download fzf-$version-freebsd_${binary_arch:-386}   ;;
  OpenBSD\ *64)  download fzf-$version-openbsd_${binary_arch:-amd64} ;;
  OpenBSD\ *86)  download fzf-$version-openbsd_${binary_arch:-386}   ;;
  *)             binary_available=0 binary_error=1 ;;
esac

cd "$fzf_base"
if [ -n "$binary_error" ]; then
  if [ $binary_available -eq 0 ]; then
    echo "No prebuilt binary for $archi ..."
  else
    echo "  - $binary_error !!!"
  fi
  if command -v go > /dev/null; then
    echo -n "Building binary (go get -u github.com/junegunn/fzf/src/fzf) ... "
    if [ -z "${GOPATH-}" ]; then
      export GOPATH="${TMPDIR:-/tmp}/fzf-gopath"
      mkdir -p "$GOPATH"
    fi
    if go get -u github.com/junegunn/fzf/src/fzf; then
      echo "OK"
      cp "$GOPATH/bin/fzf" "$fzf_base/bin/"
    else
      echo "Failed to build binary. Installation failed."
      exit 1
    fi
  else
    echo "go executable not found. Installation failed."
    exit 1
  fi
fi

[[ "$*" =~ "--bin" ]] && exit 0

# Auto-completion
if [ -z "$auto_completion" ]; then
  ask "Do you want to enable fuzzy auto-completion?"
  auto_completion=$?
fi

# Key-bindings
if [ -z "$key_bindings" ]; then
  ask "Do you want to enable key bindings?"
  key_bindings=$?
fi

echo
has_zsh=$(command -v zsh > /dev/null && echo 1 || echo 0)
shells=$([ $has_zsh -eq 1 ] && echo "bash zsh" || echo "bash")
for shell in $shells; do
  echo -n "Generate ~/.fzf.$shell ... "
  src=~/.fzf.${shell}

  fzf_completion="[[ \$- == *i* ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null"
  if [ $auto_completion -eq 0 ]; then
    fzf_completion="# $fzf_completion"
  fi

  fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""
  if [ $key_bindings -eq 0 ]; then
    fzf_key_bindings="# $fzf_key_bindings"
  fi

  cat > $src << EOF
# Setup fzf
# ---------
if [[ ! "\$PATH" == *$fzf_base/bin* ]]; then
  export PATH="\$PATH:$fzf_base/bin"
fi

# Auto-completion
# ---------------
$fzf_completion

# Key bindings
# ------------
$fzf_key_bindings

EOF
  echo "OK"
done

# fish
has_fish=$(command -v fish > /dev/null && echo 1 || echo 0)
if [ $has_fish -eq 1 ]; then
  echo -n "Update fish_user_paths ... "
  fish << EOF
  echo \$fish_user_paths | grep $fzf_base/bin > /dev/null
  or set --universal fish_user_paths \$fish_user_paths $fzf_base/bin
EOF
  [ $? -eq 0 ] && echo "OK" || echo "Failed"

  mkdir -p ~/.config/fish/functions
  if [ -e ~/.config/fish/functions/fzf.fish ]; then
    echo -n "Remove unnecessary ~/.config/fish/functions/fzf.fish ... "
    rm -f ~/.config/fish/functions/fzf.fish && echo "OK" || echo "Failed"
  fi

  fish_binding=~/.config/fish/functions/fzf_key_bindings.fish
  if [ $key_bindings -ne 0 ]; then
    echo -n "Symlink $fish_binding ... "
    ln -sf "$fzf_base/shell/key-bindings.fish" \
           "$fish_binding" && echo "OK" || echo "Failed"
  else
    echo -n "Removing $fish_binding ... "
    rm -f "$fish_binding"
    echo "OK"
  fi
fi

append_line() {
  set -e

  local update line file pat lno
  update="$1"
  line="$2"
  file="$3"
  pat="${4:-}"

  echo "Update $file:"
  echo "  - $line"
  [ -f "$file" ] || touch "$file"
  if [ $# -lt 4 ]; then
    lno=$(\grep -nF "$line" "$file" | sed 's/:.*//' | tr '\n' ' ')
  else
    lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ')
  fi
  if [ -n "$lno" ]; then
    echo "    - Already exists: line #$lno"
  else
    if [ $update -eq 1 ]; then
      echo >> "$file"
      echo "$line" >> "$file"
      echo "    + Added"
    else
      echo "    ~ Skipped"
    fi
  fi
  echo
  set +e
}

if [ $update_config -eq 2 ]; then
  echo
  ask "Do you want to update your shell configurat