-
-
Save hedzr/5c7bc141c14b075e2bed9175e4aac948 to your computer and use it in GitHub Desktop.
lazy-loader.sh - hook zsh unknown command handler and source in a script file and try re-invoke the function/command.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# for zsh | |
lazy_loaded=() | |
function command_not_found_handler() { | |
local f dir processed=0 cmd=$1 && shift | |
for dir in $HOME/.local/bin $HOME/bin /opt/bin; do | |
local dx="$dir/.zsh/lazy" | |
dbg "dx: $dx" | |
if [ -d $dx ]; then | |
f="$dx/$cmd.sh" | |
if not_in_array $f $kazy_loaded; then | |
if [ -f "$f" ]; then | |
source $f | |
dbg "yes: $f" | |
lazy_loaded+=($f) | |
processed=1 | |
eval $cmd $@ | |
fi | |
fi | |
fi | |
done | |
if (($processed)); then | |
return 0 | |
else | |
err "COMMAND NOT FOUND: You tried to run '$cmd' with args '$@'" | |
return 127 | |
fi | |
} | |
# for bash | |
command_not_found_handle() { | |
command_not_found_handler "$@" | |
} | |
not_in_array() { | |
local find="$1" | |
shift | |
local arr=("$@") | |
[[ ! ${arr[*]} =~ (^|[[:space:]])"$find"($|[[:space:]]) ]] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment