Created
January 6, 2023 22:51
-
-
Save traverseda/f630bba8264e445aec08b52295bf4ac5 to your computer and use it in GitHub Desktop.
Convert ansi escape sequences to html tags
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
ansi_escape =re.compile(r'(\x9B|\x1B\[)[0-?]*[ -\/]*[@-~]') | |
def replace_ansi(text): | |
"""Quick hack, not expected to work in all cases but works for basic loguru cases. | |
""" | |
text = text.replace("\x1b[0m","</span>") | |
text = text.replace("\x1b[1m",'<span style="font-weight: bold;">') | |
text = text.replace("\x1b[3m",'<span style="font-style: italic;">') | |
text = text.replace("\x1b[8m",'<span style="text-decoration: line-through;">') | |
text = text.replace("\x1b[4m",'<span style="text-decoration: underline;">') | |
text = text.replace("\x1b[30m",'<span style="color:black;">') | |
text = text.replace("\x1b[31m",'<span style="color:red;">') | |
text = text.replace("\x1b[32m",'<span style="color:green;">') | |
text = text.replace("\x1b[33m",'<span style="color:yellow;">') | |
text = text.replace("\x1b[34m",'<span style="color:blue;">') | |
text = text.replace("\x1b[35m",'<span style="color:magenta;">') | |
text = text.replace("\x1b[36m",'<span style="color:cyan;">') | |
text = text.replace("\x1b[37m",'<span style="color:white;">') | |
return ansi_escape.sub('', text) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment