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

draft: try to make Pretty's field formatter respect the writer's ansi setting #1747

Merged
Merged
33 changes: 28 additions & 5 deletions tracing-subscriber/src/fmt/format/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,17 @@ pub struct PrettyVisitor<'a> {
/// [`MakeVisitor`]: crate::field::MakeVisitor
#[derive(Debug)]
pub struct PrettyFields {
ansi: bool,
/// A value to override the provided `Writer`'s ANSI formatting
/// configuration.
///
/// If this is `Some`, we override the `Writer`'s ANSI setting. This is
/// necessary in order to continue supporting the deprecated
/// `PrettyFields::with_ansi` method. If it is `None`, we don't override the
/// ANSI formatting configuration (because the deprecated method was not
/// called).
// TODO: when `PrettyFields::with_ansi` is removed, we can get rid
// of this entirely.
ansi: Option<bool>,
}

// === impl Pretty ===
Expand Down Expand Up @@ -247,21 +257,34 @@ impl Default for PrettyFields {
impl PrettyFields {
/// Returns a new default [`PrettyFields`] implementation.
pub fn new() -> Self {
Self { ansi: true }
// By default, don't override the `Writer`'s ANSI colors
// configuration. We'll only do this if the user calls the
// deprecated `PrettyFields::with_ansi` method.
Self { ansi: None }
}

/// Enable ANSI encoding for formatted fields.
#[deprecated(
since = "0.3.3",
note = "Use `fmt::Subscriber::with_ansi` or `fmt::Collector::with_ansi` instead."
)]
pub fn with_ansi(self, ansi: bool) -> Self {
Self { ansi, ..self }
Self {
ansi: Some(ansi),
..self
}
}
}

impl<'a> MakeVisitor<Writer<'a>> for PrettyFields {
type Visitor = PrettyVisitor<'a>;

#[inline]
fn make_visitor(&self, target: Writer<'a>) -> Self::Visitor {
PrettyVisitor::new(target.with_ansi(self.ansi), true)
fn make_visitor(&self, mut target: Writer<'a>) -> Self::Visitor {
if let Some(ansi) = self.ansi {
target = target.with_ansi(ansi);
}
PrettyVisitor::new(target, true)
}
}

Expand Down