Unrendered ANSI codes #1767
Replies: 2 comments 1 reply
|
Yes — route the background log messages through In cmd2 4.2.4, interactive input runs inside For your example, replace the logging setup with a small handler: from rich.text import Text
class PromptLogHandler(logging.Handler):
def __init__(self, app):
super().__init__()
self.app = app
def emit(self, record):
try:
self.app.add_alert(
msg=Text(self.format(record), style="cyan")
)
except Exception:
self.handleError(record)
app = Cmd(allow_cli_args=False)
logging.basicConfig(
level=logging.INFO,
format="%(message)s",
handlers=[PromptLogHandler(app)],
force=True,
)
with Worker().run_in_thread():
app.cmdloop()Keep your
|
|
No public event for that in 4.2.4. cmd2 keeps it in a private flag ( You don't need it though. prompt_toolkit's import io
import logging
from prompt_toolkit import print_formatted_text
from prompt_toolkit.formatted_text import ANSI
from rich.console import Console
from rich.logging import RichHandler
class PromptToolkitWriter(io.TextIOBase):
def write(self, s):
if s:
print_formatted_text(ANSI(s), end="")
return len(s)
console = Console(file=PromptToolkitWriter(), force_terminal=True)
logging.basicConfig(
level="NOTSET",
format="%(message)s",
datefmt="[%X]",
handlers=[RichHandler(console=console, enable_link_path=False)],
)That one handler covers the prompt, a running command, and before/after The Tried with your script plus a command that sleeps 1.5 s (Windows 11, Python 3.13, cmd2 4.2.4, prompt_toolkit 3.0.53, rich 15.0.0): colors are right at the prompt, during the command and after it. Not tried on Python 3.12 or the classic Windows 10 console. |

Uh oh!
There was an error while loading. Please reload this page.
Running into an issue where I have logs coming from another thread while the prompt is waiting for user input. Instead of nicely colored text, the ANSI escape codes are shown. Is there a way to avoid this?
Thank you.
I am running this on Windows 10 with the following:
This can replicate the problem:
All reactions