Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Panel borders broken if using a styled console as renderable #3252

Closed
sivann opened this issue Jan 8, 2024 · 5 comments
Closed

[BUG] Panel borders broken if using a styled console as renderable #3252

sivann opened this issue Jan 8, 2024 · 5 comments

Comments

@sivann
Copy link

sivann commented Jan 8, 2024

  • [ X] I've checked docs and closed issues for possible solutions.
  • [ X] I can't find my issue in the FAQ.

Describe the bug

I created a new renderable based on console, to show recent logs (like tail -f) inside a layout panel. A full example follows:

from rich.console import Console
import datetime
import os
import time
from rich.layout import Layout
from rich.live import Live
from rich.panel import Panel
from rich.pretty import Pretty

class ConsoleLog(Console):
    def __init__(self,*args,**kwargs):
        console_file = open(os.devnull,'w')
        super().__init__(record=True,file=console_file,*args,**kwargs)

    def __rich_console__(self, console, options):
        w=options.size[0]
        h=options.size[1]
        lines = self.export_text(clear=False, styles=True).split('\n')
        for line in lines[-h:]:
            yield line

if __name__=='__main__':

    layout = Layout(name="root")
    layout.split(Layout(name="top", ratio=2), Layout(name="bottom", ratio=3),)

    cp=ConsoleLog()
    layout['top'].update(Panel(cp))


    live = Live(layout, refresh_per_second=1)
    live.start(refresh=True)
    time.sleep(1)


    lines=[]
    while True:
        time.sleep(1)
        now = datetime.datetime.now(datetime.timezone.utc)
        dt=now.strftime("%H:%M:%S")
        lines.append(dt)
        msg=f"{dt} [red]Should be RED[/red]"
        cp.print(msg)

When rich_console yields styled lines (styles=True in export_text()) , then Panel borders get broken. Padding the yielded string does not seem to help.

Screenshot:

image

Another example with variable length lines:
image

Platform
Linux, Rich 13.7.0, python 3.10, terminal iTerm on MacOS

╭───────────────────────── <class 'rich.console.Console'> ─────────────────────────╮
│ A high level console interface.                                                  │
│                                                                                  │
│ ╭──────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=142 ColorSystem.EIGHT_BIT>                                    │ │
│ ╰──────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                  │
│     color_system = '256'                                                         │
│         encoding = 'utf-8'                                                       │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'> │
│           height = 63                                                            │
│    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=142, height=63),             │
│                        legacy_windows=False,                                     │
│                        min_width=1,                                              │
│                        max_width=142,                                            │
│                        is_terminal=True,                                         │
│                        encoding='utf-8',                                         │
│                        max_height=63,                                            │
│                        justify=None,                                             │
│                        overflow=None,                                            │
│                        no_wrap=False,                                            │
│                        highlight=None,                                           │
│                        markup=None,                                              │
│                        height=None                                               │
│                    )                                                             │
│            quiet = False                                                         │
│           record = False                                                         │
│         safe_box = True                                                          │
│             size = ConsoleDimensions(width=142, height=63)                       │
│        soft_wrap = False                                                         │
│           stderr = False                                                         │
│            style = None                                                          │
│         tab_size = 8                                                             │
│            width = 142                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <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,                 │
│     'JUPYTER_COLUMNS': None,       │
│     'JUPYTER_LINES': None,         │
│     'JPY_PARENT_PID': None,        │
│     'VSCODE_VERBOSE_LOGGING': None │
│ }                                  │
╰────────────────────────────────────╯
platform="Linux"

Copy link

github-actions bot commented Jan 8, 2024

We found the following entry in the FAQ which you may find helpful:

Feel free to close this issue if you found an answer in the FAQ. Otherwise, please give us a little time to review.

This is an automated reply, generated by FAQtory

@willmcgugan
Copy link
Collaborator

You are capturing rendered output, which will have escape sequences in them. Rich won't decode these escapes sequences by default. You could use Text.from_ansi to convert the strings in to something that Rich can print.

@sivann
Copy link
Author

sivann commented Jan 9, 2024

@willmcgugan thank you! That fixed it, I could not make the connection. For anyone else reading:
replaced: yield line
with: yield Text.from_ansi(line)

@sivann sivann closed this as completed Jan 9, 2024
Copy link

github-actions bot commented Jan 9, 2024

I hope we solved your problem.

If you like using Rich, you might also enjoy Textual

@sivann
Copy link
Author

sivann commented Jan 9, 2024

Not the place to ask but would it be possible to call console's smart highlighting in another renderable, e.g. a table cell without rendering a console inside ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants