From bdaf29f84c4c3a18f9c25bbeec3401652612b44a Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 11 Sep 2021 20:29:49 -0700 Subject: [PATCH] Create lib finalize hook Create an array `_bash_it_library_finalize_hook` and loop at the end of the main `bash_it.sh` to run each element in the array. The purpose here is to run some command after everything else has been loaded. For example, the appearance lib checks for executables for SCM commands, but `$PATH` may be altered after appearance has loaded and therefore some available commands may never be discovered. Therefore, create `_bash_it_appearance_scm_init()` and add it to the hook. It will re-check at end of `bash_it.sh` just before prompt is first displayed. --- bash_it.sh | 12 ++++++++++-- themes/base.theme.bash | 2 +- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/bash_it.sh b/bash_it.sh index bda3407e..59c6ed8e 100755 --- a/bash_it.sh +++ b/bash_it.sh @@ -15,8 +15,11 @@ fi # shellcheck disable=SC1090 source "${BASH_IT}"/vendor/github.com/erichs/composure/composure.sh -# We need to load logging module first as well in order to be able to log -# shellcheck source=./lib/log.bash +# Declare our end-of-main finishing hook +declare -a _bash_it_library_finalize_hook + +# We need to load logging module early in order to be able to log +# shellcheck source-path=SCRIPTDIR/lib source "${BASH_IT}/lib/log.bash" # We can only log it now @@ -148,3 +151,8 @@ if ! _command_exists reload && [[ -n "${BASH_IT_RELOAD_LEGACY:-}" ]]; then ;; esac fi + +for _bash_it_library_finalize_f in "${_bash_it_library_finalize_hook[@]:-}"; do + eval "${_bash_it_library_finalize_f?}" # Use `eval` to achieve the same behavior as `$PROMPT_COMMAND`. +done +unset "${!_bash_it_library_finalize_@}" diff --git a/themes/base.theme.bash b/themes/base.theme.bash index f9f5190d..94e6befd 100644 --- a/themes/base.theme.bash +++ b/themes/base.theme.bash @@ -106,7 +106,7 @@ function _bash_it_appearance_scm_init() { fi fi } -_bash_it_appearance_scm_init +_bash_it_library_finalize_hook+=('_bash_it_appearance_scm_init') function scm { if [[ "$SCM_CHECK" = false ]]; then