Textualize/rich

[BUG] Panel title/subtitle styles are overriden by border style

noprobelm opened this issue · 2 comments

I'll echo the others here by first expressing my thanks for making such a great tool. I'm an enthusiast, and rich has become an integral part of my daily code. I greatly admire your work.

To the problem...

Problem Description

I'm having an issue with panels. If a Panel instance has stylized title and subtitle attributes, their styles will invariably be overwritten by whatever the border_style is at the time of rendering (if a border style is present). I would expect this kind of behavior if title and subtitle were without styles, but not when they've been explicitly defined in the Panel instance.

How to Recreate

This code will recreate the problem.

from rich.panel import Panel
from rich.text import Text
from rich import print

title = Text("Wake up!", "bold #ffb703")
subtitle = Text("The world says hello!", "italic #f64740")
renderable = Text(f"A panel with 'title' and 'subtitle' elements")

panel = Panel(
    renderable=renderable,
    title=title,
    subtitle=subtitle,
    border_style="#5EAD7A",
    style='bold #81adc8'
)

print(panel)

panel = Panel(
    renderable=renderable,
    title=title,
    subtitle=subtitle,
)
panel.style='bold #81adc8'
print(panel)

Which produces

unexpected

I would expect the title/subtitles in the resulting panels would be stylized according to what's specified in the Panel instance, like this

expected

Root Cause

I think this problem is due to the way the __rich_console__ method for Panel overwrites the styling for title_text and subtitle_text with border_style (if Panel.border_style != "none"), irrespective of whether styling for Panel.title or Panel.subtitle were defined already. The code in question:

rich.panel.Panel; 198-199
rich.panel.Panel; 247-248

Proposed Solution

I've forked rich and made local modifications with a possible solution. I'll create a pull request in accordance with your guidelines later today so we can review.

Platform & Configuration Details

Click to expand
  • Arch Linux
  • xfce4-terminal
$ 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                                                           │
╰──────────────────────────────────────────────────────────────────────────────────╯
╭─── <class 'rich._windows.WindowsConsoleFeatures'> ────╮
│ Windows features available.                           │
│                                                       │
│ ╭───────────────────────────────────────────────────╮ │
│ │ WindowsConsoleFeatures(vt=False, truecolor=False) │ │
│ ╰───────────────────────────────────────────────────╯ │
│                                                       │
│ truecolor = False                                     │
│        vt = False                                     │
╰───────────────────────────────────────────────────────╯
╭────── Environment Variables ───────╮
│ {                                  │
│     'TERM': 'xterm-256color',      │
│     'COLORTERM': 'truecolor',      │
│     '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 │
│ }                                  │
╰────────────────────────────────────╯
$ pip freeze | grep rich
rich==13.0.1

Thank you for your issue. Give us a little time to review it.

PS. You might want to check the FAQ if you haven't done so already.

This is an automated reply, generated by FAQtory

This problem is tangential to issue 2446, but not the same. The code introduced by pull request 2543 aimed at resolving the issue is what resulted in the current behavior.