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

style::force_color_output doesn't disable attributes like bold #921

Open
rtbo opened this issue Aug 26, 2024 · 0 comments
Open

style::force_color_output doesn't disable attributes like bold #921

rtbo opened this issue Aug 26, 2024 · 0 comments

Comments

@rtbo
Copy link

rtbo commented Aug 26, 2024

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 added a commit to rtbo/crossterm that referenced this issue Aug 28, 2024
rtbo added a commit to rtbo/crossterm that referenced this issue Aug 28, 2024
rtbo added a commit to rtbo/crossterm that referenced this issue Aug 28, 2024
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