Zsh: Move plugin management into own file

Also Don't install plugins that are already installed by the package
manager.
This commit is contained in:
tastytea 2022-04-05 15:28:54 +02:00
parent 6d3b0609a2
commit 98a36aa78c
Signed by: tastytea
SSH Key Fingerprint: SHA256:FBkvrOlhq5use1XEttyUGT4bUTDVA1ar9SgIc9P03cM
3 changed files with 76 additions and 44 deletions

View File

@ -40,49 +40,8 @@ export EDITOR="${VISUAL}"
############################# Plugins ##########################################
zmodload zsh/stat
zmodload zsh/datetime
local -a _my_plugins=(
mafredri/zsh-async
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
if command -v antibody > /dev/null; then
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
# Generate plugin lists and update or install them
source "${ZDOTDIR}"/plugins.zsh
############################# Variables ########################################

View File

@ -52,7 +52,10 @@ add-zsh-hook zshaddhistory _my_import_history_zshaddhistory
ZSH_HIGHLIGHT_HIGHLIGHTERS=(main brackets)
ZSH_AUTOSUGGEST_HIGHLIGHT_STYLE="fg=236"
# Set up in .zprofile.
for plugin in "${(@s/ /)ZSH_PLUGIN_DIST}"; do
[[ -n "${plugin}" ]] && source "${plugin}"
done
unset plugin
source "${ZSH_PLUGIN_SOURCE}"
# Fuzzy finder, <https://github.com/junegunn/fzf>.

70
.config/zsh/plugins.zsh Normal file
View File

@ -0,0 +1,70 @@
# Generate plugin lists and update or install them
function()
{
setopt LOCAL_OPTIONS EXTENDED_GLOB
local -a _my_plugins=(
mafredri/zsh-async
zsh-users/zsh-completions
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 load 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
if [[ -d /var/db/pkg/app-shells/zsh-autosuggestions-*(#qN) ]]; then
_my_plugins=(${_my_plugins:#zsh-users/zsh-autosuggestions})
_my_plugins_dist+=(/usr/share/zsh/site-functions/zsh-autosuggestions.zsh)
fi
if [[ -d /var/db/pkg/app-shells/zsh-syntax-highlighting-*(#qN) ]]; then
_my_plugins=(${_my_plugins:#zsh-users/zsh-syntax-highlighting})
_my_plugins_dist+=(/usr/share/zsh/site-functions/zsh-syntax-highlighting.zsh)
fi
fi
export ZSH_PLUGIN_DIST="${_my_plugins_dist}" # Arrays can not be exported
zmodload zsh/stat
zmodload zsh/datetime
if command -v antibody > /dev/null; then
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
}