Skip to content

Commit

Permalink
Add line cleanup ANSI codes before printing progress bar info
Browse files Browse the repository at this point in the history
- Rationale: To clean up possible leftovers from previous output in
 cases when separation lines was not enough to clean space for progress
 bar, over when playback start over in the loop after the end.
- Also got rid of trailing spaces after printing status.

Signed-off-by: Michael Orlov <michael.orlov@apex.ai>
  • Loading branch information
morlov-apexai committed Feb 19, 2025
1 parent 4d3180c commit 9554b72
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -134,13 +134,12 @@ class PlayerProgressBarImpl {
ss <<
// Clear and print newlines
progress_bar_helper_clear_and_move_cursor_down_ <<
// Print progress bar
"====== Playback Progress ======\n" <<
// Print progress bar
// Use "\033[2K" ANSI control code to clear an entire line in the terminal before output.
"\033[2K====== Playback Progress ======\n\033[2K" <<
"[" << std::fixed << std::setprecision(9) << progress_current_time_secs_ <<
"] Duration " << std::setprecision(2) << progress_secs_from_start_ <<
// Spaces at the end are used to clear any previous progress bar in case the new one is
// shorter, which can happen when the playback starts a new loop.
"/" << duration_secs_ << " [" << static_cast<char>(status) << "] \n" <<
"/" << duration_secs_ << " [" << static_cast<char>(status) << "]\n" <<
// Go up to the beginning of the blank lines
progress_bar_helper_move_cursor_up_;
o_stream_ << ss.rdbuf() << std::flush;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ TEST_F(TestPlayerProgressBar, can_dtor_after_output) {
}
EXPECT_THAT(oss.str(),
MatchesRegex(
"====== Playback Progress ======\n"
"\\[1000000000.000000000\\] Duration 0\\.00/1000000000\\.00 \\[D\\].*"
"\033\\[2K====== Playback Progress ======\n\033\\[2K"
"\\[1000000000.000000000\\] Duration 0\\.00/1000000000\\.00 \\[D\\]\n.*"
)
);
}
Expand Down Expand Up @@ -124,10 +124,10 @@ TEST_F(TestPlayerProgressBar, update_status_with_enabled_progress_bar) {
progress_bar->update(PlayerProgressBar::PlayerStatus::STOPPED);
EXPECT_THAT(oss.str(),
MatchesRegex(
".*====== Playback Progress ======\n"
".*\033\\[2K====== Playback Progress ======\n\033\\[2K"
"\\[0\\.000000000\\] Duration 0\\.00/0\\.00 \\[R\\].*"
".*====== Playback Progress ======\n"
"\\[0\\.000000000\\] Duration 0\\.00/0\\.00 \\[S\\].*"
".*\033\\[2K====== Playback Progress ======\n\033\\[2K"
"\\[0\\.000000000\\] Duration 0\\.00/0\\.00 \\[S\\]\n.*"
)
);
}
Expand Down Expand Up @@ -257,11 +257,11 @@ TEST_F(TestPlayerProgressBar, update_with_limited_rate_respect_update_rate) {
// Check if the progress bar is updated at the correct 3 timestamps
EXPECT_THAT(oss.str(),
MatchesRegex(
".*====== Playback Progress ======\n"
".*\033\\[2K====== Playback Progress ======\n\033\\[2K"
"\\[1\\.000000000\\] Duration 0\\.00/5\\.00"
".*====== Playback Progress ======\n"
".*\033\\[2K====== Playback Progress ======\n\033\\[2K"
"\\[1\\.325000000\\] Duration 0\\.32/5\\.00"
".*====== Playback Progress ======\n"
".*\033\\[2K====== Playback Progress ======\n\033\\[2K"
"\\[1\\.700000000\\] Duration 0\\.70/5\\.00.*"
)
);
Expand Down Expand Up @@ -317,7 +317,7 @@ TEST_F(TestPlayerProgressBar, update_with_limited_rate_with_zero_timestamp) {

EXPECT_THAT(oss.str(),
MatchesRegex(
".*====== Playback Progress ======\n"
".*\033\\[2K====== Playback Progress ======\n\033\\[2K"
"\\[0\\.000000000\\] Duration -1\\.00/5\\.00.*"
)
);
Expand Down

0 comments on commit 9554b72

Please sign in to comment.