Skip to content

[BUG] Using rich.syntax CLI with line numbers and indent has incorrect background #1088

@davised

Description

@davised

Read the docs
Done. For others reading this issue: https://rich.readthedocs.io/en/latest/syntax.html

Describe the bug
The background set using rich.syntax -b is not applied to the line numbers or indents (-l -i flags).

To Reproduce

python -m rich.syntax -ilb '#000000' rich/diagnose.py

syntax_background_issue

Code section
I think this is the relevant section. I found that background_color is set to my -b option, but then highlight_number_style is not set to what is expected (?).

The strange thing is that the card printed using python -m rich looks completely fine to me.

rich/syntax.py
421     def _get_line_numbers_color(self, blend: float = 0.3) -> Color:
422background_color = self._theme.get_background_style().bgcolor
423if background_color is None or background_color.is_system_defined:
424     ┆   ┆   return background_color or Color.default()
425foreground_color = self._get_token_color(Token.Text)
426if foreground_color is None or foreground_color.is_system_defined:
427     ┆   ┆   return foreground_color or Color.default()
428new_color = blend_rgb(
429     ┆   ┆   background_color.get_truecolor(),
430     ┆   ┆   foreground_color.get_truecolor(),
431     ┆   ┆   cross_fade=blend,
432     ┆   )
433return Color.from_triplet(new_color)
434
435     @property
436     def _numbers_column_width(self) -> int:
437"""Get the number of characters used to render the numbers column."""
438column_width = 0
439if self.line_numbers:
440     ┆   ┆   column_width = len(str(self.start_line + self.code.count("\n"))) + 2
441return column_width
442
443     def _get_number_styles(self, console: Console) -> Tuple[Style, Style, Style]:
444"""Get background, number, and highlight styles for line numbers."""
445background_style = self._get_base_style()
446if background_style.transparent_background:
447     ┆   ┆   return Style.null(), Style(dim=True), Style.null()
448if console.color_system in ("256", "truecolor"):
449     ┆   ┆   number_style = Style.chain(
450     ┆   ┆   ┆   background_style,
451     ┆   ┆   ┆   self._theme.get_style_for_token(Token.Text),
452     ┆   ┆   ┆   Style(color=self._get_line_numbers_color()),
453     ┆   ┆   )
454     ┆   ┆   highlight_number_style = Style.chain(
455     ┆   ┆   ┆   background_style,
456     ┆   ┆   ┆   self._theme.get_style_for_token(Token.Text),
457     ┆   ┆   ┆   Style(bold=True, color=self._get_line_numbers_color(0.9)),
458     ┆   ┆   )
459else:
460     ┆   ┆   number_style = background_style + Style(dim=True)
461     ┆   ┆   highlight_number_style = background_style + Style(dim=False)
462return background_style, number_style, highlight_number_style

Platform
I'm on Arch Linux using the kitty terminal (tested on alacritty as well).

Diagnose

$ python -m rich.diagnose
╭─────────────────────────────────────── <class 'rich.console.Console'> ───────────────────────────────────────╮
│ A high level console interface.                                                                              │
│                                                                                                              │
│ ╭──────────────────────────────────────────────────────────────────────────────────────────────────────────╮ │
│ │ <console width=112 ColorSystem.TRUECOLOR>                                                                │ │
│ ╰──────────────────────────────────────────────────────────────────────────────────────────────────────────╯ │
│                                                                                                              │
│     color_system = 'truecolor'                                                                               │
│         encoding = 'utf-8'                                                                                   │
│             file = <_io.TextIOWrapper name='<stdout>' mode='w' encoding='utf-8'>                             │
│           height = 57                                                                                        │
│ is_dumb_terminal = False                                                                                     │
│   is_interactive = True                                                                                      │
│       is_jupyter = False                                                                                     │
│      is_terminal = True                                                                                      │
│   legacy_windows = False                                                                                     │
│         no_color = False                                                                                     │
│          options = ConsoleOptions(size=ConsoleDimensions(width=112, height=57), legacy_windows=False,        │
│                    min_width=1, max_width=112, is_terminal=True, encoding='utf-8', justify=None,             │
│                    overflow=None, no_wrap=False, highlight=None, height=None)                                │
│            quiet = False                                                                                     │
│           record = False                                                                                     │
│         safe_box = True                                                                                      │
│             size = ConsoleDimensions(width=112, height=57)                                                   │
│        soft_wrap = False                                                                                     │
│           stderr = False                                                                                     │
│            style = None                                                                                      │
│         tab_size = 8                                                                                         │
│            width = 112                                                                                       │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────╯

I'm happy to help troubleshoot and/or fix the bug once we get it figured out.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions