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

allow arbitrary Display/Debug types in paint #64

Open
yaahc opened this issue May 6, 2020 · 0 comments
Open

allow arbitrary Display/Debug types in paint #64

yaahc opened this issue May 6, 2020 · 0 comments

Comments

@yaahc
Copy link

yaahc commented May 6, 2020

Right now the paint function on Style and Color only accepts

I where
    I: Into<Cow<'a, S>>,
    <S as ToOwned>::Owned: Debug,

But in the documents it notes that the paint function doesn't do allocations and return the string with the color applied, instead it returns a Display type which wraps the original string. It seems like this restriction could be relaxed without making a breaking change.

Ideally I'd like it to return a type that impls Display if the inner type impls display, and impls Debug if the inner type impls Debug, which would let me write code like this:

                write!(
                    f,
                    "\n{}",
                    Style::new().bold().paint(format_args!(
                        "{:>8} > {}",
                        cur_line_no,
                        line.unwrap()
                    ))
                )?;

instead of what I have to write right now

                write!(
                    f,
                    "\n{:>8}{}{}",
                    bold.paint(cur_line_no.to_string()),
                    bold.paint(" > "),
                    bold.paint(line.unwrap())
                )?;

It would also make it easier to work with error reporting:

        for (n, error) in errors {
            writeln!(f)?;
            write!(Indented::numbered(f, n), "{}", Red.paint(error))?;
        }

instead of:

        let mut buf = String::new();
        for (n, error) in errors {
            writeln!(f)?;
            buf.clear();
            write!(&mut buf, "{}", error).unwrap();
            write!(Indented::numbered(f, n), "{}", Red.paint(&buf))?;
        }
@yaahc yaahc changed the title allow arbitrary Display types in paint allow arbitrary Display/Debug types in paint May 6, 2020
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

1 participant