diff --git a/rosbag2_transport/src/rosbag2_transport/player_progress_bar_impl.hpp b/rosbag2_transport/src/rosbag2_transport/player_progress_bar_impl.hpp index 279293291..0542dee53 100644 --- a/rosbag2_transport/src/rosbag2_transport/player_progress_bar_impl.hpp +++ b/rosbag2_transport/src/rosbag2_transport/player_progress_bar_impl.hpp @@ -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(status) << "] \n" << + "/" << duration_secs_ << " [" << static_cast(status) << "]\n" << // Go up to the beginning of the blank lines progress_bar_helper_move_cursor_up_; o_stream_ << ss.rdbuf() << std::flush; diff --git a/rosbag2_transport/test/rosbag2_transport/test_player_progress_bar.cpp b/rosbag2_transport/test/rosbag2_transport/test_player_progress_bar.cpp index c81b84a05..bf760aaa2 100644 --- a/rosbag2_transport/test/rosbag2_transport/test_player_progress_bar.cpp +++ b/rosbag2_transport/test/rosbag2_transport/test_player_progress_bar.cpp @@ -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.*" ) ); } @@ -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.*" ) ); } @@ -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.*" ) ); @@ -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.*" ) );