Workaround the Github forgetting styles bug

This commit is contained in:
Joe Rickerby
2022-12-04 13:51:56 +00:00
parent c777af1f60
commit b00547cd97
3 changed files with 38 additions and 1 deletions
+28
View File
@@ -672,3 +672,31 @@ def chdir(new_path: Path | str) -> Generator[None, None, None]:
yield
finally:
os.chdir(cwd)
def fix_ansi_codes_for_github_actions(text: str) -> str:
"""
Github Actions forgets the current ANSI style on every new line. This
function repeats the current ANSI style on every new line.
"""
ansi_code_regex = re.compile(r"(\033\[[0-9;]*m)")
ansi_codes: list[str] = []
output = ""
for line in text.split("\n"):
# add the current ANSI codes to the beginning of the line
output += "".join(ansi_codes) + line + "\n"
# split the line at each ANSI code
parts = ansi_code_regex.split(line)
# if there are any ANSI codes, save them
if len(parts) > 1:
# iterate over the ANSI codes in this line
for code in parts[1::2]:
if code == "\033[0m":
# reset the list of ANSI codes when the clear code is found
ansi_codes = []
else:
ansi_codes.append(code)
return output