Custom __rich_console__ method - only the first line of output is being displayed
See original GitHub issueRead the docs
I have checked the documentation for __rich_console__
- I couldnโt find much, so I may have missed something.
Describe the bug
I have a class that wraps a renderable, with a custom __rich_console__
method which yields only the last N lines of the rendered representation of the wrapped renderable. However, when I use it, only the first of the yielded lines is displayed.
To Reproduce
import rich
class Latest:
def __init__(self, n, renderable):
self.n = n
self.renderable = renderable
def __rich_console__(self, console, options):
for line in console.render_lines(self.renderable)[-self.n:]:
for seg in line:
print(repr(seg))
yield seg
rich.print(Latest(3, "\n".join(f"Line {i}" for i in range(10))))
Output from this is:
Segment('Line ', Style())
Segment('7', Style(color=Color('cyan', ColorType.STANDARD, number=6), bold=True, italic=False))
Segment('
', Style())
Segment('Line ', Style())
Segment('8', Style(color=Color('cyan', ColorType.STANDARD, number=6), bold=True, italic=False))
Segment('
', Style())
Segment('Line ', Style())
Segment('9', Style(color=Color('cyan', ColorType.STANDARD, number=6), bold=True, italic=False))
Segment('
')
Line 7
It shows all of the segments, but the rendered output is only โLine 7โ - the following lines appear to have been discarded.
Platform What platform (Win/Linux/Mac) are you running on? Windows 10 What terminal software are you using? Windows Terminal
Diagnose I may ask you to cut and paste the output of the following commands. It may save some time if you do it now.
python -m rich.diagnose
python -m rich._windows
pip freeze | grep rich
โฏ python -m rich.diagnose
โญโโโโโโโโโโโโโโโโโโโโโโโโโ <class 'rich.console.Console'> โโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ A high level console interface. โ
โ โ
โ โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ <console width=120 ColorSystem.TRUECOLOR> โ โ
โ โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ โ
โ โ
โ color_system = 'truecolor' โ
โ encoding = 'utf-8' โ
โ file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> โ
โ height = 30 โ
โ 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=120, height=30), โ
โ legacy_windows=False, โ
โ min_width=1, โ
โ max_width=120, โ
โ is_terminal=True, โ
โ encoding='utf-8', โ
โ max_height=30, โ
โ justify=None, โ
โ overflow=None, โ
โ no_wrap=False, โ
โ highlight=None, โ
โ markup=None, โ
โ height=None โ
โ ) โ
โ quiet = False โ
โ record = False โ
โ safe_box = True โ
โ size = ConsoleDimensions(width=120, height=30) โ
โ soft_wrap = False โ
โ stderr = False โ
โ style = None โ
โ tab_size = 8 โ
โ width = 120 โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
PS 16:55 (.venv) 00:00.461 C:\Work\Scratch\threadprog
โฏ python -m rich._windows
platform="Windows"
WindowsConsoleFeatures(vt=True, truecolor=True)
PS 16:56 (.venv) 00:00.449 C:\Work\Scratch\threadprog
โฏ pip freeze | grep rich
rich==10.10.0
Did I help?
If I was able to resolve your problem, consider sponsoring my work on Rich, or buy me a coffee to say thanks.
Issue Analytics
- State:
- Created 2 years ago
- Comments:5
Ah! They are probably long stretches of spaces to fill the line. Itโs the terminal wrapping them that confused me. That makes sense (Iโm sort of abusing the machinery here, so getting something weird isnโt entirely unexpected).
Thanks for the very fast and useful help.
Youโll need new lines (โ\nโ) in the output. Easiest way to do that would be to set
new_lines=True
on therender_lines
call.