Convert string from Text to str keeping ansi escape codes intact #2648
-
Hello! I've been wandering around the roads of the internet for hours. Is it possible to use >>> s = Text.to_ansi('[b]This is bold text.')
>>> str(s)
'\x1b[1mThis is bold text.\x1b[0m' |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
OK I found out one can do >>> S = Style(bold=True)
>>> s = S.render('This is bold text.')
>>> str(s)
'\x1b[1mThis is bold text.\x1b[0m' I suppose that could work but I'd prefer using markup |
Beta Was this translation helpful? Give feedback.
-
See the docs for how to capture output. |
Beta Was this translation helpful? Give feedback.
-
Oh! How could I miss that. I thought of from rich.console import Console
console = Console()
def text_to_ansi(s):
with console.capture() as capture:
console.print(s)
return capture.get()
s = text_to_ansi('[b]This is bold text.')
str(s)
# '\x1b[1mThis is bold text.\x1b[0m\n' Easy! Thank you so much Will! I have to say, I'm so impressed with Rich and Textual. Such cool tools. 🤩 |
Beta Was this translation helpful? Give feedback.
See the docs for how to capture output.