ogham / rust-ansi-term

Rust library for ANSI terminal colours and styles (bold, underline)

Home Page:https://crates.io/crates/ansi_term

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

allow arbitrary `Display`/`Debug` types in `paint`

yaahc opened this issue · comments

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))?;
        }