Skip to content

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.

@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

@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?

.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

Copy link
Author

@urisinger urisinger Jun 21, 2025

Choose a reason for hiding this comment

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

I provided a blanket impl for ColorWriter that should fix prevent this from being a very big change, but if you use custom styles you will still need to provide the styles with StylesWriter instead of passing it in the config. This definitely needs to be a version bump since it removes the styles field from Config.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Awesome, when I have the time I will investigate whether some of the dependents of this project require any changes.

Copy link
Author

@urisinger urisinger Jun 21, 2025

Choose a reason for hiding this comment

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

Awesome, when I have the time I will investigate whether some of the dependents of this project require any changes.

Great, we can also add the html/svg renderer as part of the package or at least include it in the readme.

@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.

@urisinger
Copy link
Author

@kaleidawave any progress?

@kaleidawave
Copy link
Collaborator

Hi @urisinger , apologies have been busy recently.

As I have only recently become a maintainer and still don't have a lot of knowledge of the codebase and this change is breaking I have put this on the back-burner.

I have found a few changes that would be needed in external repositories, I will add some more as I test others

gfx-rs/wgpu naga/src/error.rs:50:17 & naga/src/span.rs:330:20
bevyengine/naga_oil src/compose/error.rs:295:20

But other repositories: dtolnay/cxx and EmbarkStudios/toml-span already work 🎉


I can merge this soon, but need 3 things:

  1. I have a few things to add to my change to CI PR Update and improve CI and testing #388 but it should be ready soon? If this PR passes under that I will be more confident about merging it.
  2. To investigate whether this PR can fix term::emit's writer argument does not behave additively wrt features. #389 (or be modified not to run into the current issue)?
  3. For a short piece of text on what users of the library need to modify when updating. Something I can put into the changelog/release notes for 0.13.0 update

Thanks!

@urisinger
Copy link
Author

urisinger commented Jul 16, 2025

Hi @urisinger , apologies have been busy recently.

As I have only recently become a maintainer and still don't have a lot of knowledge of the codebase and this change is breaking I have put this on the back-burner.

I have found a few changes that would be needed in external repositories, I will add some more as I test others

gfx-rs/wgpu naga/src/error.rs:50:17 & naga/src/span.rs:330:20
bevyengine/naga_oil src/compose/error.rs:295:20

But other repositories: dtolnay/cxx and EmbarkStudios/toml-span already work 🎉

I can merge this soon, but need 3 things:

1. I have a few things to add to my change to CI PR [Update and improve CI and testing #388](https://github.com/brendanzab/codespan/pull/388) but it should be ready soon? If this PR passes under that I will be more confident about merging it.

2. To investigate whether this PR can fix [`term::emit`'s `writer` argument does not behave additively wrt features. #389](https://github.com/brendanzab/codespan/issues/389) (or be modified not to run into the current issue)?

3. For a short piece of text on what users of the library **need to modify** when updating. Something I can put into the changelog/release notes for `0.13.0` update

Thanks!

this commit fixed #389, it provided a blaket implementation of StylesWriter for ColorWriter that uses the default Color config. It might be possible to implement StylesWriter for a regular Writer too if the term feature is off.

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