Use anstream to avoid writing colorized output (#415)

A more robust solution to avoiding colorized output by ensuring we write
to `stdout` and `stderr` via the
[`anstream`](https://docs.rs/anstream/latest/anstream/) crate.

Closes https://github.com/astral-sh/puffin/issues/393.
This commit is contained in:
Charlie Marsh
2023-11-13 12:00:12 -08:00
committed by GitHub
parent 76a41066ac
commit 0af2f7e39f
7 changed files with 17 additions and 14 deletions
+4 -12
View File
@@ -1,13 +1,13 @@
use anstream::AutoStream;
use std::borrow::Cow;
use std::fmt::Write;
use std::io::{stdout, BufWriter};
use std::io::stdout;
use std::path::Path;
use std::str::FromStr;
use std::{env, fs};
use anyhow::{anyhow, Result};
use colored::Colorize;
use fs_err::File;
use itertools::Itertools;
use tracing::debug;
@@ -182,14 +182,10 @@ pub(crate) async fn pip_compile(
.dimmed()
)?;
if output_file.is_some() {
colored::control::set_override(false);
}
let mut writer: Box<dyn std::io::Write> = if let Some(output_file) = output_file {
Box::new(BufWriter::new(File::create(output_file)?))
Box::new(AutoStream::auto(fs::File::create(output_file)?))
} else {
Box::new(stdout())
Box::new(AutoStream::auto(stdout()))
};
writeln!(
@@ -205,10 +201,6 @@ pub(crate) async fn pip_compile(
)?;
write!(writer, "{resolution}")?;
if output_file.is_some() {
colored::control::unset_override();
}
Ok(ExitStatus::Success)
}
+2 -1
View File
@@ -1,3 +1,4 @@
use anstream::eprint;
use indicatif::ProgressDrawTarget;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
@@ -26,7 +27,7 @@ impl std::fmt::Write for Printer {
fn write_str(&mut self, s: &str) -> std::fmt::Result {
match self {
Self::Default | Self::Verbose => {
#[allow(clippy::print_stderr)]
#[allow(clippy::print_stderr, clippy::ignored_unit_patterns)]
{
eprint!("{s}");
}