Unrendered ANSI codes #1767
Replies: 3 comments 3 replies
|
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. |
|
@ShiftPoster @jdfesa @Erfouni I love to see users helping each other out like this. Great work and suggestions all! If anyone has ideas for how to make things better in |




My final solution below. Thank you @jdfesa for the
app.add_alert, it works great for my situation. Also thanks @Erfouni , I like having options, especially when they require less typing. I'll whip that one out when I'm not usingProgress.