From fce8ae79fb38056de8581b4a75b298a59fa8be18 Mon Sep 17 00:00:00 2001 From: Guillaume Beuzeboc Date: Fri, 1 Nov 2024 20:57:45 +0100 Subject: [PATCH] feat(echo --clear): add --clear option to echo (#819) * feat(echo --clear): add --clear option to echo Signed-off-by: Guillaumebeuzeboc * address review comments, and adjust test result check. Signed-off-by: Tomoya Fujita * use escape sequence instead of clear/cls, and remove test. Signed-off-by: Tomoya Fujita --------- Signed-off-by: Guillaumebeuzeboc Signed-off-by: Tomoya Fujita Co-authored-by: Tomoya Fujita --- ros2topic/ros2topic/verb/echo.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/ros2topic/ros2topic/verb/echo.py b/ros2topic/ros2topic/verb/echo.py index 7f669c17c..0bce1711d 100644 --- a/ros2topic/ros2topic/verb/echo.py +++ b/ros2topic/ros2topic/verb/echo.py @@ -107,6 +107,9 @@ def add_arguments(self, parser, cli_name): parser.add_argument( '--include-message-info', '-i', action='store_true', help='Shows the associated message info.') + parser.add_argument( + '--clear', '-c', action='store_true', + help='Clear screen before printing next message') def choose_qos(self, node, args): @@ -185,6 +188,7 @@ def main(self, *, args): self.no_str = args.no_str self.flow_style = args.flow_style self.once = args.once + self.clear_screen = args.clear self.filter_fn = None if args.filter_expr: @@ -280,6 +284,10 @@ def _subscriber_callback(self, msg, info): if self.future is not None and self.once: self.future.set_result(True) + # Clear terminal screen before print + if self.clear_screen: + clear_terminal() + if not hasattr(submsg, '__slots__'): # raw if self.include_message_info: @@ -322,3 +330,7 @@ def _message_lost_event_callback(message_lost_status): f'\n\ttotal count: {message_lost_status.total_count}', end='---\n' ) + + +def clear_terminal(): + print('\x1b[H\x1b[2J')