{% 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=bash # 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 if [[ "${BASH_VERSINFO[0]}" -lt 4 ]]; then echo -e "\033[96momni:\033[0m \033[91mBash version 4 or higher is required for autocompletion (current: ${BASH_VERSION})\033[0m" else _omni_complete_bash() { local cur prev opts {% 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 "${COMP_WORDS[0]}" 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 -%} words=() {%- if OMNI_COMMAND_ALIASES %} [[ -n "${alias_prefix}" ]] && words+=("${alias_prefix}") {%- endif %} {% raw -%} [[ ${#COMP_WORDS[@]} -gt 1 ]] && words+=("${COMP_WORDS[@]:1:$COMP_CWORD}") {%- endraw %} # Handle completion COMPREPLY=() cur="${COMP_WORDS[COMP_CWORD]}" prev="${COMP_WORDS[COMP_CWORD-1]}" opts=$(\ COMP_CWORD=$((COMP_CWORD + alias_skip)) \ COMP_TYPE=$COMP_TYPE \ {{ OMNI_BIN }} --complete ${words[@]}) while read -r opt; do if [[ "${opt}" != "${cur}"* ]]; then continue fi local val="${opt}" if [[ "${opt}" == '~/'* ]] || [[ "${opt}" == '~' ]]; then val="${opt:0:1}$(printf "%q" "${opt:1}")" fi # For each completion, add a space at the end UNLESS # they end with a slash (dir completion) if [[ "${val}" != */ ]]; then val="${val} " fi COMPREPLY+=("${val}") done <<< "${opts}" # Do not add a space after the completion, as we add it # ourselves unless the completion is a directory compopt -o nospace return 0 } complete -F _omni_complete_bash omni {% if OMNI_ALIASES or OMNI_COMMAND_ALIASES -%} {% for alias in OMNI_ALIASES -%} complete -F _omni_complete_bash {{ alias }} {% endfor -%} {% for alias in OMNI_COMMAND_ALIASES -%} complete -F _omni_complete_bash {{ alias.alias }} {% endfor -%} {% endif -%} fi # 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 [[ ! "${PROMPT_COMMAND}" == *" __omni_hook "* ]] && { PROMPT_COMMAND="{ [[ \$- =~ x ]] && { \set +x; __omni_hook bash 2>&3; \set -x; } || { __omni_hook bash 2>&3; } } 4>&2 2>/dev/null 3>&4; ${PROMPT_COMMAND}" } {% 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