Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions crates/anstyle/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(())
}
}
}