From 165cc1b31d59d48f5e17c2c6bfe834925b9bff34 Mon Sep 17 00:00:00 2001 From: Timon Engelke Date: Tue, 13 Dec 2022 13:55:19 +0100 Subject: [PATCH] Fix calculation of string length --- colcon_notification/event_handler/status.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/colcon_notification/event_handler/status.py b/colcon_notification/event_handler/status.py index 246768d..8027e4a 100644 --- a/colcon_notification/event_handler/status.py +++ b/colcon_notification/event_handler/status.py @@ -22,6 +22,19 @@ from colcon_core.subprocess import SIGINT_RESULT +def _colorama_str_len(s): + in_code = False + l = 1 + for c in s: + if c == '\033': + in_code = True + elif c == 'm' and in_code: + in_code = False + elif not in_code: + l += 1 + return l + + class StatusEventHandler(EventHandlerExtensionPoint): """ Continuously update a status line. @@ -183,13 +196,13 @@ def __call__(self, event): # noqa: D102 # append dots when skipping at least one block if i < len(blocks) - 1: msg += ' ...' - if len(msg) < max_width: + if _colorama_str_len(msg) < max_width: break else: return print(msg, end='\r') - self._last_status_line_length = len(msg) + self._last_status_line_length = _colorama_str_len(msg) elif isinstance(data, EventReactorShutdown): self._clear_last_status_line()