Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 74 additions & 67 deletions config/zsh/plugins/plugin-installer.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,89 @@
# This is useful because I've ended up with a slow shell startup while
# using more complete solutions like zplug.
zsh-plugin-installer() {
local REPO=
local DST=
local EXEC_AFTER=
local EXEC_ALWAYS=
zmodload zsh/zutil || return

for arg in "$@"; do
case ${arg} in
repo=*)
REPO="${arg#*=}"
shift
;;
dst=*)
DST="${arg#*=}"
shift
;;
exec-after=*)
EXEC_AFTER="${arg#*=}"
shift
;;
exec-always=*)
EXEC_ALWAYS="${arg#*=}"
shift
;;
*)
echo "The option ${arg} is unknown."
exit 1
;;
esac
done
C_RED='\e[31m'
C_GREEN='\e[32m'
C_YELLOW='\e[33m'
C_BLUE='\e[34m'
C_NO='\e[0m' # No Color

local PKG_NAME=$(basename $REPO)
STARTED_AT=$(date +%s.%N)
local help verbose repo exec_after exec_always
# -D pulls parsed flags out of $@
# -F says fail if we find a flag that wasn't defined
# -K allows us to set default values without zparseopts overwriting them
# Remember that the first dash is automatically handled, so long options are -opt, not --opt
# Add a trailing colon (:) to an option spec if it requires an argument
zparseopts -D -F -K -- \
{h,-help}=help \
{v,-verbose}=verbose \
{r,-repo}:=repo \
{a,-exec-after}:=exec_after \
{w,-exec-always}:=exec_always

