dotfiles/.config/zsh/plugins.zsh

100 lines
3.3 KiB
Bash

# Generate plugin lists and update or install them
function()
{
setopt LOCAL_OPTIONS EXTENDED_GLOB
unset ZSH_PLUGIN_DIST ZSH_PLUGIN_SOURCE
local -a _my_plugins=(
zsh-users/zsh-completions
mafredri/zsh-async
# Must come before autosuggestions and syntax-highlighting
Aloxaf/fzf-tab
zsh-users/zsh-autosuggestions
zsh-users/zsh-syntax-highlighting
# Must come after zsh-syntax-highlighting
zsh-users/zsh-history-substring-search
)
if ! command -v fzf > /dev/null; then
# Don't load these plugins if fzf was not found
_my_plugins=(${_my_plugins:#Aloxaf/fzf-tab})
fi
if [[ "$HOST" == "steuerbeamter" ]]; then
# Don't load these plugins on slow computers
_my_plugins=(${_my_plugins:#zsh-users/zsh-autosuggestions})
fi
local -a _my_plugins_dist
# Don't fetch plugins that are installed by the package manager
if [[ -f /etc/gentoo-release ]]; then
if [[ -d /var/db/pkg/app-shells/zsh-completions-*(#qN) ]]; then
_my_plugins=(${_my_plugins:#zsh-users/zsh-completions})
fi
function _plugin_installed()
{
local catpkg=${1}
local uri=${2}
local filename=${3}
if [[ -d /var/db/pkg/${catpkg}-*(#qN) ]]; then
_my_plugins=(${_my_plugins:#${uri}})
_my_plugins_dist+=(/usr/share/zsh/site-functions/${filename})
fi
}
_plugin_installed app-shells/zsh-async \
mafredri/zsh-async \
async.zsh
_plugin_installed app-shells/fzf-tab \
Aloxaf/fzf-tab \
fzf-tab.zsh
_plugin_installed app-shells/zsh-autosuggestions \
zsh-users/zsh-autosuggestions \
zsh-autosuggestions.zsh
_plugin_installed app-shells/zsh-syntax-highlighting \
zsh-users/zsh-syntax-highlighting \
zsh-syntax-highlighting.zsh
_plugin_installed app-shells/zsh-history-substring-search \
zsh-users/zsh-history-substring-search \
zsh-history-substring-search.zsh
unfunction _plugin_installed
fi
if [[ -n ${_my_plugins_dist} ]]; then
export ZSH_PLUGIN_DIST="${_my_plugins_dist}" # Arrays can not be exported
fi
[[ -z ${_my_plugins} ]] && return
# If there are plugins not handled by the package manager, use antibody
if command -v antibody > /dev/null; then
zmodload zsh/stat
zmodload zsh/datetime
local plugin_cache_dir="$(antibody home)"
export ZSH_PLUGIN_SOURCE="${plugin_cache_dir}/antibody-plugins.zsh"
if [[ -f "${ZSH_PLUGIN_SOURCE}" ]] \
&& [[ $(zstat +size "${ZSH_PLUGIN_SOURCE}") -ne 0 ]]; then
local _plugins_modified=$(zstat +mtime "${ZSH_PLUGIN_SOURCE}")
local _now=$(strftime '%s')
# Update plugins every 10 days
if [[ $(( ${_now} - ${_plugins_modified} - 60 * 60 * 24 * 10)) -gt 0 ]]; then
print "\e[3;95mUpdating plugins…\e[0m"
antibody update --parallelism=$(nproc --ignore=1) \
&& touch "${ZSH_PLUGIN_SOURCE}"
fi
else
print "\e[3;95mInstalling plugins…\e[0m"
mkdir -p "${plugin_cache_dir}"
antibody bundle --parallelism=$(nproc --ignore=1) \
<<<$(print -l ${_my_plugins}) > "${ZSH_PLUGIN_SOURCE}"
fi
else
print -u 2 "\e[7;95mantibody not found.\e[0m"
fi
}