├── README.md ├── zsh-lazyload.plugin.zsh └── zsh-lazyload.zsh /README.md: -------------------------------------------------------------------------------- 1 | # zsh-lazyload [![starline](https://starlines.qoo.monster/assets/qoomon/zsh-lazyload)](https://github.com/qoomon/starlines) 2 | 3 | zsh plugin for lazy load commands and speed up start up time of zsh 4 | 5 | 6 | ## Usage 7 | `lazyload command_name [command_name...] -- load_command` 8 | 9 | #### Examples 10 | ##### `nvm` 11 | - `lazyload nvm -- 'source ~/.nvm/nvm.sh'` 12 | 13 | ## Install 14 | 15 | ### [zgem](https://github.com/qoomon/zgem) 16 | `zgem bundle 'https://github.com/qoomon/zsh-lazyload.git' from:'git' use:'zsh-lazyload.zsh'` 17 | ### [zplug](https://github.com/zdharma/zplugin) 18 | `zplug qoomon/zsh-lazyload` 19 | ### [zgen](https://github.com/tarjoilija/zgen) 20 | ``` 21 | zgen load qoomon/zsh-lazyload 22 | zgen save 23 | ``` 24 | ### [Antigen](https://github.com/zsh-users/antigen) 25 | ``` 26 | antigen bundle qoomon/zsh-lazyload 27 | antigen apply 28 | ``` 29 | ### [Oh My ZSH! custom plugin](http://ohmyz.sh/) 30 | ``` 31 | git clone https://github.com/qoomon/zsh-lazyload $ZSH_CUSTOM/plugins/zsh-lazyload 32 | plugins+=(zsh-lazyload) 33 | ``` 34 | ### [zplug](https://github.com/zplug/zplug) 35 | `zplug "qoomon/zsh-lazyload"` 36 | ### manually 37 | ``` 38 | git clone https://github.com/qoomon/zsh-lazyload.git 39 | source zsh-lazyload/zsh-lazyload.zsh 40 | ``` 41 | 42 | 43 | -------------------------------------------------------------------------------- /zsh-lazyload.plugin.zsh: -------------------------------------------------------------------------------- 1 | 0=${(%):-%N} 2 | source ${0:A:h}/zsh-lazyload.zsh 3 | -------------------------------------------------------------------------------- /zsh-lazyload.zsh: -------------------------------------------------------------------------------- 1 | function lazyload { 2 | local seperator='--' 3 | local seperator_index=${@[(ie)$seperator]} 4 | local cmd_list=(${@:1:(($seperator_index - 1))}); 5 | local load_cmd=${@[(($seperator_index + 1))]}; 6 | 7 | if [[ ! $load_cmd ]]; then 8 | >&2 echo "[ERROR] lazyload: No load command defined" 9 | >&2 echo " $@" 10 | return 1 11 | fi 12 | 13 | # check if lazyload was called by placeholder function 14 | if (( ${cmd_list[(Ie)${funcstack[2]}]} )); then 15 | unfunction $cmd_list 16 | eval "$load_cmd" 17 | else 18 | # create placeholder function for each command 19 | local cmd 20 | for cmd in $cmd_list; eval "function $cmd { lazyload $cmd_list $seperator ${(qqqq)load_cmd} && $cmd \"\$@\" }" 21 | fi 22 | } 23 | 24 | ### Notes ### 25 | # ${funcstack[2]} resolves to the caller function name 26 | # ${ARRAY[(Ie)$value]} resolves to the index of the VALUE within the ARRAY (I - Index; e - exact match, no pattern match) 27 | # (($NUMBER)) resolves to false for NUMBER 0, else true 28 | # ${(qqqq)VAR} resolves to quoted value in the format of $'...' 29 | --------------------------------------------------------------------------------