8000 Make rendering color agnostic by urisinger · Pull Request #387 · brendanzab/codespan · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Make rendering color agnostic #387

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open

Conversation

urisinger
Copy link
@urisinger urisinger commented Jun 18, 2025

Resolves #386

This adds a new WriteStyle trait for styling that replaces WriteColor.

Right now, the only public APIs I’ve changed are the ones for Config and emit: I removed Style from Config (because a renderer doesn’t always need one) and added a style parameter to emit.

Using the API is very simple, just implement the WriteStyle trait, and then pass your writer to the renderer. I’ve implemented a minimal HTML backend here, based on readme_preview.rs.

@kaleidawave kaleidawave added the enhancement New feature or request label Jun 18, 2025
@kaleidawave
Copy link
Collaborator
kaleidawave commented Jun 18, 2025

Very cool! I like the HTML backend, can see myself using that.

Had a quick look, will have a closer look later in week. Before it is merged I think it needs some fixes after running cargo check --no-default-features (apologies the CI on this repo is not great)

@urisinger
Copy link
Author

im trying to run the tests and im getting an error in anyhow,

error[E0599]: no method named `backtrace` found for reference `&(dyn std::error::Error + Send + Sync + 'st
atic)` in the current scope
  --> /home/urisig/.cargo/registry/src/index.crates.io-1949cf8c6b5b557f/anyhow-1.0.0/src/error.rs:159:44
   |
159 |             .or_else(|| self.inner.error().backtrace())
   |                                            ^^^^^^^^^ method not found in `&dyn Error + Send + Sync```

@kaleidawave
Copy link
Collaborator

Oooh I have run into that issue before... I don't think it was on this repository and I cannot remember how I fixed it. I can only think: double check you are on the latest and run cargo build etc with --locked

8000

@urisinger
Copy link
Author

Oooh I have run into that issue before... I don't think it was on this repository and I cannot remember how I fixed it. I can only think: double check you are on the latest and run cargo build etc with --locked

works now, even managed to built the example svg: example svg

@urisinger
Copy link
Author

tests are now passing. should this be locked behind the termcolor feature just like before? I think i might be able to get this to work without it.

@urisinger
Copy link
Author
urisinger commented Jun 18, 2025

@kaleidawave the pr is ready to review. Although im not quite sure about the changes to the api yet, the emit function requires a bit more boilerplate to use now.

Im also thinking of making the html renderer part of the crate, its a very simple renderer that can be quite useful

@@ -104,51 +108,51 @@ pub enum DisplayStyle {
pub struct Styles {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why these fields are no longer public?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i used it to refactor and forgot to make it public, they definitely should be public


#[cfg(feature = "termcolor")]
#[cfg(feature = "std")]
impl<'a, W: WriteColor> io::Write for StylesWriter<'a, W> {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! So this impl ensures all existing uses will not break and this update will be backwards compatible?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, you will have to create a StylesWriter instead of a ColorWriter, but its just a two line change with

let styles = Styles::default();
let styles_writer = StylesWriter::new(writer, &styles);

.unwrap();
let style = codespan_reporting::term::Styles::default();
let color_writer = StandardStream::stdout(ColorChoice::Auto);
let mut writer = codespan_reporting::term::StylesWriter::new(color_writer.lock(), &style);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason why the writer has changed. StandardStream::stdout(ColorChoice::Auto) should still work right? Want to make sure that it doesn't create breaking changes

@kaleidawave
Copy link
Collaborator

Looks good, have a few queries but after they are resolved and I have tested this on one of my own projects I will merge.

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

Successfully merging this pull request may close these issues.

Consider making rendering color agnostic
2 participants
0