{% if not SHIMS_ONLY -%} # This function is used to run the omni command, and then operate on # the requested shell changes from the command (changing current # working directory, environment, etc.); this is why we require using # a shell function for this, instead of simply calling the omni # command from the path function omni() { # Prepare the environment for omni export OMNI_UUID=$(command -v uuidgen >/dev/null && uuidgen || {{ OMNI_BIN }} hook uuid) local tmpdir=${TMPDIR:-/tmp} OMNI_FILE_PREFIX="omni_${OMNI_UUID}" export OMNI_CMD_FILE="${tmpdir}/${OMNI_FILE_PREFIX}.cmd" export OMNI_SHELL=zsh # Run the command {{ OMNI_BIN }} "$@" EXIT_CODE=$? # Check if OMNI_CMD_FILE exists, and if it does, run the commands # inside without a subshell, so that the commands can modify the # environment of the current shell, and then delete the file if [[ -f $OMNI_CMD_FILE ]] && [[ "$EXIT_CODE" == "0" ]]; then while IFS= read -r cmd; do eval $cmd EXIT_CODE=$? if [[ "$EXIT_CODE" != "0" ]]; then echo -e "\033[96momni:\033[0m \033[31mcommand failed:\033[0m $cmd \033[90m(exit: $EXIT_CODE)\033[0m" break fi done < $OMNI_CMD_FILE fi # Delete the files, we do that with '&' so there's no delay to return # to the prompt, and within a subshell so that monitor mode (set +m) # is disabled for that command, allowing to hide the '[x] Done' message (find "${tmpdir}/" -name "${OMNI_FILE_PREFIX}*" -exec rm {} \; >/dev/null 2>&1 &) # Unset the environment variables unset OMNI_SHELL unset OMNI_UUID unset OMNI_FILE_PREFIX unset OMNI_CMD_FILE # Return the exit code of the command return $EXIT_CODE } {% if OMNI_ALIASES or OMNI_COMMAND_ALIASES -%} # Setup aliases for omni {% for alias in OMNI_ALIASES -%} alias {{ alias }}="omni" {% endfor -%} {% for alias in OMNI_COMMAND_ALIASES -%} alias {{ alias.alias }}={{ alias.full_command }} {% endfor %} {% endif -%} # Setup autocompletion for omni _omni_complete_zsh() { {% if OMNI_COMMAND_ALIASES -%} # Handle command aliases # Get the first word to check if it is a command alias local alias_prefix local alias_skip case "${words[1]}" in {% for alias in OMNI_COMMAND_ALIASES -%} "{{ alias.alias }}") alias_prefix={{ alias.command }} alias_skip={{ alias.command_size }} ;; {% endfor -%} *) # Not a command alias ;; esac {% endif -%} autocomplete="{{ OMNI_BIN }} --complete" {%- if OMNI_COMMAND_ALIASES %} [[ -n "${alias_prefix}" ]] && autocomplete="${autocomplete} ${alias_prefix}" {%- endif %} {% raw -%} [[ ${#words[@]} -gt 1 ]] && autocomplete="${autocomplete} ${words[2,CURRENT]}" {%- endraw %} local opts=$(\ eval "COMP_CWORD=$((CURRENT - 1{% if OMNI_COMMAND_ALIASES %} + alias_skip{% endif %})) \ COMP_TYPE=$compstate[quote] \ $autocomplete") local reply=($(echo $opts)) {% raw -%} if [[ "${#reply[@]}" -lt 1 ]]; then return fi # For each completion, override them by adding a space # at the end UNLESS they end with a slash (dir completion) for i in "${#reply[@]}"; do if [[ "${reply[$i]}" != */ ]]; then reply[$i]="${reply[$i]} " fi done compadd -Q -S '' -- $reply {%- endraw %} } if ! type compdef >/dev/null 2>&1; then autoload -U compinit && compinit fi compdef _omni_complete_zsh omni {% if OMNI_ALIASES or OMNI_COMMAND_ALIASES -%} {% for alias in OMNI_ALIASES -%} compdef _omni_complete_zsh {{ alias }} {% endfor -%} {% for alias in OMNI_COMMAND_ALIASES -%} compdef _omni_complete_zsh {{ alias.alias }} {% endfor -%} {% endif %} # Prepare omni's hook __omni_hook() { local ppid=$$ eval "$(OMNI_SHELL_PPID="${ppid}" "{{ OMNI_BIN }}" hook env{% if KEEP_SHIMS %} --keep-shims{% endif %} "${@}")" } # Inject omni's hooks into the prompt command __omni_hook_precmd() { __omni_hook zsh } if [[ ! ${precmd_functions[(r)__omni_hook_precmd]} ]]; then precmd_functions+=("__omni_hook_precmd"); fi {% endif -%} # Add the shims directory to the PATH, so that the dynamic # environment can be used in non-interactive shells. # This will automatically be removed from the PATH when the # dynamic environment is loaded, allowing to favor it over # the shims if [[ ":$PATH:" != *":{{ OMNI_SHIMS }}:"* ]]; then export PATH="{{ OMNI_SHIMS }}:$PATH" fi