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

Use the alternate formatting macros from color_print. #461

Open
iago-lito opened this issue Jul 24, 2024 · 2 comments
Open

Use the alternate formatting macros from color_print. #461

iago-lito opened this issue Jul 24, 2024 · 2 comments

Comments

@iago-lito
Copy link

iago-lito commented Jul 24, 2024

I like how the color_print crate provides the cformat!() macro to statically generate ANSI colored strings.

Would it be an option to enable opt-in into this alternate formatting style for the #[snafu(display(..))] attributes?

Something along:

use color_print::cformat;

#[derive(Debug, Snafu)]
enum Error {
    #[snafu(display(cformat, "<green>{username}</> may not log in until they pay USD <yellow,bold>{amount:E}</>"))]
    UserMustPayForService { username: String, amount: f32 },
}

?

(maybe one condition would be that color_print features cwrite!(), the colored version of write!(), rather than cformat!()?)

@8573
Copy link

8573 commented Jul 24, 2024

Would this mean that (for types opting in) the errors would be displayable only in terminal, and specifically only terminals supporting these color codes?

@iago-lito
Copy link
Author

I guess it would, yes. There is a terminfo feature in this crate to accommodate various terminals, but non-ANSI are explicitly not supported.

Also, I guess that if you want to format!("a string with the {error} message.") without the escape codes inside, you'd need to remove them, or ask that color_print features some API to render without them and turn that into format!("a string with the {} message.", error.unpaint()) or something.

To be honest, I am not 100% sure that color_print is ready for the job yet, or that making it interact with snafu would be free of quirks. But I'm happy to investigate because I like the approach taken by color_print and I wonder how much work it would represent to integrate the two together.

Is the #[snafu(display(..))] attribute deeply entangled with write!(..) / format_args!(..), or could it easily handle alternative formatting macros?

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