diff mbox series

knotty: add OSC 9;4 sequence for progress display

Message ID 20261005083628.1451356-1-git@cookiesoft.de
State New
Headers show
Series knotty: add OSC 9;4 sequence for progress display | expand

Commit Message

Marcel Hellwig Oct. 5, 2026, 8:36 a.m. UTC
From: Marcel Hellwig <git@cookiesoft.de>

OSC 9;4[0] allows the terminal emulator to display a progress bar.
This is helpful for the long-running tasks in both Bitbake and Devtool.

The most prominent supporters are Windows Terminal and KDE's Konsole.
Terminals that do not support the sequence simply ignore it, so no
special treament is required.

[0]: https://learn.microsoft.com/en-us/windows/terminal/tutorials/progress-bar-sequences

Signed-off-by: Marcel Hellwig <git@cookiesoft.de>
---
 bitbake/lib/bb/ui/knotty.py | 58 +++++++++++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

Comments

Paul Barker Oct. 5, 2026, 2:31 p.m. UTC | #1
On Mon, 2026-10-05 at 10:36 +0200, git@cookiesoft.de wrote:
> From: Marcel Hellwig <git@cookiesoft.de>
> 
> OSC 9;4[0] allows the terminal emulator to display a progress bar.
> This is helpful for the long-running tasks in both Bitbake and Devtool.
> 
> The most prominent supporters are Windows Terminal and KDE's Konsole.
> Terminals that do not support the sequence simply ignore it, so no
> special treament is required.

Sadly I don't think we can assume this - not all terminal emulators
handle these escape sequences properly. We've had issues with this when
systemd started enabling OSC sequence output by default [1].

I would prefer this to be configurable, and probably off by default if
there isn't a way to detect whether the current terminal supports these
sequences.

[1]: https://lore.kernel.org/openembedded-core/20260415082949.3446677-1-Qi.Chen@windriver.com/

Best regards,
Marcel Hellwig Oct. 5, 2026, 7:39 p.m. UTC | #2
On 10/5/26 16:31, Paul Barker wrote:
> On Mon, 2026-10-05 at 10:36 +0200, git@cookiesoft.de wrote:
>> From: Marcel Hellwig <git@cookiesoft.de>
>>
>> OSC 9;4[0] allows the terminal emulator to display a progress bar.
>> This is helpful for the long-running tasks in both Bitbake and Devtool.
>>
>> The most prominent supporters are Windows Terminal and KDE's Konsole.
>> Terminals that do not support the sequence simply ignore it, so no
>> special treament is required.
Hey Paul,
> Sadly I don't think we can assume this - not all terminal emulators
> handle these escape sequences properly. We've had issues with this when
> systemd started enabling OSC sequence output by default [1].
I'm not sure if we can directly compare these situations.
When we are talking about OSC in systemd, we are talking about embedded 
devices and possible UART connections to them, e.g. via minicom.
When we are talking about Bitbake, we are generally talking about 
desktop or workstation systems with graphical output and a somewhat 
recent systems / terminal emulator.

I see your point though and perhaps it would be safer to not assume, 
that the sequence is always handled correctly, but I would very much 
like you to reconsider this.

> I would prefer this to be configurable, and probably off by default if
> there isn't a way to detect whether the current terminal supports these
> sequences.
Nevertheless, I can think of two possible solutions:
   1. Provide a `supports_term_progress` function, similar to what the 
Rust crate anstyle-query implements[2].
This should cover most terminals that are supporting it for now, 
although it would need to be adjusted as terminal support evolves.

   2. introduce an environment / config variable, such as 
`BB_ENABLE_OSC_PROGRESS` that can be used to control the feature.

I would very much prefer an opt-out mechanism instead of opt-in, but I 
understand your concern about begin  compatible to as many environments 
as possible.

Let me know which approach you would prefer.
I'm more than happy to contribute :)

Regards,
Marcel

[2]: 
https://github.com/rust-cli/anstyle/blob/main/crates/anstyle-progress/src/query.rs


