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

RGB color on tree-sitter highlight #758

Closed
alemuller opened this issue Oct 10, 2020 · 6 comments · Fixed by #952
Closed

RGB color on tree-sitter highlight #758

alemuller opened this issue Oct 10, 2020 · 6 comments · Fixed by #952
Labels

Comments

@alemuller
Copy link
Contributor

{
  "parser-directories": [
    "/home/alemuller/git"
  ],
  "theme": {
      "number": "#FF0000"
  }
}

When calling tree-sitter highlight file.ext the following error is produced:

thread 'main' panicked at 'Unsupported color type', cli/src/highlight.rs:271:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

When color is a number or an ANSI name it works.

@lazytype
Copy link
Contributor

@maxbrunsfeld are you accepting PRs? seems like this could be a fairly focused change in cli/src/highlight.rs

@maxbrunsfeld
Copy link
Contributor

Right now, the RGB ones only work with ‘—html’. Do many terminals support RGB colors? I’m not familiar with that. But if there’s a way that makes sense to render it to the terminal, and isn’t too complicated, I’m open to it.

@lazytype
Copy link
Contributor

I looked at what some terminal coloring crates do to support RGB and it seems like they just approximate it. Here's a crate https://crates.io/crates/ansi_colours, and here's one implementation I found that's only a few lines long https://github.com/ChrisB9/terminal_color_builder/blob/main/src/color.rs#L146-L152

@maxbrunsfeld
Copy link
Contributor

only a few lines long

oh yeah, that level of complexity is totally fine by me 👍 . That seems reasonable for this purpose.

@ahlinc
Copy link
Contributor

ahlinc commented Feb 27, 2021

@maxbrunsfeld As I know last several years there happen wide adoption of 24bit true colors in plenty of terminals.
There is a gist page https://gist.github.com/XVilka/8346728 were the adoption progress is tracked.
On the gist there is a description how to detect truecolors support in terminals and in case of no support a fallback to 256 ansi_colors through mentioned crate would be a good option.
@lazytype It seems there is a better crate https://crates.io/crates/colored that have 24bit true colors support.

@lazytype
Copy link
Contributor

It seems like the colored crate does not yet try to approximate the closest color if true colors is not supported. There's an open PR for this here colored-rs/colored#90.

But it also turns out that the crate that tree-sitter is currently using, ansi_term also supports true colors https://github.com/ogham/rust-ansi-term/pull/13/files, I modified my local clone of tree-sitter to use it and verified that it works in iterm2. In the default macOS Terminal app, the color will just be rendered as a gray. So I could send a PR for the local changes I have or also add support for approximating the color in ANSI.

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

Successfully merging a pull request may close this issue.

4 participants