Skip to content

Commit

Permalink
restore compatibility with supports-color v2
Browse files Browse the repository at this point in the history
  • Loading branch information
sunshowers authored and jam1garner committed Sep 10, 2024
1 parent 9d3f1b1 commit c06d445
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ name = "override"
required-features = ["supports-colors"]

[features]
supports-colors = ["supports-color"]
# Enable conversions from all versions of supports-color, but only use the latest to determine if
# colors are actually supported.
#
# Don't use dep:supports-color because it's possible there's a broken crate in the wild which uses
# the nonfunctional "supports-color" feature.
supports-colors = ["dep:supports-color-2", "supports-color"]
alloc = []

[dependencies]
supports-color-2 = { package = "supports-color", version = "2.0", optional = true }
supports-color = { version = "3.0.0", optional = true }
34 changes: 30 additions & 4 deletions src/supports_colors.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
use core::fmt;

mod private {
pub(super) trait Sealed {}
}

#[cfg(feature = "supports-colors")]
/// A display wrapper which applies a transformation based on if the given stream supports
/// colored terminal output
Expand Down Expand Up @@ -39,6 +35,15 @@ impl From<supports_color::Stream> for Stream {
}
}

impl From<supports_color_2::Stream> for Stream {
fn from(stream: supports_color_2::Stream) -> Self {
match stream {
supports_color_2::Stream::Stdout => Self::Stdout,
supports_color_2::Stream::Stderr => Self::Stderr,
}
}
}

macro_rules! impl_fmt_for {
($($trait:path),* $(,)?) => {
$(
Expand Down Expand Up @@ -82,3 +87,24 @@ impl_fmt_for! {
fmt::Octal,
fmt::Pointer,
}

#[cfg(test)]
mod test {
use crate::OwoColorize;

use super::*;

#[test]
fn test_supports_color_versions() {
println!(
"{}",
"This might be green"
.if_supports_color(supports_color_2::Stream::Stdout, |x| x.green())
);

println!(
"{}",
"This might be red".if_supports_color(supports_color::Stream::Stdout, |x| x.red())
);
}
}

0 comments on commit c06d445

Please sign in to comment.