Skip to content

Reduce verbosity in getting the final color for a stream #91

Open
@pksunkara

Description

With the updated crates and API:

    // I read the color flag the user specified in the cli arguments and default it for my program
    program.color.write_global();

    // I get the final result for the `stdout` stream of whether to show color or not
    let should_color = match anstream::AutoStream::choice(&std::io::stdout()) {
        anstream::ColorChoice::Auto => unreachable!(),
        anstream::ColorChoice::AlwaysAnsi => true,
        anstream::ColorChoice::Always => true,
        anstream::ColorChoice::Never => false,
    };

    // I tell the logger to show color
    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer()
                .with_ansi(should_color)
        )
        .init();

Before, it used to be:

    program.color.write_global();

    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer()
                .with_ansi(get(Stream::Stdout).color())
        )
        .init();

I think we can make it simpler similarly:

    program.color.write_global();

    tracing_subscriber::registry()
        .with(
            tracing_subscriber::fmt::layer()
                .with_ansi(stdout().color())
        )
        .init();

All we have to do is add pub fn color(&self) -> bool to anstream::AutoStream

Metadata

Assignees

No one assigned

    Labels

    A-streamArea: anstreamC-enhancementCategory: Raise on the bar on expectations

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions