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

Support HTML output #74

Open
oovm opened this issue May 25, 2023 · 4 comments
Open

Support HTML output #74

oovm opened this issue May 25, 2023 · 4 comments

Comments

@oovm
Copy link

oovm commented May 25, 2023

Is there any way to render output as html?

I want to render error report in jupyter, but if read stderr directly to it will look like this:

image

@zesterer
Copy link
Owner

There is currently no support for HTML output. Always happy to accept PRs though!

If you want something a little more sensible, you can run it through an ANSI escape sequence stripped like this and then just write it out as text with a monospace font.

@oovm
Copy link
Author

oovm commented Jun 1, 2023

I achieved the output html in my fork by changing the formatting here

write!(f, "{}", Paint::new(&self.0).fg(col))

write!(f, "<span style=\color: "{:x}\">{}</span>", col, Paint::new(&self.0).fg(col))

But the original stderr write can't be used, here should provide an injection mechanism.

And I feel that the Color function of yansi is not as rich as that of termcolor, maybe a generic can be added here?

@oovm
Copy link
Author

oovm commented Jun 1, 2023

I thought of a mechanism like this to support different output formats:

trait SpanFormatter {
    fn write_lhs(&self, w: impl Write) -> std::fmt::Result;
    fn write_rhs(&self, w: impl Write) -> std::fmt::Result;
}

impl SpanFormatter for Color {
    fn write_lhs(&self, w: impl Write) -> std::fmt::Result {
        // Write the ANSI escape code for this color.
        write!("\x1b[38;2;{};{};{}m", self.r, self.g, self.b)
    }

    fn write_rhs(&self, w: impl Write) -> std::fmt::Result {
        // recover the default colour
        write!(w, "\x1b[0m")
    }
}

impl SpanFormatter for HTML {
    fn write_lhs(&self, w: impl Write) -> std::fmt::Result {
        // Write the HTML span tag for this color.
        write!(w, "<span style=\"color: #{:x}\">", self.0)
    }

    fn write_rhs(&self, w: impl Write) -> std::fmt::Result {
        // recover the default colour
        write!(w, "</span>")
    }
}

One of the more complicated aspects of html is that it also needs to support escape

@max-sixty
Copy link

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

No branches or pull requests

3 participants