mirror of
https://github.com/ohmyzsh/ohmyzsh.git
synced 2024-12-18 02:20:09 +00:00
refactor(fzf): adopt code style and simplify function naming (#10514)
This commit is contained in:
parent
41c15a2159
commit
010c36d4ab
1 changed files with 137 additions and 137 deletions
|
@ -1,176 +1,176 @@
|
||||||
function setup_using_base_dir() {
|
function fzf_setup_using_base_dir() {
|
||||||
local fzf_base fzf_shell fzfdirs dir
|
local fzf_base fzf_shell fzfdirs dir
|
||||||
|
|
||||||
test -d "${FZF_BASE}" && fzf_base="${FZF_BASE}"
|
test -d "${FZF_BASE}" && fzf_base="${FZF_BASE}"
|
||||||
|
|
||||||
|
if [[ -z "${fzf_base}" ]]; then
|
||||||
|
fzfdirs=(
|
||||||
|
"${HOME}/.fzf"
|
||||||
|
"${HOME}/.nix-profile/share/fzf"
|
||||||
|
"${XDG_DATA_HOME:-$HOME/.local/share}/fzf"
|
||||||
|
"/usr/local/opt/fzf"
|
||||||
|
"/usr/share/fzf"
|
||||||
|
"/usr/local/share/examples/fzf"
|
||||||
|
)
|
||||||
|
for dir in ${fzfdirs}; do
|
||||||
|
if [[ -d "${dir}" ]]; then
|
||||||
|
fzf_base="${dir}"
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
if [[ -z "${fzf_base}" ]]; then
|
if [[ -z "${fzf_base}" ]]; then
|
||||||
fzfdirs=(
|
if (( ${+commands[fzf-share]} )) && dir="$(fzf-share)" && [[ -d "${dir}" ]]; then
|
||||||
"${HOME}/.fzf"
|
fzf_base="${dir}"
|
||||||
"${HOME}/.nix-profile/share/fzf"
|
elif (( ${+commands[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
|
||||||
"${XDG_DATA_HOME:-$HOME/.local/share}/fzf"
|
if [[ -d "${dir}" ]]; then
|
||||||
"/usr/local/opt/fzf"
|
fzf_base="${dir}"
|
||||||
"/usr/share/fzf"
|
|
||||||
"/usr/local/share/examples/fzf"
|
|
||||||
)
|
|
||||||
for dir in ${fzfdirs}; do
|
|
||||||
if [[ -d "${dir}" ]]; then
|
|
||||||
fzf_base="${dir}"
|
|
||||||
break
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
if [[ -z "${fzf_base}" ]]; then
|
|
||||||
if (( ${+commands[fzf-share]} )) && dir="$(fzf-share)" && [[ -d "${dir}" ]]; then
|
|
||||||
fzf_base="${dir}"
|
|
||||||
elif (( ${+commands[brew]} )) && dir="$(brew --prefix fzf 2>/dev/null)"; then
|
|
||||||
if [[ -d "${dir}" ]]; then
|
|
||||||
fzf_base="${dir}"
|
|
||||||
fi
|
|
||||||
fi
|
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
fi
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
if [[ ! -d "${fzf_base}" ]]; then
|
if [[ ! -d "${fzf_base}" ]]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
|
# Fix fzf shell directory for Arch Linux, NixOS or Void Linux packages
|
||||||
if [[ ! -d "${fzf_base}/shell" ]]; then
|
if [[ ! -d "${fzf_base}/shell" ]]; then
|
||||||
fzf_shell="${fzf_base}"
|
fzf_shell="${fzf_base}"
|
||||||
else
|
else
|
||||||
fzf_shell="${fzf_base}/shell"
|
fzf_shell="${fzf_base}/shell"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Setup fzf binary path
|
# Setup fzf binary path
|
||||||
if (( ! ${+commands[fzf]} )) && [[ "$PATH" != *$fzf_base/bin* ]]; then
|
if (( ! ${+commands[fzf]} )) && [[ "$PATH" != *$fzf_base/bin* ]]; then
|
||||||
export PATH="$PATH:$fzf_base/bin"
|
export PATH="$PATH:$fzf_base/bin"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Auto-completion
|
# Auto-completion
|
||||||
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
||||||
source "${fzf_shell}/completion.zsh" 2> /dev/null
|
source "${fzf_shell}/completion.zsh" 2> /dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Key bindings
|
# Key bindings
|
||||||
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
|
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
|
||||||
source "${fzf_shell}/key-bindings.zsh"
|
source "${fzf_shell}/key-bindings.zsh"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function setup_using_debian_package() {
|
function fzf_setup_using_debian() {
|
||||||
if (( ! $+commands[dpkg] )) || ! dpkg -s fzf &>/dev/null; then
|
if (( ! $+commands[dpkg] )) || ! dpkg -s fzf &>/dev/null; then
|
||||||
# Either not a debian based distro, or no fzf installed
|
# Either not a debian based distro, or no fzf installed
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# NOTE: There is no need to configure PATH for debian package, all binaries
|
# NOTE: There is no need to configure PATH for debian package, all binaries
|
||||||
# are installed to /usr/bin by default
|
# are installed to /usr/bin by default
|
||||||
|
|
||||||
local completions key_bindings
|
local completions key_bindings
|
||||||
|
|
||||||
case $PREFIX in
|
case $PREFIX in
|
||||||
*com.termux*)
|
*com.termux*)
|
||||||
# Support Termux package
|
# Support Termux package
|
||||||
completions="${PREFIX}/share/fzf/completion.zsh"
|
completions="${PREFIX}/share/fzf/completion.zsh"
|
||||||
key_bindings="${PREFIX}/share/fzf/key-bindings.zsh"
|
key_bindings="${PREFIX}/share/fzf/key-bindings.zsh"
|
||||||
;;
|
;;
|
||||||
*)
|
*)
|
||||||
# Determine completion file path: first bullseye/sid, then buster/stretch
|
# Determine completion file path: first bullseye/sid, then buster/stretch
|
||||||
completions="/usr/share/doc/fzf/examples/completion.zsh"
|
completions="/usr/share/doc/fzf/examples/completion.zsh"
|
||||||
[[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
|
[[ -f "$completions" ]] || completions="/usr/share/zsh/vendor-completions/_fzf"
|
||||||
key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
|
key_bindings="/usr/share/doc/fzf/examples/key-bindings.zsh"
|
||||||
;;
|
;;
|
||||||
esac
|
esac
|
||||||
|
|
||||||
# Auto-completion
|
# Auto-completion
|
||||||
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
||||||
source $completions 2> /dev/null
|
source $completions 2> /dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Key bindings
|
# Key bindings
|
||||||
if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
|
if [[ ! "$DISABLE_FZF_KEY_BINDINGS" == "true" ]]; then
|
||||||
source $key_bindings
|
source $key_bindings
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup_using_opensuse_package() {
|
function fzf_setup_using_opensuse() {
|
||||||
# OpenSUSE installs fzf in /usr/bin/fzf
|
# OpenSUSE installs fzf in /usr/bin/fzf
|
||||||
# If the command is not found, the package isn't installed
|
# If the command is not found, the package isn't installed
|
||||||
(( $+commands[fzf] )) || return 1
|
(( $+commands[fzf] )) || return 1
|
||||||
|
|
||||||
# The fzf-zsh-completion package installs the auto-completion in
|
# The fzf-zsh-completion package installs the auto-completion in
|
||||||
local completions="/usr/share/zsh/site-functions/_fzf"
|
local completions="/usr/share/zsh/site-functions/_fzf"
|
||||||
# The fzf-zsh-completion package installs the key-bindings file in
|
# The fzf-zsh-completion package installs the key-bindings file in
|
||||||
local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"
|
local key_bindings="/etc/zsh_completion.d/fzf-key-bindings"
|
||||||
|
|
||||||
# If these are not found: (1) maybe we're not on OpenSUSE, or
|
# If these are not found: (1) maybe we're not on OpenSUSE, or
|
||||||
# (2) maybe the fzf-zsh-completion package isn't installed.
|
# (2) maybe the fzf-zsh-completion package isn't installed.
|
||||||
if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
|
if [[ ! -f "$completions" || ! -f "$key_bindings" ]]; then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Auto-completion
|
# Auto-completion
|
||||||
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
||||||
source "$completions" 2>/dev/null
|
source "$completions" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Key bindings
|
# Key bindings
|
||||||
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
|
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
|
||||||
source "$key_bindings" 2>/dev/null
|
source "$key_bindings" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup_using_openbsd_package() {
|
function fzf_setup_using_openbsd() {
|
||||||
# openBSD installs fzf in /usr/local/bin/fzf
|
# openBSD installs fzf in /usr/local/bin/fzf
|
||||||
if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
|
if [[ "$OSTYPE" != openbsd* ]] || (( ! $+commands[fzf] )); then
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# The fzf package installs the auto-completion in
|
# The fzf package installs the auto-completion in
|
||||||
local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
|
local completions="/usr/local/share/zsh/site-functions/_fzf_completion"
|
||||||
# The fzf package installs the key-bindings file in
|
# The fzf package installs the key-bindings file in
|
||||||
local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"
|
local key_bindings="/usr/local/share/zsh/site-functions/_fzf_key_bindings"
|
||||||
|
|
||||||
# Auto-completion
|
# Auto-completion
|
||||||
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
if [[ -o interactive && "$DISABLE_FZF_AUTO_COMPLETION" != "true" ]]; then
|
||||||
source "$completions" 2>/dev/null
|
source "$completions" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
# Key bindings
|
# Key bindings
|
||||||
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
|
if [[ "$DISABLE_FZF_KEY_BINDINGS" != "true" ]]; then
|
||||||
source "$key_bindings" 2>/dev/null
|
source "$key_bindings" 2>/dev/null
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
}
|
|
||||||
|
|
||||||
function indicate_error() {
|
|
||||||
cat >&2 <<EOF
|
|
||||||
[oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
|
|
||||||
Please add \`export FZF_BASE=/path/to/fzf/install/dir\` to your .zshrc
|
|
||||||
EOF
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Indicate to user that fzf installation not found if nothing worked
|
# Indicate to user that fzf installation not found if nothing worked
|
||||||
setup_using_openbsd_package \
|
function fzf_setup_error() {
|
||||||
|| setup_using_debian_package \
|
cat >&2 <<'EOF'
|
||||||
|| setup_using_opensuse_package \
|
[oh-my-zsh] fzf plugin: Cannot find fzf installation directory.
|
||||||
|| setup_using_base_dir \
|
Please add `export FZF_BASE=/path/to/fzf/install/dir` to your .zshrc
|
||||||
|| indicate_error
|
EOF
|
||||||
|
}
|
||||||
|
|
||||||
unset -f setup_using_opensuse_package setup_using_debian_package setup_using_base_dir indicate_error
|
fzf_setup_using_openbsd \
|
||||||
|
|| fzf_setup_using_debian \
|
||||||
|
|| fzf_setup_using_opensuse \
|
||||||
|
|| fzf_setup_using_base_dir \
|
||||||
|
|| fzf_setup_error
|
||||||
|
|
||||||
|
unset -f -m 'fzf_setup_*'
|
||||||
|
|
||||||
if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
|
if [[ -z "$FZF_DEFAULT_COMMAND" ]]; then
|
||||||
if (( $+commands[rg] )); then
|
if (( $+commands[rg] )); then
|
||||||
export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
|
export FZF_DEFAULT_COMMAND='rg --files --hidden --glob "!.git/*"'
|
||||||
elif (( $+commands[fd] )); then
|
elif (( $+commands[fd] )); then
|
||||||
export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
|
export FZF_DEFAULT_COMMAND='fd --type f --hidden --exclude .git'
|
||||||
elif (( $+commands[ag] )); then
|
elif (( $+commands[ag] )); then
|
||||||
export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
|
export FZF_DEFAULT_COMMAND='ag -l --hidden -g "" --ignore .git'
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
|
|
Loading…
Reference in a new issue