From e71ea4ad02fc82674dec55b57d444eaff1f2813f Mon Sep 17 00:00:00 2001 From: John D Pell Date: Sat, 16 Oct 2021 14:54:08 -0700 Subject: [PATCH] lib/log: function `_bash-it-log-prefix-by-path()` ...to replace `_set-prefix-based-on-path()` in `scripts/reloader`. Deliberately does not use `_bash-it-get-component-name-from-path()`/`_bash-it-get-component-type-from-path()` as we need some of the intermediate state and would have to reimplement anyway. --- lib/log.bash | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/lib/log.bash b/lib/log.bash index 673fdd08..444a6854 100644 --- a/lib/log.bash +++ b/lib/log.bash @@ -11,6 +11,38 @@ : "${BASH_IT_LOG_LEVEL_TRACE:=7}" readonly "${!BASH_IT_LOG_LEVEL_@}" +function _bash-it-log-prefix-by-path() { + local component_path="${1?${FUNCNAME[0]}: path specification required}" + local without_extension component_directory + local component_filename component_type component_name + + # get the directory, if any + component_directory="${component_path%/*}" + # drop the directory, if any + component_filename="${component_path##*/}" + # strip the file extension + without_extension="${component_filename%.bash}" + # strip before the last dot + component_type="${without_extension##*.}" + # strip component type, but try not to strip other words + # - aliases, completions, plugins, themes + component_name="${without_extension%.[acpt][hlo][eimu]*[ens]}" + # Finally, strip load priority prefix + component_name="${component_name##[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR:----}"}" + + # best-guess for files without a type + if [[ "${component_type:-${component_name}}" == "${component_name}" ]]; then + if [[ "${component_directory}" == *'vendor'* ]]; then + component_type='vendor' + else + component_type="${component_directory##*/}" + fi + fi + + # shellcheck disable=SC2034 + BASH_IT_LOG_PREFIX="${component_type:-lib}: $component_name" +} + function _has_colors() { # Check that stdout is a terminal, and that it has at least 8 colors. [[ -t 1 && "${_bash_it_available_colors:=$(tput colors 2> /dev/null)}" -ge 8 ]]