>
> [1]: 
> https://lore.kernel.org/openembedded-core/20260415082949.3446677-1-Qi.Chen@windriver.com/
>
> Best regards,
>
>
> -=-=-=-=-=-=-=-=-=-=-=-
> Links: You receive all messages sent to this group.
> View/Reply Online (#20295): 
> https://lists.openembedded.org/g/bitbake-devel/message/20295
> Mute This Topic: https://lists.openembedded.org/mt/121588018/10661791
> Group Owner: bitbake-devel+owner@lists.openembedded.org
> Unsubscribe: https://lists.openembedded.org/g/bitbake-devel/unsub 
> [git@cookiesoft.de]
> -=-=-=-=-=-=-=-=-=-=-=-
diff mbox series

Patch

diff --git a/bitbake/lib/bb/ui/knotty.py b/bitbake/lib/bb/ui/knotty.py
index 5198e93d8d..b7388f2948 100644
--- a/bitbake/lib/bb/ui/knotty.py
+++ b/bitbake/lib/bb/ui/knotty.py
@@ -119,6 +119,58 @@  class InteractConsoleLogFilter(logging.Filter):
         self.tf.clearFooter()
         return True
 
+class Osc94FilterNotifier(logging.Filter):
+    def __init__(self, tf):
+        self.tf = tf
+
+    def filter(self, record):
+        if record.levelno == bb.msg.BBLogFormatter.ERROR:
+            self.tf.osc94.error()
+        elif record.levelno == bb.msg.BBLogFormatter.WARNING:
+            self.tf.osc94.warning()
+
+        return True
+
+class Osc94(object):
+    REMOVE = 0
+    SET = 1
+    ERROR = 2
+    INDETERMINATE = 3
+    WARNING = 4
+
+    def __init__(self, fd = None):
+        self.fd = fd
+        self._warning = False
+        self._error = False
+
+    def _write(self, what, argument = None):
+        if self.fd is not None:
+            argument = "" if argument is None else argument
+            self.fd.write(f"\x1b]9;4;{what};{argument}\x07")
+            self.fd.flush()
+
+    def clear(self):
+        self._write(self.REMOVE)
+
+    def indeterminate(self):
+        self._write(self.INDETERMINATE)
+
+    def error(self):
+        self._error = True
+
+    def warning(self):
+        self._warning = True
+
+    def progress(self, value):
+        value = int(min(max(value, 0), 100))
+        if self._error:
+            self._write(self.ERROR, value)
+        elif self._warning:
+            self._write(self.WARNING, value)
+        else:
+            self._write(self.SET, value)
+
+
 class TerminalFilter(object):
     rows = 25
     columns = 80
@@ -160,6 +212,7 @@  class TerminalFilter(object):
         self.lastpids = []
         self.lasttime = None
         self.quiet = quiet
+        self.osc94 = Osc94(sys.stdout)
 
         if not self.interactive:
             return
@@ -203,8 +256,10 @@  class TerminalFilter(object):
 
         for h in handlers:
             h.addFilter(InteractConsoleLogFilter(self))
+            h.addFilter(Osc94FilterNotifier(self))
 
         self.main_progress = None
+        self.osc94.indeterminate()
 
     def clearFooter(self):
         if self.footer_present:
@@ -274,6 +329,7 @@  class TerminalFilter(object):
                 tasks.append(msg)
 
         if self.main.shutdown:
+            self.osc94.indeterminate()
             content = pluralise("Waiting for %s running task to finish",
                                 "Waiting for %s running tasks to finish", len(activetasks))
             if not self.quiet:
@@ -303,6 +359,7 @@  class TerminalFilter(object):
             self.main_progress.setmessage(msg)
             progress = max(0, self.helper.tasknumber_current - 1)
             content += self.main_progress.update(progress)
+            self.osc94.progress(self.main_progress.percent)
             print('')
         lines = self.getlines(content)
         if not self.quiet:
@@ -335,6 +392,7 @@  class TerminalFilter(object):
         return lines
 
     def finish(self):
+        self.osc94.clear()
         if self.stdinbackup:
             fd = sys.stdin.fileno()
             self.termios.tcsetattr(fd, self.termios.TCSADRAIN, self.stdinbackup)