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

[REQUEST] Syntax highlighting using standard ANSI colours. #284

Closed
tomchristie opened this issue Sep 15, 2020 · 3 comments · Fixed by #287
Closed

[REQUEST] Syntax highlighting using standard ANSI colours. #284

tomchristie opened this issue Sep 15, 2020 · 3 comments · Fixed by #287
Labels
accepted Task was accepted enhancement New feature or request

Comments

@tomchristie
Copy link
Contributor

I'm working on an HTTP command line client using rich, which is looking great, but I'm having a few problems getting the syntax highlighting just right.

The problem is that I really want to use the whatever the existing console background colour is, but also want a theme that works well on both light & dark consoles, so need to also be able to use the default foreground text colour for at least part of the theme.

An example of a tool that gets this right is httpie - https://github.com/httpie/httpie

Their default theme is "auto" which uses pygment's TerminalFormatter, and which results in a theme that has a different foreground text depending on the terminal style.

Screen Shot 2020-09-15 at 20 37 51

Screen Shot 2020-09-15 at 20 38 00

I don't think I'm currently able to replicate something similar in rich.

Similarly, for cases where a user does explicitly select a theme, I'd ideally still like to use the terminal background, rather than painting over it. I can do this part, but only by digging into private API...

syntax._background_color = None
syntax._pygments_style_class.background_color = None

Ideally I'd love to find a good way to get two things out of this...

  • The ability to use an "auto" theme, that uses standard ANSI colours, and matches the terminal theme.
  • Some public API for removing any explicit background colour on syntax highlighting, so only the text is colourised.
@willmcgugan
Copy link
Collaborator

Thanks for the pointers.

It looks like Pygments terminal formatter essentially has its own theme that maps to ansi colors. I think I can replicate that. There are actually 2 terminal "themes" in the terminal formatter, for light and dark backgrounds.

What I might do is add a special case "ansi_light" and "ansi_dark" themes. Although I doubt the distinction is necessary unless the terminal is using the default primary colors (probably just cmd.exe).

I will also add an option to override the background color, which you can set to "default"

@willmcgugan willmcgugan added accepted Task was accepted enhancement New feature or request and removed Needs triage labels Sep 16, 2020
@willmcgugan willmcgugan reopened this Sep 18, 2020
@willmcgugan
Copy link
Collaborator

@tomchristie 7.0.0 has these changes to Syntax.

You can set the theme to "ansi_dark" or "ansi_light" which will use system defined colors. You can also set background_color="default" to force the background to be transparent.

@tomchristie
Copy link
Contributor Author

Love it! Looks fantastic. 💚

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
accepted Task was accepted enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants