Skip to content

Instantly share code, notes, and snippets.

@mattmc3
Last active April 20, 2024 10:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattmc3/7937adb6416877fdee2622bbc247325e to your computer and use it in GitHub Desktop.
Save mattmc3/7937adb6416877fdee2622bbc247325e to your computer and use it in GitHub Desktop.
Zsh Unplugged Enhanced
#!/usr/bin/env zsh
##? plugin-load - load plugins without a fancy plugin manager
##?
##? usage: plugin-load [-h|--help]
##? plugin-load [-n|--no-source] [-d|--defer] [-f|--fpath] [-p|--path]
##? [-u|--use-dir <plugin-subdir>] [<repo...>]
function plugin-load {
local use_dir flag_no_source flag_fpath flag_path flag_defer repos=()
opterr() { echo >&2 "Unknown option '$1'" }
while (( $# )); do
case $1 in
--) shift; repos+=("$@"); break ;;
-h|--help) fnhelp plugin-load && return ;;
-n|--no-source) flag_no_source=1; ;;
-d|--defer) flag_defer=1; ;;
-f|--fpath) flag_fpath=1 ;;
-p|--path) flag_path=1 ;;
-u|--use-dir) shift; use_dir=$1 ;;
-u=*|--use-dir=*) use_dir="${1#*=}" ;;
-*) opterr $1 && return 2 ;;
*) repos+=("$@"); break ;;
esac
shift
done
local repo plugin_name plugin_dir initfile initfiles
ZPLUGINDIR=${ZPLUGINDIR:-${ZDOTDIR:-$HOME/.config/zsh}/plugins}
for repo in $repos; do
plugin_name=${repo:t}
plugin_dir=${use_dir:-$ZPLUGINDIR}/$plugin_name
initfile=$plugin_dir/$plugin_name.plugin.zsh
if [[ ! -d $plugin_dir ]] && [[ "$repo" = */* ]]; then
echo "Cloning $repo..."
git clone --quiet --depth 1 --recursive --shallow-submodules https://github.com/$repo $plugin_dir
if [[ ! -e $initfile ]]; then
initfiles=($plugin_dir/*.plugin.{z,}sh(N) $plugin_dir/*.{z,}sh{-theme,}(N))
[[ ${#initfiles[@]} -gt 0 ]] && ln -sf "${initfiles[1]}" "$initfile"
fi
fi
[[ -n "$flag_path" ]] && export PATH="$plugin_dir:$PATH"
[[ -n "$flag_fpath" ]] && fpath+=( $plugin_dir )
[[ -n "$flag_no_source" ]] && continue
if [[ -n "$flag_defer" ]]; then
zsh-defer . $initfile
elif [[ -z "$flag_no_source" ]]; then
. $initfile
fi
done
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment