Skip to content

Commit 5c4f98b

Browse files
committed
misc: Fixes
- Print and underline 'panic' messages - Use `core.warn` internally to print warnings - Properly `unset` trap function handlers on `core.trap_remove` - Fix find color function to have same behavior as used interanlly
1 parent 591b5b3 commit 5c4f98b

File tree

3 files changed

+11
-26
lines changed

3 files changed

+11
-26
lines changed

pkg/src/public/bash-core.sh

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ core.trap_remove() {
8484

8585
# rho (WET)
8686
local global_trap_handler_name=
87-
printf -v global_trap_handler_name '%q' "___global_trap_${signal_spec}_handler___"
87+
printf -v global_trap_handler_name '%q' "core.private.trap_handler_${signal_spec}"
88+
8889
unset -f "$global_trap_handler_name"
8990
done
9091
}
@@ -214,14 +215,17 @@ core.err_exists() {
214215
# @description Use when a serious fault occurs. It will print the current ERR (if it exists)
215216
core.panic() {
216217
local code='1'
217-
218218
if [[ $1 =~ [0-9]+ ]]; then
219219
code=$1
220220
elif [ -n "$1" ]; then
221221
if [ -n "$2" ]; then
222222
code=$2
223223
fi
224-
printf '%s\n' "Panic: $1" >&2
224+
if core.private.should_print_color 2; then
225+
printf "\033[1;31m\033[4m%s:\033[0m %s\n" 'Panic' "$1" >&2
226+
else
227+
printf "%s: %s\n" 'Panic' "$1" >&2
228+
fi
225229
fi
226230

227231
if core.err_exists; then
@@ -346,29 +350,13 @@ core.print_info() {
346350
# use tput because simple environment variable checking heuristics suffice. Deprecated because this code
347351
# has been moved to bash-std
348352
core.should_output_color() {
349-
# https://no-color.org
350-
if [ ${NO_COLOR+x} ]; then
351-
return 1
352-
fi
353-
354-
# FIXME
355-
# # 0 => 2 colors
356-
# # 1 => 16 colors
357-
# # 2 => 256 colors
358-
# # 3 => 16,777,216 colors
359-
# if [[ -v FORCE_COLOR ]]; then
360-
# return 0
361-
# fi
362-
363-
if [ "$COLORTERM" = "truecolor" ] || [ "$COLORTERM" = "24bit" ]; then
364-
return 0
365-
fi
353+
local fd="$1"
366354

367-
if [ "$TERM" = 'dumb' ]; then
355+
if [[ ${NO_COLOR+x} || "$TERM" = 'dumb' ]]; then
368356
return 1
369357
fi
370358

371-
if [ -t 0 ]; then
359+
if [ -t "$fd" ]; then
372360
return 0
373361
fi
374362

pkg/src/util/util.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ core.private.util.trap_handler_common() {
3030
return $?
3131
fi
3232
else
33-
printf "%s\n" "Warn: core.trap_add: Function '$trap_handler' registered for signal '$signal_spec' no longer exists. Skipping" >&2
33+
core.print_warn "Trap handler function '$trap_handler' that was registered for signal '$signal_spec' no longer exists. Skipping" >&2
3434
fi
3535
done; unset trap_func
3636
}

tests/misc.bats

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ load './util/init.sh'
2020
NO_COLOR= run core.should_output_color
2121
assert_failure
2222

23-
COLORTERM='truecolor' run core.should_output_color
24-
assert_success
25-
2623
TERM='dumb' run core.should_output_color
2724
assert_failure
2825
}

0 commit comments

Comments
 (0)