Skip to content

Commit 6f16183

Browse files
committed
lib/log: Improve @go.log_command line formatting
It occurred to me that `@go.log_command` should not try to only convert unexpanded formatting code sequences if they exist in the output, but should strip them out entirely whether they're expanded or not.
1 parent e681b86 commit 6f16183

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

lib/log

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ readonly __GO_LOG_COMMAND_EXIT_PATTERN='^@go.log_command (exit|fatal):([0-9]+)$'
460460
local __go_log_command_args=("$@")
461461
local cmd_string="${__go_log_command_args[*]}"
462462
local line
463+
local stripped_line
463464
local exit_state
464465
local exit_status=1
465466

@@ -482,6 +483,7 @@ readonly __GO_LOG_COMMAND_EXIT_PATTERN='^@go.log_command (exit|fatal):([0-9]+)$'
482483

483484
while IFS= read -r line; do
484485
line="${line%$'\r'}"
486+
stripped_line=''
485487

486488
if [[ "$line" =~ $__GO_LOG_COMMAND_EXIT_PATTERN ]]; then
487489
# If the line immediately previous was fatal, keep the fatal state.
@@ -497,9 +499,12 @@ readonly __GO_LOG_COMMAND_EXIT_PATTERN='^@go.log_command (exit|fatal):([0-9]+)$'
497499

498500
for fd in "${__go_log_level_file_descriptors[@]}"; do
499501
if [[ -t "$fd" || -n "$_GO_LOG_FORMATTING" ]]; then
500-
printf '%b\n' "$line" >&"$fd"
501-
else
502502
printf '%s\n' "$line" >&"$fd"
503+
else
504+
if [[ -z "$stripped_line" ]]; then
505+
@go.strip_formatting_codes "$line" stripped_line
506+
fi
507+
printf '%s\n' "$stripped_line" >&"$fd"
503508
fi
504509
done
505510
done < <(_@go.log_command_invoke)

tests/log/log-command.bats

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ teardown() {
101101
@test "$SUITE: log single failing command to standard error" {
102102
run_log_script \
103103
'function failing_function() {' \
104-
' printf "%s\n" "\e[1m$*\e[0m" >&2' \
104+
' printf "%b\n" "\e[1m$*\e[0m" >&2' \
105105
' exit 127' \
106106
'}' \
107107
'@go.log_command failing_function foo bar baz'
108108

109109
assert_failure
110110
assert_log_equals \
111111
RUN 'failing_function foo bar baz' \
112-
'\e[1mfoo bar baz\e[0m' \
112+
'foo bar baz' \
113113
ERROR 'failing_function foo bar baz (exit status 127)'
114114
assert_log_file_equals "$TEST_LOG_FILE" "${lines[@]}"
115115
}
@@ -120,7 +120,7 @@ teardown() {
120120

121121
_GO_LOG_FORMATTING='true' run_log_script \
122122
'function failing_function() {' \
123-
' printf "%s\n" "\e[1m$*\e[0m" >&2' \
123+
' printf "%b\n" "\e[1m$*\e[0m" >&2' \
124124
' exit 127' \
125125
'}' \
126126
'@go.log_command failing_function foo bar baz'

0 commit comments

Comments
 (0)