2014-11-30 15:02:34 -05:00
|
|
|
#!/usr/bin/env bash
|
2013-10-22 21:26:55 -04:00
|
|
|
|
2015-08-02 02:09:57 -04:00
|
|
|
[[ "$@" =~ --pre ]] && version=0.10.2 pre=1 ||
|
|
|
|
version=0.10.1 pre=0
|
2015-01-04 12:17:26 -05:00
|
|
|
|
2015-01-04 22:21:26 -05:00
|
|
|
cd $(dirname $BASH_SOURCE)
|
|
|
|
fzf_base=$(pwd)
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2015-04-13 20:05:45 -04:00
|
|
|
# If stdin is a tty, we are "interactive".
|
|
|
|
[ -t 0 ] && interactive=yes
|
|
|
|
|
2015-01-04 00:29:42 -05:00
|
|
|
ask() {
|
2015-04-13 20:05:45 -04:00
|
|
|
# non-interactive shell: wait for a linefeed
|
|
|
|
# interactive shell: continue after a single keypress
|
|
|
|
[ -n "$interactive" ] && read_n='-n 1' || read_n=
|
|
|
|
|
|
|
|
read -p "$1 ([y]/n) " $read_n -r
|
2015-01-04 00:29:42 -05:00
|
|
|
echo
|
|
|
|
[[ ! $REPLY =~ ^[Nn]$ ]]
|
|
|
|
}
|
|
|
|
|
2015-01-04 22:21:26 -05:00
|
|
|
check_binary() {
|
2015-01-07 06:08:05 -05:00
|
|
|
echo -n " - Checking fzf executable ... "
|
2015-01-13 09:46:38 -05:00
|
|
|
local output=$("$fzf_base"/bin/fzf --version 2>&1)
|
|
|
|
if [ "$version" = "$output" ]; then
|
|
|
|
echo "$output"
|
2015-01-17 06:40:00 -05:00
|
|
|
binary_error=""
|
2015-01-13 09:46:38 -05:00
|
|
|
else
|
|
|
|
echo "$output != $version"
|
2015-01-07 06:08:05 -05:00
|
|
|
rm -f "$fzf_base"/bin/fzf
|
2015-01-13 09:46:38 -05:00
|
|
|
binary_error="Invalid binary"
|
|
|
|
return 1
|
2015-01-04 22:21:26 -05:00
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2015-01-07 06:08:05 -05:00
|
|
|
symlink() {
|
|
|
|
echo " - Creating symlink: bin/$1 -> bin/fzf"
|
2015-01-12 22:29:12 -05:00
|
|
|
(cd "$fzf_base"/bin &&
|
2015-01-17 06:40:00 -05:00
|
|
|
rm -f fzf &&
|
2015-01-12 22:29:12 -05:00
|
|
|
ln -sf $1 fzf)
|
2015-01-17 06:40:00 -05:00
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
binary_error="Failed to create symlink"
|
|
|
|
return 1
|
|
|
|
fi
|
2015-01-07 06:08:05 -05:00
|
|
|
}
|
|
|
|
|
2015-01-01 14:49:30 -05:00
|
|
|
download() {
|
2015-01-13 09:46:38 -05:00
|
|
|
echo "Downloading bin/fzf ..."
|
2015-08-02 02:09:57 -04:00
|
|
|
if [ $pre = 0 ]; then
|
|
|
|
if [ -x "$fzf_base"/bin/fzf ]; then
|
|
|
|
echo " - Already exists"
|
|
|
|
check_binary && return
|
|
|
|
elif [ -x "$fzf_base"/bin/$1 ]; then
|
|
|
|
symlink $1 && check_binary && return
|
|
|
|
fi
|
2015-01-04 00:29:42 -05:00
|
|
|
fi
|
2015-01-03 12:42:58 -05:00
|
|
|
mkdir -p "$fzf_base"/bin && cd "$fzf_base"/bin
|
|
|
|
if [ $? -ne 0 ]; then
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_error="Failed to create bin directory"
|
|
|
|
return
|
2015-01-03 12:42:58 -05:00
|
|
|
fi
|
|
|
|
|
2015-01-06 10:24:05 -05:00
|
|
|
local url=https://github.com/junegunn/fzf-bin/releases/download/$version/${1}.tgz
|
2015-01-03 12:42:58 -05:00
|
|
|
if which curl > /dev/null; then
|
|
|
|
curl -fL $url | tar -xz
|
|
|
|
elif which wget > /dev/null; then
|
|
|
|
wget -O - $url | tar -xz
|
2014-06-20 12:43:25 -04:00
|
|
|
else
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_error="curl or wget not found"
|
|
|
|
return
|
2015-01-03 12:42:58 -05:00
|
|
|
fi
|
|
|
|
|
|
|
|
if [ ! -f $1 ]; then
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_error="Failed to download ${1}"
|
|
|
|
return
|
2014-06-20 12:43:25 -04:00
|
|
|
fi
|
2015-01-03 12:42:58 -05:00
|
|
|
|
2015-01-07 06:08:05 -05:00
|
|
|
chmod +x $1 && symlink $1 && check_binary
|
2015-01-01 14:49:30 -05:00
|
|
|
}
|
|
|
|
|
2015-01-03 12:42:58 -05:00
|
|
|
# Try to download binary executable
|
2015-01-04 11:25:54 -05:00
|
|
|
archi=$(uname -sm)
|
2015-01-04 22:21:26 -05:00
|
|
|
binary_available=1
|
|
|
|
binary_error=""
|
2015-01-05 12:04:06 -05:00
|
|
|
case "$archi" in
|
2015-01-06 10:24:05 -05:00
|
|
|
Darwin\ x86_64) download fzf-$version-darwin_amd64 ;;
|
|
|
|
Darwin\ i*86) download fzf-$version-darwin_386 ;;
|
|
|
|
Linux\ x86_64) download fzf-$version-linux_amd64 ;;
|
|
|
|
Linux\ i*86) download fzf-$version-linux_386 ;;
|
2015-03-11 21:06:15 -04:00
|
|
|
*) binary_available=0 binary_error=1 ;;
|
2015-01-05 12:04:06 -05:00
|
|
|
esac
|
2015-01-03 12:42:58 -05:00
|
|
|
|
2015-01-04 22:21:26 -05:00
|
|
|
cd "$fzf_base"
|
|
|
|
if [ -n "$binary_error" ]; then
|
2015-01-03 12:42:58 -05:00
|
|
|
if [ $binary_available -eq 0 ]; then
|
2015-01-04 22:21:26 -05:00
|
|
|
echo "No prebuilt binary for $archi ... "
|
2015-01-03 12:42:58 -05:00
|
|
|
else
|
2015-01-07 06:08:05 -05:00
|
|
|
echo " - $binary_error !!!"
|
2015-01-03 12:42:58 -05:00
|
|
|
fi
|
|
|
|
echo "Installing legacy Ruby version ..."
|
2015-01-01 14:49:30 -05:00
|
|
|
|
|
|
|
# ruby executable
|
|
|
|
echo -n "Checking Ruby executable ... "
|
|
|
|
ruby=`which ruby`
|
2013-12-25 11:06:46 -05:00
|
|
|
if [ $? -ne 0 ]; then
|
2015-01-07 06:08:05 -05:00
|
|
|
echo "ruby executable not found !!!"
|
2013-12-25 11:06:46 -05:00
|
|
|
exit 1
|
|
|
|
fi
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2015-01-01 14:49:30 -05:00
|
|
|
# System ruby is preferred
|
|
|
|
system_ruby=/usr/bin/ruby
|
|
|
|
if [ -x $system_ruby -a $system_ruby != "$ruby" ]; then
|
|
|
|
$system_ruby --disable-gems -rcurses -e0 2> /dev/null
|
|
|
|
[ $? -eq 0 ] && ruby=$system_ruby
|
|
|
|
fi
|
|
|
|
|
|
|
|
echo "OK ($ruby)"
|
|
|
|
|
|
|
|
# Curses-support
|
|
|
|
echo -n "Checking Curses support ... "
|
|
|
|
"$ruby" -rcurses -e0 2> /dev/null
|
2013-12-25 11:39:17 -05:00
|
|
|
if [ $? -eq 0 ]; then
|
2015-01-01 14:49:30 -05:00
|
|
|
echo "OK"
|
2013-12-25 11:39:17 -05:00
|
|
|
else
|
2015-01-01 14:49:30 -05:00
|
|
|
echo "Not found"
|
|
|
|
echo "Installing 'curses' gem ... "
|
|
|
|
if (( EUID )); then
|
|
|
|
/usr/bin/env gem install curses --user-install
|
|
|
|
else
|
|
|
|
/usr/bin/env gem install curses
|
|
|
|
fi
|
|
|
|
if [ $? -ne 0 ]; then
|
|
|
|
echo
|
|
|
|
echo "Failed to install 'curses' gem."
|
|
|
|
if [[ $(uname -r) =~ 'ARCH' ]]; then
|
|
|
|
echo "Make sure that base-devel package group is installed."
|
|
|
|
fi
|
|
|
|
exit 1
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Ruby version
|
|
|
|
echo -n "Checking Ruby version ... "
|
|
|
|
"$ruby" -e 'exit RUBY_VERSION >= "1.9"'
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
echo ">= 1.9"
|
|
|
|
"$ruby" --disable-gems -rcurses -e0 2> /dev/null
|
|
|
|
if [ $? -eq 0 ]; then
|
|
|
|
fzf_cmd="$ruby --disable-gems $fzf_base/fzf"
|
|
|
|
else
|
|
|
|
fzf_cmd="$ruby $fzf_base/fzf"
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
echo "< 1.9"
|
2013-12-25 11:39:17 -05:00
|
|
|
fzf_cmd="$ruby $fzf_base/fzf"
|
|
|
|
fi
|
2015-03-13 00:08:42 -04:00
|
|
|
|
|
|
|
# Create fzf script
|
|
|
|
echo -n "Creating wrapper script for fzf ... "
|
|
|
|
rm -f "$fzf_base"/bin/fzf
|
|
|
|
echo "#!/bin/sh" > "$fzf_base"/bin/fzf
|
|
|
|
echo "$fzf_cmd \"\$@\"" >> "$fzf_base"/bin/fzf
|
|
|
|
chmod +x "$fzf_base"/bin/fzf
|
|
|
|
echo "OK"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
|
2015-04-16 08:58:41 -04:00
|
|
|
[[ "$*" =~ "--bin" ]] && exit 0
|
|
|
|
|
2013-11-19 11:29:36 -05:00
|
|
|
# Auto-completion
|
2015-01-04 00:29:42 -05:00
|
|
|
ask "Do you want to add auto-completion support?"
|
2013-11-26 05:01:01 -05:00
|
|
|
auto_completion=$?
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2013-12-21 10:18:41 -05:00
|
|
|
# Key-bindings
|
2015-01-04 00:29:42 -05:00
|
|
|
ask "Do you want to add key bindings?"
|
2013-12-21 10:18:41 -05:00
|
|
|
key_bindings=$?
|
|
|
|
|
2013-11-19 11:29:36 -05:00
|
|
|
echo
|
|
|
|
for shell in bash zsh; do
|
2013-11-26 05:01:01 -05:00
|
|
|
echo -n "Generate ~/.fzf.$shell ... "
|
|
|
|
src=~/.fzf.${shell}
|
2013-11-19 11:29:36 -05:00
|
|
|
|
2015-05-12 09:54:48 -04:00
|
|
|
fzf_completion="[[ \$- =~ i ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null"
|
2015-05-09 07:15:14 -04:00
|
|
|
if [ $auto_completion -ne 0 ]; then
|
2013-11-26 05:01:01 -05:00
|
|
|
fzf_completion="# $fzf_completion"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
|
2015-03-13 00:08:42 -04:00
|
|
|
fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""
|
|
|
|
if [ $key_bindings -ne 0 ]; then
|
|
|
|
fzf_key_bindings="# $fzf_key_bindings"
|
|
|
|
fi
|
2015-01-12 12:39:00 -05:00
|
|
|
|
2015-03-13 00:08:42 -04:00
|
|
|
cat > $src << EOF
|
2015-01-12 12:39:00 -05:00
|
|
|
# Setup fzf
|
|
|
|
# ---------
|
|
|
|
if [[ ! "\$PATH" =~ "$fzf_base/bin" ]]; then
|
2015-03-25 14:44:18 -04:00
|
|
|
export PATH="\$PATH:$fzf_base/bin"
|
2015-01-01 14:49:30 -05:00
|
|
|
fi
|
2013-12-21 10:18:41 -05:00
|
|
|
|
2015-03-25 14:06:31 -04:00
|
|
|
# Man path
|
|
|
|
# --------
|
2015-03-25 14:26:28 -04:00
|
|
|
if [[ ! "\$MANPATH" =~ "$fzf_base/man" && -d "$fzf_base/man" ]]; then
|
2015-03-25 14:11:08 -04:00
|
|
|
export MANPATH="\$MANPATH:$fzf_base/man"
|
|
|
|
fi
|
2015-03-25 14:06:31 -04:00
|
|
|
|
2013-12-21 10:18:41 -05:00
|
|
|
# Auto-completion
|
|
|
|
# ---------------
|
2014-05-03 11:49:29 -04:00
|
|
|
$fzf_completion
|
2013-11-26 05:01:01 -05:00
|
|
|
|
2013-12-21 10:18:41 -05:00
|
|
|
# Key bindings
|
|
|
|
# ------------
|
2015-03-13 00:08:42 -04:00
|
|
|
$fzf_key_bindings
|
2013-12-21 10:18:41 -05:00
|
|
|
|
2015-03-13 00:08:42 -04:00
|
|
|
EOF
|
2013-11-26 05:01:01 -05:00
|
|
|
echo "OK"
|
|
|
|
done
|
|
|
|
|
2014-05-01 22:27:32 -04:00
|
|
|
# fish
|
|
|
|
has_fish=0
|
2015-03-13 00:08:42 -04:00
|
|
|
if [ -n "$(which fish 2> /dev/null)" ]; then
|
2014-05-01 22:27:32 -04:00
|
|
|
has_fish=1
|
2015-03-13 00:08:42 -04:00
|
|
|
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"
|
|
|
|
|
2014-05-01 22:27:32 -04:00
|
|
|
mkdir -p ~/.config/fish/functions
|
2015-03-13 00:08:42 -04:00
|
|
|
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"
|
2015-01-04 00:35:13 -05:00
|
|
|
fi
|
2014-05-01 22:27:32 -04:00
|
|
|
|
|
|
|
if [ $key_bindings -eq 0 ]; then
|
2015-03-13 00:08:42 -04:00
|
|
|
echo -n "Symlink ~/.config/fish/functions/fzf_key_bindings.fish ... "
|
|
|
|
ln -sf $fzf_base/shell/key-bindings.fish \
|
|
|
|
~/.config/fish/functions/fzf_key_bindings.fish && echo "OK" || echo "Failed"
|
2014-05-01 22:27:32 -04:00
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
append_line() {
|
|
|
|
echo "Update $2:"
|
|
|
|
echo " - $1"
|
|
|
|
[ -f "$2" ] || touch "$2"
|
2014-09-17 23:55:28 -04:00
|
|
|
if [ $# -lt 3 ]; then
|
2014-09-18 06:03:01 -04:00
|
|
|
line=$(\grep -nF "$1" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
2014-09-17 23:55:28 -04:00
|
|
|
else
|
2014-09-18 06:03:01 -04:00
|
|
|
line=$(\grep -nF "$3" "$2" | sed 's/:.*//' | tr '\n' ' ')
|
2014-09-17 23:55:28 -04:00
|
|
|
fi
|
2014-03-30 11:53:35 -04:00
|
|
|
if [ -n "$line" ]; then
|
2014-09-18 06:03:01 -04:00
|
|
|
echo " - Already exists: line #$line"
|
2013-11-26 05:01:01 -05:00
|
|
|
else
|
2014-05-01 22:27:32 -04:00
|
|
|
echo "$1" >> "$2"
|
2014-09-18 06:03:01 -04:00
|
|
|
echo " + Added"
|
2013-11-19 11:29:36 -05:00
|
|
|
fi
|
|
|
|
echo
|
2014-05-01 22:27:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
echo
|
|
|
|
for shell in bash zsh; do
|
2014-09-18 00:01:39 -04:00
|
|
|
append_line "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" ~/.${shell}rc "~/.fzf.${shell}"
|
2013-11-19 11:29:36 -05:00
|
|
|
done
|
|
|
|
|
2014-05-01 22:27:32 -04:00
|
|
|
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then
|
|
|
|
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
|
|
|
|
append_line "fzf_key_bindings" "$bind_file"
|
|
|
|
fi
|
|
|
|
|
2013-11-28 23:42:13 -05:00
|
|
|
cat << EOF
|
2014-05-01 22:27:32 -04:00
|
|
|
Finished. Restart your shell or reload config file.
|
2013-12-25 11:54:29 -05:00
|
|
|
source ~/.bashrc # bash
|
|
|
|
source ~/.zshrc # zsh
|
2014-05-01 22:27:32 -04:00
|
|
|
EOF
|
|
|
|
[ $has_fish -eq 1 ] && echo " fzf_key_bindings # fish"; cat << EOF
|
2013-11-28 23:42:13 -05:00
|
|
|
|
2014-05-20 12:14:21 -04:00
|
|
|
Use uninstall script to remove fzf.
|
2014-03-30 11:53:35 -04:00
|
|
|
|
|
|
|
For more information, see: https://github.com/junegunn/fzf
|
2013-11-28 23:42:13 -05:00
|
|
|
EOF
|
2013-10-22 21:26:55 -04:00
|
|
|
|