Skip to content

Commit 610d0b8

Browse files
author
OpenShift Bot
authored
Merge pull request #9331 from stevekuznetsov/skuznets/clear-string
Merged by openshift-bot
2 parents 53923f3 + 4435217 commit 610d0b8

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

hack/cmd_util.sh

+12-18
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,8 @@ function os::cmd::internal::expect_exit_code_run_grep() {
174174
os::test::junit::declare_test_start
175175

176176
local name=$(os::cmd::internal::describe_call "${cmd}" "${cmd_eval_func}" "${grep_args}" "${test_eval_func}")
177-
echo "Running ${name}..."
177+
local preamble="Running ${name}..."
178+
echo "${preamble}"
178179
# for ease of parsing, we want the entire declaration on one line, so we replace '\n' with ';'
179180
junit_log+=( "${name//$'\n'/;}" )
180181

@@ -193,11 +194,8 @@ function os::cmd::internal::expect_exit_code_run_grep() {
193194
local end_time=$(os::cmd::internal::seconds_since_epoch)
194195
local time_elapsed=$(echo "scale=3; ${end_time} - ${start_time}" | bc | xargs printf '%5.3f') # in decimal seconds, we need leading zeroes for parsing later
195196

196-
# some commands are multi-line, so we may need to clear more than just the previous line
197-
local cmd_length=$(echo "${cmd}" | wc -l)
198-
for (( i=0; i<${cmd_length}; i++ )); do
199-
os::text::clear_last_line
200-
done
197+
# clear the preamble so we can print out the success or error message
198+
os::text::clear_string "${preamble}"
201199

202200
local return_code
203201
if (( cmd_succeeded && test_succeeded )); then
@@ -460,7 +458,8 @@ function os::cmd::internal::run_until_exit_code() {
460458
local description=$(os::cmd::internal::describe_call "${cmd}" "${cmd_eval_func}")
461459
local duration_seconds=$(echo "scale=3; $(( duration )) / 1000" | bc | xargs printf '%5.3f')
462460
local description="${description}; re-trying every ${interval}s until completion or ${duration_seconds}s"
463-
echo "Running ${description}..."
461+
local preamble="Running ${description}..."
462+
echo "${preamble}"
464463
# for ease of parsing, we want the entire declaration on one line, so we replace '\n' with ';'
465464
junit_log+=( "${description//$'\n'/;}" )
466465

@@ -481,11 +480,8 @@ function os::cmd::internal::run_until_exit_code() {
481480
local end_time=$(os::cmd::internal::seconds_since_epoch)
482481
local time_elapsed=$(echo "scale=9; ${end_time} - ${start_time}" | bc | xargs printf '%5.3f') # in decimal seconds, we need leading zeroes for parsing later
483482

484-
# some commands are multi-line, so we may need to clear more than just the previous line
485-
local cmd_length=$(echo "${cmd}" | wc -l)
486-
for (( i=0; i<${cmd_length}; i++ )); do
487-
os::text::clear_last_line
488-
done
483+
# clear the preamble so we can print out the success or error message
484+
os::text::clear_string "${preamble}"
489485

490486
local return_code
491487
if (( cmd_succeeded )); then
@@ -541,7 +537,8 @@ function os::cmd::internal::run_until_text() {
541537
local description=$(os::cmd::internal::describe_call "${cmd}" "" "${text}" "os::cmd::internal::success_func")
542538
local duration_seconds=$(echo "scale=3; $(( duration )) / 1000" | bc | xargs printf '%5.3f')
543539
local description="${description}; re-trying every ${interval}s until completion or ${duration_seconds}s"
544-
echo "Running ${description}..."
540+
local preamble="Running ${description}..."
541+
echo "${preamble}"
545542
# for ease of parsing, we want the entire declaration on one line, so we replace '\n' with ';'
546543
junit_log+=( "${description//$'\n'/;}" )
547544

@@ -564,11 +561,8 @@ function os::cmd::internal::run_until_text() {
564561
local end_time=$(os::cmd::internal::seconds_since_epoch)
565562
local time_elapsed=$(echo "scale=9; ${end_time} - ${start_time}" | bc | xargs printf '%5.3f') # in decimal seconds, we need leading zeroes for parsing later
566563

567-
# some commands are multi-line, so we may need to clear more than just the previous line
568-
local cmd_length=$(echo "${cmd}" | wc -l)
569-
for (( i=0; i<${cmd_length}; i++ )); do
570-
os::text::clear_last_line
571-
done
564+
# clear the preamble so we can print out the success or error message
565+
os::text::clear_string "${preamble}"
572566

573567
local return_code
574568
if (( test_succeeded )); then

hack/text.sh

+19
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,25 @@ function os::text::clear_last_line() {
5555
fi
5656
}
5757

58+
# os::text::clear_string attempts to clear the entirety of a string from the terminal.
59+
# If the string contains literal tabs or other characters that take up more than one
60+
# character space in output, or if the window size is changed before this function
61+
# is called, it will not function correctly.
62+
# No action is taken if this is called outside of a TTY
63+
function os::text::clear_string() {
64+
local -r string="$1"
65+
if [[ -t 1 ]]; then
66+
echo "${string}" | while read line; do
67+
# num_lines is the number of terminal lines this one line of output
68+
# would have taken up with the current terminal width in columns
69+
local num_lines=$(( ${#line} / $( tput cols ) ))
70+
for (( i = 0; i <= num_lines; i++ )); do
71+
os::text::clear_last_line
72+
done
73+
done
74+
fi
75+
}
76+
5877
# os::text::print_bold prints all input in bold text
5978
function os::text::print_bold() {
6079
os::text::bold

0 commit comments

Comments
 (0)