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] Using rich.syntax CLI with line numbers and indent has incorrect background #1088

Closed
davised opened this issue Mar 4, 2021 · 4 comments
Labels
bug Something isn't working

Comments

@davised
Copy link
Contributor

davised commented Mar 4, 2021

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.

@willmcgugan willmcgugan added bug Something isn't working and removed Needs triage labels Mar 4, 2021
@willmcgugan
Copy link
Collaborator

Thanks. I'll have a fix in the next version...

@davised
Copy link
Contributor Author

davised commented Mar 4, 2021

Thanks Will. Appreciate your work here. I'm thinking of submitting some feature updates to track() when I have the time to go through the pr with you.

Stay safe and healthy.

@willmcgugan
Copy link
Collaborator

Thanks @davised Please try 9.13.0

@davised
Copy link
Contributor Author

davised commented Mar 7, 2021

Thanks for the quick turnaround. Looks beautiful.

I like the syntax highlighting choices from your package more than my own vim theme despite them using the same colors!

Cheers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants