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 for WASM #169

Open
hseager opened this issue Apr 15, 2024 · 2 comments
Open

Support for WASM #169

hseager opened this issue Apr 15, 2024 · 2 comments

Comments

@hseager
Copy link

hseager commented Apr 15, 2024

Firstly, thanks for the crate!

Would it be possible to get this crate working when building for WASM?

I've built a text based adventure game which I'm also building for WASM and it's a shame for the colored text not to work: https://github.com/hseager/you-are-merlin

I thought I'd ask for some advice before diving in and to see if it's a terrible idea or not as I apprectiate this crate is built with the terminal in mind. Thanks!

@hseager
Copy link
Author

hseager commented Apr 20, 2024

I've created a wrapper module which detects WASM builds and isn't perfect, but is working well enough for my needs.

pub trait TextFormatter {
    fn format_bold(self) -> String;
}

impl TextFormatter for &str {
    #[cfg(target_arch = "wasm32")]
    fn format_bold(self) -> String {
        format!("<span style='font-weight: bold;'>{}</span>", self)
    }

    #[cfg(not(target_arch = "wasm32"))]
    fn format_bold(self) -> String {
        use colored::Colorize;

        self.bold().to_string()
    }
}

I wonder if there would be interest in integrating this into this crate, or if it better separated out into another crate.

@spenserblack
Copy link
Collaborator

IMO a tool that does what you're asking, wrapping text in <span style="STYLE"></span>, should be a separate crate. This could also be useful even when not used in WASM. For example, a server rendering an HTML response.

BTW, such a tool should probably use the appropriate HTML elements. As a usage example, perhaps something like this:

assert_eq!("foo".strong().style("color: red;").to_string(), "<strong style=\"color: red;\">foo</strong>");

This could be achieved, for example, if str implements fn strong(&self) -> SomeHTMLType and SomeHTMLType implements fn style(mut self, style: SomeStyleType) -> Self.

You might want to check if a crate that does this already exists, because this usage I'm making up does feel vaguely familiar.

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

2 participants