Add command-line flags to install script

Close #392

  usage: ./install [OPTIONS]

      --help               Show this message
      --bin                Download fzf binary only
      --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
This commit is contained in:
Junegunn Choi 2015-10-23 14:56:27 +09:00
parent 4d709e0dd2
commit 02203c7739
2 changed files with 93 additions and 27 deletions

View File

@ -68,7 +68,7 @@ Or you can have [vim-plug](https://github.com/junegunn/vim-plug) manage fzf
(recommended): (recommended):
```vim ```vim
Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': 'yes \| ./install' } Plug 'junegunn/fzf', { 'dir': '~/.fzf', 'do': './install --all' }
``` ```
#### Upgrading fzf #### Upgrading fzf

118
install
View File

@ -1,10 +1,56 @@
#!/usr/bin/env bash #!/usr/bin/env bash
set -u
[[ "$@" =~ --pre ]] && version=0.10.8 pre=1 || [[ "$@" =~ --pre ]] && version=0.10.8 pre=1 ||
version=0.10.8 pre=0 version=0.10.8 pre=0
auto_completion=
key_bindings=
update_config=1
help() {
cat << EOF
usage: $0 [OPTIONS]
--help Show this message
--bin Download fzf binary only
--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
EOF
}
for opt in $@; do
case $opt in
--help)
help
exit 0
;;
--all)
auto_completion=1
key_bindings=1
update_config=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 ;;
*)
echo "unknown option: $opt"
help
exit 1
;;
esac
done
cd $(dirname $BASH_SOURCE) cd $(dirname $BASH_SOURCE)
fzf_base=$(pwd) fzf_base="$(pwd)"
# If stdin is a tty, we are "interactive". # If stdin is a tty, we are "interactive".
[ -t 0 ] && interactive=yes [ -t 0 ] && interactive=yes
@ -16,7 +62,7 @@ ask() {
read -p "$1 ([y]/n) " $read_n -r read -p "$1 ([y]/n) " $read_n -r
echo echo
[[ ! $REPLY =~ ^[Nn]$ ]] [[ $REPLY =~ ^[Nn]$ ]]
} }
check_binary() { check_binary() {
@ -173,12 +219,16 @@ fi
[[ "$*" =~ "--bin" ]] && exit 0 [[ "$*" =~ "--bin" ]] && exit 0
# Auto-completion # Auto-completion
ask "Do you want to add auto-completion support?" if [ -z "$auto_completion" ]; then
auto_completion=$? ask "Do you want to enable fuzzy auto-completion?"
auto_completion=$?
fi
# Key-bindings # Key-bindings
ask "Do you want to add key bindings?" if [ -z "$key_bindings" ]; then
key_bindings=$? ask "Do you want to enable key bindings?"
key_bindings=$?
fi
echo echo
for shell in bash zsh; do for shell in bash zsh; do
@ -186,12 +236,12 @@ for shell in bash zsh; do
src=~/.fzf.${shell} src=~/.fzf.${shell}
fzf_completion="[[ \$- == *i* ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null" fzf_completion="[[ \$- == *i* ]] && source \"$fzf_base/shell/completion.${shell}\" 2> /dev/null"
if [ $auto_completion -ne 0 ]; then if [ $auto_completion -eq 0 ]; then
fzf_completion="# $fzf_completion" fzf_completion="# $fzf_completion"
fi fi
fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\"" fzf_key_bindings="source \"$fzf_base/shell/key-bindings.${shell}\""
if [ $key_bindings -ne 0 ]; then if [ $key_bindings -eq 0 ]; then
fzf_key_bindings="# $fzf_key_bindings" fzf_key_bindings="# $fzf_key_bindings"
fi fi
@ -237,29 +287,45 @@ EOF
rm -f ~/.config/fish/functions/fzf.fish && echo "OK" || echo "Failed" rm -f ~/.config/fish/functions/fzf.fish && echo "OK" || echo "Failed"
fi fi
if [ $key_bindings -eq 0 ]; then fish_binding=~/.config/fish/functions/fzf_key_bindings.fish
echo -n "Symlink ~/.config/fish/functions/fzf_key_bindings.fish ... " if [ $key_bindings -ne 0 ]; then
ln -sf $fzf_base/shell/key-bindings.fish \ echo -n "Symlink $fish_binding ... "
~/.config/fish/functions/fzf_key_bindings.fish && echo "OK" || echo "Failed" 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
fi fi
append_line() { append_line() {
set -e set -e
echo "Update $2:"
echo " - $1" local skip line file pat lno
[ -f "$2" ] || touch "$2" skip="$1"
if [ $# -lt 3 ]; then line="$2"
line=$(\grep -nF "$1" "$2" | sed 's/:.*//' | tr '\n' ' ') 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 else
line=$(\grep -nF "$3" "$2" | sed 's/:.*//' | tr '\n' ' ') lno=$(\grep -nF "$pat" "$file" | sed 's/:.*//' | tr '\n' ' ')
fi fi
if [ -n "$line" ]; then if [ -n "$lno" ]; then
echo " - Already exists: line #$line" echo " - Already exists: line #$lno"
else else
echo >> "$2" if [ $skip -eq 1 ]; then
echo "$1" >> "$2" echo >> "$file"
echo " + Added" echo "$line" >> "$file"
echo " + Added"
else
echo " ~ Skipped"
fi
fi fi
echo echo
set +e set +e
@ -267,12 +333,12 @@ append_line() {
echo echo
for shell in bash zsh; do for shell in bash zsh; do
append_line "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" ~/.${shell}rc "~/.fzf.${shell}" append_line $update_config "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" ~/.${shell}rc "~/.fzf.${shell}"
done done
if [ $key_bindings -eq 0 -a $has_fish -eq 1 ]; then if [ $key_bindings -eq 1 -a $has_fish -eq 1 ]; then
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish bind_file=~/.config/fish/functions/fish_user_key_bindings.fish
append_line "fzf_key_bindings" "$bind_file" append_line $update_config "fzf_key_bindings" "$bind_file"
fi fi
cat << EOF cat << EOF