Losing terminal markup when writing it out to a file #9593
-
I've written a Pytest plugin that marks up sections of the output for manipulation after the test run is over. The marking is done by way of the pytest_configure hook, in a manner very similar to pastebin.py. When the resulting file is written to disk, I can see that almost all of the ANSI escape/color codes are missing. The codes only exist in the final summary line, and in captured log call lines. This means that when I launch my TUI after the test run is over, all of the text, except the summary and log call lines, are un-colorized when displayed on the screen. How can I get it so that all text-markup codes are written out to my output file, and thereby the TUI displays the text as it displays in a normal console? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments
-
Perhaps try running with |
Beta Was this translation helpful? Give feedback.
-
Thanks for the suggestion. I had actually tried that once a while ago but it didn't work. I just tried again and unfortunately, same result. :-( I was using the pastebin code as a template, and I believe what's happening under the covers is it's intercepting the ANSI-encoded text as it gets written to a TerminalWriter instance, and "tee"-ing it, so that the coded text also gets written to a file. So whatever shows up on the screen should also be reproduced in the file. That seems to happen for some sections (e.g. final summary, or captured stdout//stderr). But not for others. My concept of what's happening is a bit vague so I guess I'll just have to dig into the code. There must be a way. :-) |
Beta Was this translation helpful? Give feedback.
-
Figured it out! My 'tee' code was writing plain text to the output file. After examining |
Beta Was this translation helpful? Give feedback.
Figured it out! My 'tee' code was writing plain text to the output file. After examining
_pytest/_io/terminalwriter.py
, I figured out that I could just instantiate theTerminalWriter
class, and then use itsmarkup
method to ANSI-encode my text before writing it to file.