You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm trying to use Rich to render the output of commands, possibly containing ANSI sequence and/or unintentional Rich markup, formatted using intentional Rich markup.
fromrichimportprintasrichprintfromrich.markupimportescapefromjinja2importEnvironmenttemplate_string= (
"{% if success %}[green]✓[/green]""{% elif nofail %}[yellow]✗[/yellow]""{% else %}[red]✗[/red]{% endif %} ""[bold]{{ title or command }}[/bold]""{% if failure %} ({{ code }}){% endif %}""{% if failure and output and not quiet %}\n""{{ (' > ' + command + '\n') if title and command else '' }}""{{ output|indent(2 * ' ') }}{% endif %}",
)
template=Environment().from_string(template_string)
richprint(
Text.from_ansi(
template.render(
{
"title": title,
"command": escape(command),
"code": code,
"success": code==0,
"failure": code!=0,
"number": number,
# output could contain unintentional Rich markup that should be escaped,# while keeping ANSI sequences intact "output": output,
"nofail": nofail,
"quiet": quiet,
"silent": silent,
},
),
),
)
The issue is that I can either interpret ANSI sequence, or escape the unintentional Rich markup, but not both.
If I try to interpret ANSI sequences first, Text.from_ansi() returns a Text instance that I can't pass to escape().
If I escape first, ANSI sequences are escaped as well.
Is there a way to get an ANSI-interpreted string rather than a Text instance? 🤔
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I'm trying to use Rich to render the output of commands, possibly containing ANSI sequence and/or unintentional Rich markup, formatted using intentional Rich markup.
The issue is that I can either interpret ANSI sequence, or escape the unintentional Rich markup, but not both.
If I try to interpret ANSI sequences first,
Text.from_ansi()
returns aText
instance that I can't pass toescape()
.If I escape first, ANSI sequences are escaped as well.
Is there a way to get an ANSI-interpreted string rather than a
Text
instance? 🤔Beta Was this translation helpful? Give feedback.
All reactions