Git Product home page Git Product logo

ansiterm-clone's Issues

Complex formatting settings are ignored #59

ogham/rust-ansi-term#59

I'm trying to migrate my project from using colored to ansi_term, as one of my dependencies already uses the latter. I was expecting this to be a straightforward replacement, but faced an issue: the Display implementation in ansi_term seems to ignore the formatting settings, e.g. with the following code

const DIGITS: &'static str = "1234567890";

fn main() { 
    let s = Color::Red.paint(DIGITS);
    println!("{:.5}", s);
}

the expected behavior is: the provided format string is used; red string 12345 is printed;
the actual behavior is: the formatting settings are ignored; red string 1234567890 is printed.

Is this intentional, or a bug?

rustc: 1.38.0
ansi_term: 0.12.1

This issue looks pretty hairy, although there does seem to be commits/ merged PR's in other forks that we can look into to see how it is solved.

allow arbitrary Display/Debug types in paint #64

ogham/rust-ansi-term#64

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

No Color support

I see many projects using this library, and I don't see simple way to disable colored output for those apps (without code modification). In production environments I like to grep easily logs and colors are making it super difficult.

More info about NO_COLOR environment variable at https://no-color.org/

This is a valid issue, and should be looked into

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.