Skip to content

Commit e71ea4a

Browse files
committed
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.
1 parent 6dec28b commit e71ea4a

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

lib/log.bash

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,38 @@
1111
: "${BASH_IT_LOG_LEVEL_TRACE:=7}"
1212
readonly "${!BASH_IT_LOG_LEVEL_@}"
1313

14+
function _bash-it-log-prefix-by-path() {
15+
local component_path="${1?${FUNCNAME[0]}: path specification required}"
16+
local without_extension component_directory
17+
local component_filename component_type component_name
18+
19+
# get the directory, if any
20+
component_directory="${component_path%/*}"
21+
# drop the directory, if any
22+
component_filename="${component_path##*/}"
23+
# strip the file extension
24+
without_extension="${component_filename%.bash}"
25+
# strip before the last dot
26+
component_type="${without_extension##*.}"
27+
# strip component type, but try not to strip other words
28+
# - aliases, completions, plugins, themes
29+
component_name="${without_extension%.[acpt][hlo][eimu]*[ens]}"
30+
# Finally, strip load priority prefix
31+
component_name="${component_name##[[:digit:]][[:digit:]][[:digit:]]"${BASH_IT_LOAD_PRIORITY_SEPARATOR:----}"}"
32+
33+
# best-guess for files without a type
34+
if [[ "${component_type:-${component_name}}" == "${component_name}" ]]; then
35+
if [[ "${component_directory}" == *'vendor'* ]]; then
36+
component_type='vendor'
37+
else
38+
component_type="${component_directory##*/}"
39+
fi
40+
fi
41+
42+
# shellcheck disable=SC2034
43+
BASH_IT_LOG_PREFIX="${component_type:-lib}: $component_name"
44+
}
45+
1446
function _has_colors() {
1547
# Check that stdout is a terminal, and that it has at least 8 colors.
1648
[[ -t 1 && "${_bash_it_available_colors:=$(tput colors 2> /dev/null)}" -ge 8 ]]

0 commit comments

Comments
 (0)