diff mbox series

[v2] progressbar/knotty: Allow mixing log messages and progress bars

Message ID 20260325071151.3947605-1-richard.purdie@linuxfoundation.org
State New
Headers show
Series [v2] progressbar/knotty: Allow mixing log messages and progress bars | expand

Commit Message

Richard Purdie March 25, 2026, 7:11 a.m. UTC
If we try and print a log message in the middle of progress bar, the display
can be corrupted. Add a clear() method to progress bars allowing them to be
removed for the log message, then reprinted using update().

Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
---
 lib/bb/ui/knotty.py            | 10 ++++++++++
 lib/progressbar/progressbar.py |  3 +++
 2 files changed, 13 insertions(+)
diff mbox series

Patch

diff --git a/lib/bb/ui/knotty.py b/lib/bb/ui/knotty.py
index e4b7b83061e..82531ef8f31 100644
--- a/lib/bb/ui/knotty.py
+++ b/lib/bb/ui/knotty.py
@@ -87,6 +87,7 @@  class NonInteractiveProgress(object):
     def __init__(self, msg, maxval):
         self.id = msg
         self.msg = msg
+        self.currval = 0
         self.maxval = maxval
         self.finished = False
 
@@ -96,6 +97,10 @@  class NonInteractiveProgress(object):
         return self
 
     def update(self, value):
+        self.currval = value
+        pass
+
+    def clear(self):
         pass
 
     def finish(self):
@@ -791,7 +796,12 @@  def main(server, eventHandler, params, tf = TerminalFilter):
                         event.msg = taskinfo['title'] + ': ' + event.msg
                 if hasattr(event, 'fn') and event.levelno not in [bb.msg.BBLogFormatter.WARNONCE, bb.msg.BBLogFormatter.ERRORONCE]:
                     event.msg = event.fn + ': ' + event.msg
+                # Need to remove any progress bar, then add it back after we print this message
+                if parseprogress:
+                    parseprogress.clear()
                 logging.getLogger(event.name).handle(event)
+                if parseprogress:
+                    parseprogress.update(parseprogress.currval)
                 continue
 
             if isinstance(event, bb.build.TaskFailedSilent):
diff --git a/lib/progressbar/progressbar.py b/lib/progressbar/progressbar.py
index a8e2dc09c91..1562774ba1a 100644
--- a/lib/progressbar/progressbar.py
+++ b/lib/progressbar/progressbar.py
@@ -278,6 +278,9 @@  class ProgressBar(object):
         self.last_update_time = now
         return output
 
+    def clear(self):
+        self.fd.write(" " * self.term_width + '\r')
+        self.fd.flush()
 
     def start(self, update=True):
         """Starts measuring time, and prints the bar at 0%.