[BUG] Console object failing to capture in a context manager
Describe the bug
I might have been holding it wrong, but up until version 12.0.1 the following minimal example gave the expected output.
Code:
from rich.console import Console
recorder = Console(record=True)
with recorder.capture():
recorder.print("foo")
recorder.export_text()
Output:
''
Expected output:
'foo\n'
First appears in version 12.0.1 for me.
Platform
Click to expand
Python 3.6.12 on Ubuntu 20.04
I may ask you to copy and paste the output of the following commands. It may save some time if you do it now.
If you're using Rich in a terminal:
python -m rich.diagnose
╭─────────────────────── <class 'rich.console.Console'> ────────────────────────╮
│ A high level console interface. │
│ │
│ ╭───────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=81 ColorSystem.EIGHT_BIT> │ │
│ ╰───────────────────────────────────────────────────────────────────────────╯ │
│ │
│ color_system = '256' │
│ encoding = 'utf-8' │
│ file = <_io.TextIOWrapper name='<stdout>' mode='w' │
│ encoding='utf-8'> │
│ height = 41 │
│ is_alt_screen = False │
│ is_dumb_terminal = False │
│ is_interactive = True │
│ is_jupyter = False │
│ is_terminal = True │
│ legacy_windows = False │
│ no_color = False │
│ options = ConsoleOptions( │
│ size=ConsoleDimensions(width=81, height=41), │
│ legacy_windows=False, │
│ min_width=1, │
│ max_width=81, │
│ is_terminal=True, │
│ encoding='utf-8', │
│ max_height=41, │
│ justify=None, │
│ overflow=None, │
│ no_wrap=False, │
│ highlight=None, │
│ markup=None, │
│ height=None │
│ ) │
│ quiet = False │
│ record = False │
│ safe_box = True │
│ size = ConsoleDimensions(width=81, height=41) │
│ soft_wrap = False │
│ stderr = False │
│ style = None │
│ tab_size = 8 │
│ width = 81 │
╰───────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available. │
│ │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│ │
│ truecolor = False │
│ vt = False │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ { │
│ 'TERM': 'xterm-256color', │
│ 'COLORTERM': None, │
│ 'CLICOLOR': None, │
│ 'NO_COLOR': None, │
│ 'TERM_PROGRAM': None, │
│ 'COLUMNS': None, │
│ 'LINES': None, │
│ 'JPY_PARENT_PID': None, │
│ 'VSCODE_VERBOSE_LOGGING': None │
│ } │
╰────────────────────────────────────╯
platform="Linux"
pip freeze | grep rich
rich==12.0.1
I can work around this change in behaviour, so happy for this issue to be closed. However, don't know if the change was intentional, so will leave to one of you to make the call.
IIUC then here is the change that caused it: https://github.com/Textualize/rich/pull/2066/files#diff-8f507d88f3d034716fad069c6a73da25e2915a216a2bdda6db2e8d0c13a0020aL1968-L1970
Wanted to +1 this. I worked around it using .begin_capture() / .end_capture(), but .capture() stopped working when I bumped to 12.3
This wasn't intentional, and I suspect we should restore the original behaviour.
There are differing requirements with respect to capture. Some want the capture output to also go to the terminal, and some wan't no output.
I think we should probably make it an option in capture, i.e. capture(echo=True) (the default) if you want the output to also go the terminal, and capture(echo=False) if you want to suppress the terminal output.
@willmcgugan that sounds reasonable. I've had the same need. CLI app where --out <file path> is an option on all commands. The output is displayed and stashed to a file with that option. So an echo option would be perfect.
This discussion did make another thing come to mind... Currently when I need to do this I capture/save in a variable. When I go to display, I use typer.echo because all of the rich markup is converted ANSI escape codes for the colors, which console.print/rich.print print literally. Is there a reasonable way to reliably detect ANSI colors are in the str and have console.prints behavior adapt to display the colored output when this is the case? Not sure if there would be unintended consequences.