diff tests/run-tests.py @ 52539:c2ee1ad78414

run-tests: add a function to colorize a piece of text This will get handy to color some error message
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Thu, 12 Dec 2024 15:00:44 +0000
parents a814534aaedd
children 7fd3a9309d0e
line wrap: on
line diff
--- a/tests/run-tests.py	Thu Dec 12 10:58:48 2024 +0000
+++ b/tests/run-tests.py	Thu Dec 12 15:00:44 2024 +0000
@@ -97,6 +97,7 @@
     import pygments.formatters as formatters
     import pygments.token as token
     import pygments.style as style
+    from pygments.console import ansiformat
 
     if WINDOWS:
         hgpath = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -113,8 +114,17 @@
     pygmentspresent = True
     difflexer = lexers.DiffLexer()
     terminal256formatter = formatters.Terminal256Formatter()
+
+    def colorize(text: str, style: str, color: bool = True) -> str:
+        if not color:
+            return text
+        return ansiformat(style, text)
+
 except ImportError:
-    pass
+
+    def colorize(text: str, style: str, color: bool = True) -> str:
+        return text
+
 
 progress_type = {}