From 79f8e8225b7cc62e9b66efe69863646002e328ac Mon Sep 17 00:00:00 2001 From: Miquel Sabaté Solà Date: Wed, 10 Jul 2024 21:36:02 +0200 Subject: bash: add guards on programs that may not exist MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Namely, fzf and mise are quite noisy when they are not available. Signed-off-by: Miquel Sabaté Solà --- .bashrc | 90 ++++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 47 insertions(+), 43 deletions(-) diff --git a/.bashrc b/.bashrc index 468bf5f..0975c45 100644 --- a/.bashrc +++ b/.bashrc @@ -66,7 +66,9 @@ if command -v bat &> /dev/null; then fi # Set up fzf key bindings and fuzzy completion -eval "$(fzf --bash)" +if command -v fzf &> /dev/null; then + eval "$(fzf --bash)" +fi ## # Editors & terminals. @@ -189,54 +191,56 @@ fi ## # $ mise activate bash >> .bashrc -export MISE_SHELL=bash -export __MISE_ORIG_PATH="$PATH" +if command -v mise &> /dev/null; then + export MISE_SHELL=bash + export __MISE_ORIG_PATH="$PATH" -mise() { - local command - command="${1:-}" - if [ "$#" = 0 ]; then - command mise - return - fi - shift - - case "$command" in - deactivate|s|shell) - # if argv doesn't contains -h,--help - if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then - eval "$(command mise "$command" "$@")" - return $? + mise() { + local command + command="${1:-}" + if [ "$#" = 0 ]; then + command mise + return fi - ;; - esac - command mise "$command" "$@" -} - -_mise_hook() { - local previous_exit_status=$?; - eval "$(mise hook-env -s bash)"; - return $previous_exit_status; -}; -if [[ ";${PROMPT_COMMAND:-};" != *";_mise_hook;"* ]]; then - PROMPT_COMMAND="_mise_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}" -fi -if [ -z "${_mise_cmd_not_found:-}" ]; then + shift + + case "$command" in + deactivate|s|shell) + # if argv doesn't contains -h,--help + if [[ ! " $@ " =~ " --help " ]] && [[ ! " $@ " =~ " -h " ]]; then + eval "$(command mise "$command" "$@")" + return $? + fi + ;; + esac + command mise "$command" "$@" + } + + _mise_hook() { + local previous_exit_status=$?; + eval "$(mise hook-env -s bash)"; + return $previous_exit_status; + }; + if [[ ";${PROMPT_COMMAND:-};" != *";_mise_hook;"* ]]; then + PROMPT_COMMAND="_mise_hook${PROMPT_COMMAND:+;$PROMPT_COMMAND}" + fi + if [ -z "${_mise_cmd_not_found:-}" ]; then _mise_cmd_not_found=1 if [ -n "$(declare -f command_not_found_handle)" ]; then - _mise_cmd_not_found_handle=$(declare -f command_not_found_handle) - eval "${_mise_cmd_not_found_handle/command_not_found_handle/_command_not_found_handle}" + _mise_cmd_not_found_handle=$(declare -f command_not_found_handle) + eval "${_mise_cmd_not_found_handle/command_not_found_handle/_command_not_found_handle}" fi command_not_found_handle() { - if mise hook-not-found -s bash -- "$1"; then - _mise_hook - "$@" - elif [ -n "$(declare -f _command_not_found_handle)" ]; then - _command_not_found_handle "$@" - else - echo "bash: command not found: $1" >&2 - return 127 - fi + if mise hook-not-found -s bash -- "$1"; then + _mise_hook + "$@" + elif [ -n "$(declare -f _command_not_found_handle)" ]; then + _command_not_found_handle "$@" + else + echo "bash: command not found: $1" >&2 + return 127 + fi } + fi fi -- cgit v1.2.3