diff options
author | Chris Laplante <chris.laplante@agilent.com> | 2020-07-31 11:42:48 -0400 |
---|---|---|
committer | Richard Purdie <richard.purdie@linuxfoundation.org> | 2020-08-07 21:23:02 +0100 |
commit | 889a873d71a6543efb71a0eb4ea6632c9f17175d (patch) | |
tree | cae572f39a839b2dc7e400f1bbc27369c1b19ac5 /lib/bb/progress.py | |
parent | ff821022ef1fdf05482590d8e4fe003abf227135 (diff) | |
download | bitbake-889a873d71a6543efb71a0eb4ea6632c9f17175d.tar.gz |
progress: filter ANSI escape codes before looking for progress text
This is in prepartion for introducing the log-colorizer bbclass into poky.
Signed-off-by: Chris Laplante <chris.laplante@agilent.com>
Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
Diffstat (limited to 'lib/bb/progress.py')
-rw-r--r-- | lib/bb/progress.py | 25 |
1 files changed, 22 insertions, 3 deletions
diff --git a/lib/bb/progress.py b/lib/bb/progress.py index 8cddefaeb..d051ba019 100644 --- a/lib/bb/progress.py +++ b/lib/bb/progress.py @@ -15,6 +15,25 @@ import bb.build from bb.build import StdoutNoopContextManager +# from https://stackoverflow.com/a/14693789/221061 +ANSI_ESCAPE_REGEX = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]') + + +def filter_color(string): + """ + Filter ANSI escape codes out of |string|, return new string + """ + return ANSI_ESCAPE_REGEX.sub('', string) + + +def filter_color_n(string): + """ + Filter ANSI escape codes out of |string|, returns tuple of + (new string, # of ANSI codes removed) + """ + return ANSI_ESCAPE_REGEX.subn('', string) + + class ProgressHandler: """ Base class that can pretend to be a file object well enough to be @@ -82,7 +101,7 @@ class LineFilterProgressHandler(ProgressHandler): lbreakpos = line.rfind('\r') + 1 if lbreakpos: line = line[lbreakpos:] - if self.writeline(line): + if self.writeline(filter_color(line)): super().write(line) def writeline(self, line): @@ -97,7 +116,7 @@ class BasicProgressHandler(ProgressHandler): self._fire_progress(0) def write(self, string): - percs = self._regex.findall(string) + percs = self._regex.findall(filter_color(string)) if percs: progress = int(percs[-1]) self.update(progress) @@ -112,7 +131,7 @@ class OutOfProgressHandler(ProgressHandler): self._fire_progress(0) def write(self, string): - nums = self._regex.findall(string) + nums = self._regex.findall(filter_color(string)) if nums: progress = (float(nums[-1][0]) / float(nums[-1][1])) * 100 self.update(progress) |