[install] Support for XDG Base Directory Specification (#1282)

Add --xdg option which makes the installer generate files under $XDG_CONFIG_HOME/fzf.
This commit is contained in:
Mark 2018-05-31 19:54:58 -07:00 committed by Junegunn Choi
parent 62f062ecfa
commit 2ff19084ca
2 changed files with 74 additions and 27 deletions

29
install
View File

@ -9,6 +9,9 @@ update_config=2
binary_arch= binary_arch=
allow_legacy= allow_legacy=
shells="bash zsh fish" shells="bash zsh fish"
prefix='~/.fzf'
prefix_expand=~/.fzf
fish_dir=${XDG_CONFIG_HOME:-$HOME/.config}/fish
help() { help() {
cat << EOF cat << EOF
@ -18,6 +21,7 @@ usage: $0 [OPTIONS]
--bin Download fzf binary only; Do not generate ~/.fzf.{bash,zsh} --bin Download fzf binary only; Do not generate ~/.fzf.{bash,zsh}
--all Download fzf binary and update configuration files --all Download fzf binary and update configuration files
to enable key bindings and fuzzy completion to enable key bindings and fuzzy completion
--xdg Generate files under \$XDG_CONFIG_HOME/fzf
--[no-]key-bindings Enable/disable key bindings (CTRL-T, CTRL-R, ALT-C) --[no-]key-bindings Enable/disable key bindings (CTRL-T, CTRL-R, ALT-C)
--[no-]completion Enable/disable fuzzy completion (bash & zsh) --[no-]completion Enable/disable fuzzy completion (bash & zsh)
--[no-]update-rc Whether or not to update shell configuration files --[no-]update-rc Whether or not to update shell configuration files
@ -43,6 +47,11 @@ for opt in "$@"; do
update_config=1 update_config=1
allow_legacy=1 allow_legacy=1
;; ;;
--xdg)
prefix='"${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf'
prefix_expand=${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf
mkdir -p "${XDG_CONFIG_HOME:-$HOME/.config}/fzf"
;;
--key-bindings) key_bindings=1 ;; --key-bindings) key_bindings=1 ;;
--no-key-bindings) key_bindings=0 ;; --no-key-bindings) key_bindings=0 ;;
--completion) auto_completion=1 ;; --completion) auto_completion=1 ;;
@ -240,8 +249,8 @@ fi
echo echo
for shell in $shells; do for shell in $shells; do
[[ "$shell" = fish ]] && continue [[ "$shell" = fish ]] && continue
echo -n "Generate ~/.fzf.$shell ... " src=${prefix_expand}.${shell}
src=~/.fzf.${shell} echo -n "Generate $src ... "
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 -eq 0 ]; then if [ $auto_completion -eq 0 ]; then
@ -253,7 +262,7 @@ for shell in $shells; do
fzf_key_bindings="# $fzf_key_bindings" fzf_key_bindings="# $fzf_key_bindings"
fi fi
cat > $src << EOF cat > "$src" << EOF
# Setup fzf # Setup fzf
# --------- # ---------
if [[ ! "\$PATH" == *$fzf_base/bin* ]]; then if [[ ! "\$PATH" == *$fzf_base/bin* ]]; then
@ -281,13 +290,13 @@ if [[ "$shells" =~ fish ]]; then
EOF EOF
[ $? -eq 0 ] && echo "OK" || echo "Failed" [ $? -eq 0 ] && echo "OK" || echo "Failed"
mkdir -p ~/.config/fish/functions mkdir -p "${fish_dir}/functions"
if [ -e ~/.config/fish/functions/fzf.fish ]; then if [ -e "${fish_dir}/functions/fzf.fish" ]; then
echo -n "Remove unnecessary ~/.config/fish/functions/fzf.fish ... " echo -n "Remove unnecessary ${fish_dir}/functions/fzf.fish ... "
rm -f ~/.config/fish/functions/fzf.fish && echo "OK" || echo "Failed" rm -f "${fish_dir}/functions/fzf.fish" && echo "OK" || echo "Failed"
fi fi
fish_binding=~/.config/fish/functions/fzf_key_bindings.fish fish_binding="${fish_dir}/functions/fzf_key_bindings.fish"
if [ $key_bindings -ne 0 ]; then if [ $key_bindings -ne 0 ]; then
echo -n "Symlink $fish_binding ... " echo -n "Symlink $fish_binding ... "
ln -sf "$fzf_base/shell/key-bindings.fish" \ ln -sf "$fzf_base/shell/key-bindings.fish" \
@ -353,11 +362,11 @@ echo
for shell in $shells; do for shell in $shells; do
[[ "$shell" = fish ]] && continue [[ "$shell" = fish ]] && continue
[ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc [ $shell = zsh ] && dest=${ZDOTDIR:-~}/.zshrc || dest=~/.bashrc
append_line $update_config "[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" "$dest" "~/.fzf.${shell}" append_line $update_config "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" "$dest" "${prefix}.${shell}"
done done
if [ $key_bindings -eq 1 ] && [[ "$shells" =~ fish ]]; then if [ $key_bindings -eq 1 ] && [[ "$shells" =~ fish ]]; then
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"
if [ ! -e "$bind_file" ]; then if [ ! -e "$bind_file" ]; then
create_file "$bind_file" \ create_file "$bind_file" \
'function fish_user_key_bindings' \ 'function fish_user_key_bindings' \

View File

@ -1,12 +1,45 @@
#!/usr/bin/env bash #!/usr/bin/env bash
confirm() { xdg=0
while [ 1 ]; do prefix='~/.fzf'
read -p "$1" -n 1 -r prefix_expand=~/.fzf
echo fish_dir=${XDG_CONFIG_HOME:-$HOME/.config}/fish
if [[ "$REPLY" =~ ^[Yy] ]]; then
help() {
cat << EOF
usage: $0 [OPTIONS]
--help Show this message
--xdg Remove files generated under \$XDG_CONFIG_HOME/fzf
EOF
}
for opt in "$@"; do
case $opt in
--help)
help
exit 0
;;
--xdg)
xdg=1
prefix='"${XDG_CONFIG_HOME:-$HOME/.config}"/fzf/fzf'
prefix_expand=${XDG_CONFIG_HOME:-$HOME/.config}/fzf/fzf
;;
*)
echo "unknown option: $opt"
help
exit 1
;;
esac
done
ask() {
while true; do
read -p "$1 ([y]/n) " -r
REPLY=${REPLY:-"y"}
if [[ $REPLY =~ ^[Yy]$ ]]; then
return 0 return 0
elif [[ "$REPLY" =~ ^[Nn] ]]; then elif [[ $REPLY =~ ^[Nn]$ ]]; then
return 1 return 1
fi fi
done done
@ -40,7 +73,7 @@ remove_line() {
content=$(sed 's/^[0-9]*://' <<< "$line") content=$(sed 's/^[0-9]*://' <<< "$line")
match=1 match=1
echo " - Line #$line_no: $content" echo " - Line #$line_no: $content"
[ "$content" = "$1" ] || confirm " - Remove (y/n) ? " [ "$content" = "$1" ] || ask " - Remove?"
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$src.bak" && awk -v n=$line_no 'NR == n {next} {print}' "$src" > "$src.bak" &&
mv "$src.bak" "$src" || break mv "$src.bak" "$src" || break
@ -55,25 +88,30 @@ remove_line() {
} }
for shell in bash zsh; do for shell in bash zsh; do
remove ~/.fzf.${shell} shell_config=${prefix_expand}.${shell}
remove "${shell_config}"
remove_line ~/.${shell}rc \ remove_line ~/.${shell}rc \
"[ -f ~/.fzf.${shell} ] && source ~/.fzf.${shell}" \ "[ -f ${prefix}.${shell} ] && source ${prefix}.${shell}" \
"source ~/.fzf.${shell}" "source ${prefix}.${shell}"
done done
bind_file=~/.config/fish/functions/fish_user_key_bindings.fish bind_file="${fish_dir}/functions/fish_user_key_bindings.fish"
if [ -f "$bind_file" ]; then if [ -f "$bind_file" ]; then
remove_line "$bind_file" "fzf_key_bindings" remove_line "$bind_file" "fzf_key_bindings"
fi fi
if [ -d ~/.config/fish/functions ]; then if [ -d "${fish_dir}/functions" ]; then
remove ~/.config/fish/functions/fzf.fish remove "${fish_dir}/functions/fzf.fish"
remove ~/.config/fish/functions/fzf_key_bindings.fish remove "${fish_dir}/functions/fzf_key_bindings.fish"
if [ "$(ls -A ~/.config/fish/functions)" ]; then if [ "$(ls -A "${fish_dir}/functions")" ]; then
echo "Can't delete non-empty directory: \"~/.config/fish/functions\"" echo "Can't delete non-empty directory: \"${fish_dir}/functions\""
else else
rmdir ~/.config/fish/functions rmdir "${fish_dir}/functions"
fi fi
fi fi
config_dir=$(dirname "$prefix_expand")
if [[ "$xdg" = 1 ]] && [[ "$config_dir" = */fzf ]] && [[ -d "$config_dir" ]]; then
rmdir "$config_dir"
fi