diff --git a/crates/anstyle/src/style.rs b/crates/anstyle/src/style.rs index 62dc8d59..9ba8105c 100644 --- a/crates/anstyle/src/style.rs +++ b/crates/anstyle/src/style.rs @@ -83,6 +83,13 @@ impl Style { pub fn render(self) -> impl core::fmt::Display { StyleDisplay(self) } + + /// Renders the relevant [`Reset`][crate::Reset] code + /// + /// Unlike [`Reset::render`][crate::Reset::render], this will elide the code if there is nothing to reset. + pub fn render_reset(self) -> impl core::fmt::Display { + ResetDisplay(self != Self::new()) + } } /// # Convenience @@ -409,3 +416,15 @@ fn separator(f: &mut core::fmt::Formatter<'_>, first: &mut bool) -> core::fmt::R write!(f, ";") } } + +struct ResetDisplay(bool); + +impl core::fmt::Display for ResetDisplay { + fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { + if self.0 { + write!(f, "\x1B[0m") + } else { + Ok(()) + } + } +}