-
-
Notifications
You must be signed in to change notification settings - Fork 118
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
add syntax highlighting support with syntect crate #313
Conversation
468a7a1
to
ba65bae
Compare
A bunch of tests currently fail with |
This is super cool! For the tests issue, I'm wondering if this should be hooked up with Similarly, it looks like this PR uses truecolor for the syntax highlighting, while the default theme for the rest of the output uses ANSI escapes (see #176 for the rationale). If it's feasible, it's probably best if this was consistent. |
Good points. This should definitely be changed to work with whatever color
settings are configured, and that should fix most of the test issues.
Is there a way to map truecolor to ANSI? Currently it's just converting the
RGB values from the syntect Style to an RGB in owo-colors Style (and doing
some alpha channel blending if a background is present, but I currently
have background colors off)
…On Fri, Nov 3, 2023, 6:55 PM Benjamin Lee ***@***.***> wrote:
This is super cool!
For the tests issue, I'm wondering if this should be hooked up with
GraphicalTheme somehow. Currently, the ANSI codes generally aren't an
issue in tests because the tests use GraphicalTheme::unicode_nocolor.
Even outside of tests, I would find it a bit surprising if specifying
unicode_nocolor still results in color output by default. Unfortunately,
I'm not sure there's a backwards-compatible way to do this, since
GraphicalTheme and ThemeStyles both have exhaustive public members.
Similarly, it looks like this PR uses truecolor for the syntax
highlighting, while the default theme for the rest of the output uses ANSI
escapes (see #176 <#176> for the
rationale). If it's feasible, it's probably best if this was consistent.
—
Reply to this email directly, view it on GitHub
<#313 (comment)>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AALVTBETZEQ5S5O6OGWJFJ3YCVY6DAVCNFSM6AAAAAA6476G2GVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOJTGIYTIMJSGY>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Oof, that's gonna be tricky. There is no straightforward way to map RGB to 16-color ANSI because the actual RGB values of the ANSI colors will vary arbitrarily depending on how the terminal emulator is configured. This information is not exposed to a terminal application. Ideally, you'd have the style information from the highlighter stored in a form that can be converted reasonably to both truecolor and ANSI. A common way to do this is to have your "theme" specify a separate ANSI value and truecolor value for each type of highlight. This is effectively what miette does currently. Another common approach is to specify only ANSI in the theme, and then hardcode a mapping from ANSI to truecolor with a reasonable color scheme. The latter loses a bit of flexibility (for example, if you want to have more distinct colors in your truecolor output than ANSI supports). Skimming through the syntect docs, it looks like it does neither of these, and instead just uses truecolor directly in the theme. I think getting good ANSI output is either gonna require one of:
I could be missing something, but this looks like a really thorny problem. My opinion is that it's probably not worth blocking the syntax highlighting support in miette on solving it, particularly if syntax highlighting is not enabled by default. |
src/highlighters/syntect.rs
Outdated
|
||
impl<'h> HighlighterState for SyntectHighlighterState<'h> { | ||
fn highlight_line<'s>(&mut self, line: &'s str) -> Vec<Styled<&'s str>> { | ||
let use_bg_color = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might want to expose this as an option somewhere?
src/handlers/graphical.rs
Outdated
.chars() | ||
.zip(self.line_visual_char_width(&styled_text)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't zip the same value in a loop.
Create a single variable before loop, instead
Added a comment to the issue with a gist of what the Theme struct looks like. #67 (comment) Maybe we can come up with a way to map |
With the help of With that change, I can now add I also renamed |
src/named_source.rs
Outdated
@@ -27,11 +27,6 @@ impl NamedSource { | |||
} | |||
} | |||
|
|||
/// Gets the name of this `NamedSource`. | |||
pub fn name(&self) -> &str { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't notice this, but this is a breaking change because SourceCode::name
has to return an Option<&str>
but NamedSource::name
was returning just &str
. Should we keep this for compatibility even though it shadows the new SourceCode
method?
For testing, I'm thinking it might be a good idea to have most the tests in |
Looks like rustc 1.56 doesn't like the https://github.com/kallisti-dev/miette/actions/runs/6756721685/job/18366498929 Possible fixes here:
I'm leaning toward option 1, if no one objects, even though it makes the feature name a bit more verbose than I wanted. |
fa57777
to
6a80565
Compare
Just tried this and it seems to work for most tests, but breaks the URL tests because a simple ANSI stripping algorithm is too naive. I'd have to do lookahead to properly preserve links while deleting color codes. Personally I'd rather leave the existing tests as nocolor and write new tests with EDIT: the URL tests actually don't care about the presence of ANSI codes, so we could remove the links as well and avoid lookahead. I'd still rather avoid the additional complexity of stripping out ANSI stuff in the tests that were written to expect nocolor, and instead write new tests that are color-aware. |
ab408df
to
5b118b8
Compare
c62d121
to
3a2df73
Compare
Alright, I've added crate-level docs and test cases, so I'm comfortable marking this PR as ready. Some lingering questions to resolve are:
Some items that could probably be new issues to indicate future work should be done:
Some notes on CI stuff:
|
6c87bcd
to
fded543
Compare
Moving the language detection stuff over to |
looks like we have some conflicts that need resolved, and a minimal version check issue? |
…ame)" This reverts commit 9f18e69.
bde5ccc
to
8b2407e
Compare
just resolved the merge conflict. 1d4967d fixes the CI error, but does so by excluding the as for the CI failure, the command |
(non-)update: I have not had time to investigate the CI failures with gcc on this change, but plan to soon. I think the best path forward with the MSRV issue is keep the current MSRV but exclude the I think for Miri it might be best to just not test |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, let's get this in and see how it goes! :)
To do after finalizing design: