crossterm icon indicating copy to clipboard operation
crossterm copied to clipboard

`style::force_color_output` doesn't disable attributes like bold

Open rtbo opened this issue 1 year ago • 0 comments
trafficstars

Describe the bug I want to use style::force_color_output to disable ANSI escape codes entirely, for the case the output stream is redirected to a file.

The colors are disabled, but not attributes such as bold. As a result, some ANSI escape codes are leaked into the file.

To Reproduce

Consider this function

pub fn print_diagnostic(err: &tc::Error, indent: u32) -> io::Result<()> {
    let span = err.span();

    let disabled = if !io::stderr().is_terminal() {
        // check NO_COLOR status to reinitialize the correct value afterwards
        let disabled = Colored::ansi_color_disabled();
        style::force_color_output(false);
        Some(disabled)
    } else {
        None
    };

    eprintln!(
        "{}{}",
        " ".repeat((indent + span.0) as _),
        "^".repeat((span.1 - span.0) as _).red().bold()
    );
    eprintln!("{}: {}", "error".red().bold(), err.to_string().bold());

    if let Some(disabled) = disabled {
        Colored::set_ansi_color_disabled(disabled);
    }

    io::stderr().flush()
}

OS

  • e.g. Windows

Terminal/Console

  • Windows Terminal, VsCode integrated terminal

rtbo avatar Aug 26 '24 15:08 rtbo