if [[ -z "${DST}" ]]; then
DST="${XDG_CACHE_HOME}/zsh-plugin-installer/${PKG_NAME}"
fi
if (($#help)); then
print -rC1 -- \
"$0 [-h|--help]" \
"$0 [-v|--verbose]" \
"$0 [-r|--repo=<arg>]" \
"$0 [-a|--exec-after=<arg>]" \
"$0 [-w|--exec-always=<arg>]"
return
fi

if [[ ! -d "${DST}" ]]; then
# ${DST} is not a directory
echo "Installing $PKG_NAME"
git clone --depth=1 --recurse-submodules --quiet "${REPO}" "${DST}"
local REPO=$repo[-1]
local DST=
local EXEC_AFTER=$exec_after[-1]
local EXEC_ALWAYS=$exec_always[-1]

if [[ ! -z "${EXEC_AFTER}" ]]; then
echo " -> Executing $EXEC_AFTER"
pushd -q "${DST}"
(($#verbose)) && echo -e "${C_YELLOW}REPO = '$REPO' | DST = '$DST' | EXEC_AFTER = '$EXEC_AFTER' | EXEC_ALWAYS = '$EXEC_ALWAYS' ${C_NO}"

eval "${EXEC_AFTER}"
if [[ $? -ne 0 ]]; then
echo "Error on exec after!"
fi
local PKG_NAME=$(basename $REPO)
STARTED_AT=$(date +%s.%N)

popd -q
fi
fi
if [[ -z "${DST}" ]]; then
DST="${XDG_CACHE_HOME}/zsh-plugin-installer/${PKG_NAME}"
fi

if [[ -n "${EXEC_ALWAYS}" ]]; then
# User has defined "EXEC_ALWAYS"
pushd -q "${DST}"
if [[ ! -d "${DST}" ]]; then
echo -e "${C_BLUE}Installing $PKG_NAME${C_NO} in $DST"
git clone --depth=1 --recurse-submodules --quiet "${REPO}" "${DST}"

if [[ "${EXEC_ALWAYS}" == "plug" ]]; then
source "${PKG_NAME}.plugin.zsh"
else
eval "${EXEC_ALWAYS}"
if [[ $? -ne 0 ]]; then
echo "Error on exec always!" >&2
fi
fi
if [[ ! -z "${EXEC_AFTER}" ]]; then
echo -e " ${C_BLUE}-> Executing${C_NO} $EXEC_AFTER"
pushd -q "${DST}"

popd -q
fi
eval "${EXEC_AFTER}"
if [[ $? -ne 0 ]]; then
echo -e "${C_RED}Error on exec after!${C_NO}"
fi

LOAD_TIME=$(($(date +%s.%N) - STARTED_AT))
if [[ $LOAD_TIME -gt 1 ]]; then
echo >&2 "[warning] startup time for $REPO was $LOAD_TIME seconds."
read "?Press [ENTER] to continue"
#zprof # show the profilling results from zprof module, if loaded
#read "?Press [ENTER] to continue"
fi
popd -q
fi
fi

if [[ -n "${EXEC_ALWAYS}" ]]; then
(($#verbose)) && echo -e "${C_YELLOW}Executing${C_NO} '$EXEC_ALWAYS' in $DST"
pushd -q "${DST}"

if [[ "${EXEC_ALWAYS}" == "plug" ]]; then
source "${PKG_NAME}.plugin.zsh"
else
eval "${EXEC_ALWAYS}"
if [[ $? -ne 0 ]]; then
echo -e "${C_RED}Error on exec always!${C_NO}" >&2
fi
fi

popd -q
fi

LOAD_TIME=$(($(date +%s.%N) - STARTED_AT))
if [[ $LOAD_TIME -gt 1 ]]; then
echo >&2 "${C_RED}[warning] startup time for $REPO was $LOAD_TIME seconds.${C_NO}"
read "?Press [ENTER] to continue"
#zprof # show the profilling results from zprof module, if loaded
#read "?Press [ENTER] to continue"
fi
}
18 changes: 9 additions & 9 deletions config/zsh/plugins/plugins-cfg.zsh
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ UPDATE_ZSH_DAYS=7
# I don't automatically want url quotation because it is buggy, i.e. that ignores ''
DISABLE_MAGIC_FUNCTIONS=true
plugins=(command-not-found)
zsh-plugin-installer repo="https://github.com/robbyrussell/oh-my-zsh" exec-always="source oh-my-zsh.sh"
zsh-plugin-installer --repo "https://github.com/robbyrussell/oh-my-zsh" --exec-always "source oh-my-zsh.sh"

#|
#| Themes
Expand All @@ -35,24 +35,24 @@ zsh-plugin-installer repo="https://github.com/robbyrussell/oh-my-zsh" exec-alway

DOTFILES_THEMES="${0:a:h}/../themes"

#zsh-plugin-installer repo="https://github.com/martinrotter/powerless.git" exec-always="source powerless.zsh false && source utilities.zsh true"
#zsh-plugin-installer --repo "https://github.com/martinrotter/powerless.git" --exec-always "source powerless.zsh false && source utilities.zsh true"

#zsh-plugin-installer repo="https://github.com/eendroroy/alien-minimal" exec-always="source $DOTFILES_THEMES/alien-minimal.zsh && source alien-minimal.zsh"
#zsh-plugin-installer --repo "https://github.com/eendroroy/alien-minimal" --exec-always "source $DOTFILES_THEMES/alien-minimal.zsh && source alien-minimal.zsh"

# Enable Powerlevel10k instant prompt. Should stay close to the top of ~/.config/zsh/.zshrc.
# Initialization code that may require console input (password prompts, [y/n]
# confirmations, etc.) must go above this block; everything else may go below.
if [[ -r "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh" ]]; then
source "${XDG_CACHE_HOME:-$HOME/.cache}/p10k-instant-prompt-${(%):-%n}.zsh"
fi
zsh-plugin-installer repo="https://github.com/romkatv/powerlevel10k" exec-always="source powerlevel10k.zsh-theme && source $XDG_CONFIG_HOME/zsh/.p10k.zsh"
zsh-plugin-installer --repo "https://github.com/romkatv/powerlevel10k" --exec-always "source powerlevel10k.zsh-theme && source $XDG_CONFIG_HOME/zsh/.p10k.zsh"

# starship has less options than powerlevel10k
# zsh-plugin-installer repo="https://github.com/starship/starship" exec-after="./install/install.sh --yes --bin-dir=/usr/local/bin" exec-always='eval "$(starship init zsh)"'
#zsh-plugin-installer --repo "https://github.com/starship/starship" --exec-after "./install/install.sh --yes --bin-dir=/usr/local/bin" exec-always='eval "$(starship init zsh)"'

# oh-my-posh is difficult to customize and it is focused on Windows
# nice themes: agnoster, mojada, nu4a, powerline, unicorn
# zsh-plugin-installer repo="https://github.com/JanDeDobbeleer/oh-my-posh" exec-after="bash ./website/static/install.sh -d /usr/local/bin" exec-always='eval "$(oh-my-posh init zsh --config $XDG_CACHE_HOME/oh-my-posh/themes/agnoster.omp.json)"'
#zsh-plugin-installer --repo "https://github.com/JanDeDobbeleer/oh-my-posh" --exec-after "bash ./website/static/install.sh -d /usr/local/bin" exec-always='eval "$(oh-my-posh init zsh --config $XDG_CACHE_HOME/oh-my-posh/themes/agnoster.omp.json)"'

#|
#| Tools
Expand All @@ -64,9 +64,9 @@ if [[ ! -e "/usr/local/bin/git-extras" || ! -e "$XDG_CACHE_HOME/git-extras-compl
fi
source $XDG_CACHE_HOME/git-extras-completion.zsh

zsh-plugin-installer repo="https://github.com/paulirish/git-open" exec-after="cp -f git-open /usr/local/bin/git-open"
zsh-plugin-installer --repo "https://github.com/paulirish/git-open" --exec-after "sudo cp -f git-open /usr/local/bin/git-open"

zsh-plugin-installer repo="https://github.com/junegunn/fzf" exec-after="./install --bin && sudo mv -f ./bin/fzf /usr/local/bin/fzf" exec-always="source shell/completion.zsh && source shell/key-bindings.zsh"
zsh-plugin-installer --repo "https://github.com/junegunn/fzf" --exec-after "./install --bin && sudo mv -f ./bin/fzf /usr/local/bin/fzf" --exec-always "source shell/completion.zsh && source shell/key-bindings.zsh"

# Syntax Highlighting
# https://github.com/zsh-users/zsh-syntax-highlighting/blob/master/docs/highlighters.md
Expand All @@ -79,4 +79,4 @@ ZSH_HIGHLIGHT_STYLES[path]='fg=green'
# Commands starting with `rm -rf` in red:
ZSH_HIGHLIGHT_PATTERNS+=('rm -[rR]f *' 'fg=white,bold,bg=red')
# Must be sourced at the end of the .zshrc
zsh-plugin-installer repo="https://github.com/zsh-users/zsh-syntax-highlighting" exec-always="plug"
zsh-plugin-installer --repo "https://github.com/zsh-users/zsh-syntax-highlighting" --exec-always "plug"
Loading