Say I have the string '\033[2KResolving dependencies...\033[2KResolving dependencies...'
In the Python console, I can print this, and it'll only display once
Python 3.10.9 (main, Jan 19 2023, 07:59:38) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> output = '\033[2KResolving dependencies...\033[2KResolving dependencies...'
>>> print(output)
Resolving dependencies...
Is there a way to get a string that consists solely of the printed output? In other words, I would like there to be some function
def evaluate_ansi_escapes(input: str) -> str:
...
such that evaluate_ansi_escapes(output) == 'Resolving dependencies...'
(ideally with the correct amount of whitespace in front)
edit: I've come up with the following stopgap solution
import re
def evaluate_ansi_escapes(input: str) -> str:
erases_regex = r"^.*(\\(033|e)|\x1b)\[2K"
erases = re.compile(erases_regex)
no_erases = []
for line in input.split("\n"):
while len(erases.findall(line)) > 0:
line = erases.sub("", line)
no_erases.append(line)
return "\n".join(no_erases)
This does successfully produce output that is close enough to I want:
>>> evaluate_ansi_escapes(output)
'Resolving dependencies...'
But I would love to know if there is a less hacky way to solve this problem, or if the whitespace preceding 'Resolving dependencies...'
can be captured as well.