Skip to content

Commit a877a73

Browse files
authored
Merge pull request #136 from epage/anstream
feat!: Always print color when color is enabled
2 parents 9777b50 + 13baa4c commit a877a73

File tree

18 files changed

+169
-160
lines changed

18 files changed

+169
-160
lines changed

.clippy.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
msrv = "1.60.0" # MSRV
1+
msrv = "1.64.0" # MSRV

.github/workflows/ci.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,15 @@ jobs:
5151
- name: No-default features
5252
run: cargo test --workspace --no-default-features
5353
msrv:
54-
name: "Check MSRV: 1.60.0"
54+
name: "Check MSRV: 1.64.0"
5555
runs-on: ubuntu-latest
5656
steps:
5757
- name: Checkout repository
5858
uses: actions/checkout@v3
5959
- name: Install Rust
6060
uses: actions-rs/toolchain@v1
6161
with:
62-
toolchain: 1.60.0 # MSRV
62+
toolchain: 1.64.0 # MSRV
6363
profile: minimal
6464
override: true
6565
- uses: Swatinem/rust-cache@v2
@@ -113,7 +113,7 @@ jobs:
113113
- name: Install Rust
114114
uses: actions-rs/toolchain@v1
115115
with:
116-
toolchain: 1.60.0 # MSRV
116+
toolchain: 1.64.0 # MSRV
117117
profile: minimal
118118
override: true
119119
components: clippy

Cargo.toml

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,36 @@
11
[workspace]
22
members = ["crates/*"]
3+
resolver = "2"
4+
5+
[workspace.package]
6+
license = "MIT OR Apache-2.0"
7+
edition = "2021"
8+
rust-version = "1.64.0" # MSRV
9+
include = [
10+
"build.rs",
11+
"src/**/*",
12+
"Cargo.toml",
13+
"LICENSE*",
14+
"README.md",
15+
"benches/**/*",
16+
"examples/**/*"
17+
]
318

419
[package]
520
name = "predicates"
621
version = "2.1.5"
722
description = "An implementation of boolean-valued predicate functions."
823
authors = ["Nick Stevens <nick@bitcurry.com>"]
9-
license = "MIT OR Apache-2.0"
1024
repository = "https://github.com/assert-rs/predicates-rs"
1125
homepage = "https://github.com/assert-rs/predicates-rs"
1226
documentation = "https://docs.rs/predicates"
1327
readme = "README.md"
1428
categories = ["data-structures", "rust-patterns"]
1529
keywords = ["predicate", "boolean", "combinatorial", "match", "logic"]
16-
edition = "2021"
17-
rust-version = "1.60.0" # MSRV
18-
include = [
19-
"src/**/*",
20-
"Cargo.toml",
21-
"LICENSE*",
22-
"README.md",
23-
"examples/**/*"
24-
]
30+
license.workspace = true
31+
edition.workspace = true
32+
rust-version.workspace = true
33+
include.workspace = true
2534

2635
[package.metadata.release]
2736
pre-release-replacements = [
@@ -41,15 +50,13 @@ normalize-line-endings = { version = "0.3.0", optional = true }
4150
regex = { version="1.0", optional = true }
4251
float-cmp = { version="0.9", optional = true }
4352
itertools = "0.10"
44-
yansi = { version = "0.5.1", optional = true }
45-
concolor = { version = "0.0.12", optional = true }
53+
anstyle = "0.3.0"
4654

4755
[dev-dependencies]
4856
predicates-tree = { version = "1.0", path = "crates/tree" }
4957

5058
[features]
51-
default = ["diff", "regex", "float-cmp", "normalize-line-endings"]
59+
default = ["diff", "regex", "float-cmp", "normalize-line-endings", "color"]
5260
diff = ["dep:difflib"]
5361
unstable = []
54-
color = ["dep:yansi", "dep:concolor", "concolor?/std"]
55-
color-auto = ["color", "concolor?/auto"]
62+
color = []

crates/core/Cargo.toml

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,13 @@ name = "predicates-core"
33
version = "1.0.5"
44
description = "An API for boolean-valued predicate functions."
55
authors = ["Nick Stevens <nick@bitcurry.com>"]
6-
license = "MIT OR Apache-2.0"
76
readme = "README.md"
87
repository = "https://github.com/assert-rs/predicates-rs/tree/master/crates/core"
98
homepage = "https://github.com/assert-rs/predicates-rs/tree/master/crates/core"
109
documentation = "https://docs.rs/predicates-core"
1110
categories = ["data-structures", "rust-patterns"]
1211
keywords = ["predicate", "boolean", "combinatorial", "match", "logic"]
13-
edition = "2021"
14-
rust-version = "1.60.0" # MSRV
15-
include = [
16-
"src/**/*",
17-
"Cargo.toml",
18-
"LICENSE*",
19-
"README.md",
20-
"examples/**/*"
21-
]
12+
license.workspace = true
13+
edition.workspace = true
14+
rust-version.workspace = true
15+
include.workspace = true

crates/tree/Cargo.toml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,20 @@ name = "predicates-tree"
33
version = "1.0.7"
44
authors = ["Nick Stevens <nick@bitcurry.com>"]
55
description = "Render boolean-valued predicate functions results as a tree."
6-
license = "MIT OR Apache-2.0"
76
readme = "README.md"
87
repository = "https://github.com/assert-rs/predicates-rs/tree/master/crates/tree"
98
homepage = "https://github.com/assert-rs/predicates-rs/tree/master/crates/tree"
109
documentation = "https://docs.rs/predicates-tree"
1110
categories = ["data-structures", "rust-patterns"]
1211
keywords = ["predicate", "boolean", "combinatorial", "match", "logic"]
13-
edition = "2021"
14-
rust-version = "1.60.0" # MSRV
15-
include = [
16-
"src/**/*",
17-
"Cargo.toml",
18-
"LICENSE*",
19-
"README.md",
20-
"examples/**/*"
21-
]
12+
license.workspace = true
13+
edition.workspace = true
14+
rust-version.workspace = true
15+
include.workspace = true
2216

2317
[dependencies]
2418
predicates-core = { version = "1.0", path = "../core" }
25-
termtree = "0.4"
19+
termtree = "0.4.1"
2620

2721
[dev-dependencies]
28-
predicates = { version = "2.1", path = "../..", features = ["color-auto"] }
22+
predicates = { version = "2.1", path = "../..", features = ["color"] }

src/color.rs

Lines changed: 48 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,69 @@
11
#[derive(Copy, Clone, Debug, Default)]
22
pub(crate) struct Palette {
3-
pub(crate) description: styled::Style,
4-
pub(crate) var: styled::Style,
5-
pub(crate) expected: styled::Style,
3+
description: anstyle::Style,
4+
var: anstyle::Style,
5+
expected: anstyle::Style,
66
}
77

88
impl Palette {
9-
#[cfg(feature = "color")]
10-
pub(crate) fn current() -> Self {
11-
if concolor::get(concolor::Stream::Either).ansi_color() {
9+
pub(crate) fn new(alternate: bool) -> Self {
10+
if alternate && cfg!(feature = "color") {
1211
Self {
13-
description: styled::Style(yansi::Style::new(yansi::Color::Blue).bold()),
14-
var: styled::Style(yansi::Style::new(yansi::Color::Red).bold()),
15-
expected: styled::Style(yansi::Style::new(yansi::Color::Green).bold()),
12+
description: anstyle::AnsiColor::Blue | anstyle::Effects::BOLD,
13+
var: anstyle::AnsiColor::Red | anstyle::Effects::BOLD,
14+
expected: anstyle::AnsiColor::Green | anstyle::Effects::BOLD,
1615
}
1716
} else {
18-
Self::default()
17+
Self::plain()
1918
}
2019
}
2120

22-
#[cfg(not(feature = "color"))]
23-
pub(crate) fn current() -> Self {
24-
Self::default()
21+
pub(crate) fn plain() -> Self {
22+
Self {
23+
description: Default::default(),
24+
var: Default::default(),
25+
expected: Default::default(),
26+
}
27+
}
28+
29+
pub(crate) fn description<D: std::fmt::Display>(self, display: D) -> Styled<D> {
30+
Styled::new(display, self.description)
2531
}
26-
}
2732

28-
#[cfg(feature = "color")]
29-
mod styled {
30-
#[derive(Copy, Clone, Debug, Default)]
31-
pub(crate) struct Style(pub(crate) yansi::Style);
33+
pub(crate) fn var<D: std::fmt::Display>(self, display: D) -> Styled<D> {
34+
Styled::new(display, self.var)
35+
}
3236

33-
impl Style {
34-
pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> impl std::fmt::Display {
35-
self.0.paint(item)
36-
}
37+
pub(crate) fn expected<D: std::fmt::Display>(self, display: D) -> Styled<D> {
38+
Styled::new(display, self.expected)
3739
}
3840
}
3941

40-
#[cfg(not(feature = "color"))]
41-
mod styled {
42-
#[derive(Copy, Clone, Debug, Default)]
43-
pub(crate) struct Style;
42+
#[derive(Debug)]
43+
pub(crate) struct Styled<D> {
44+
display: D,
45+
style: anstyle::Style,
46+
}
4447

45-
impl Style {
46-
pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> impl std::fmt::Display {
47-
item
48+
impl<D: std::fmt::Display> Styled<D> {
49+
pub(crate) fn new(display: D, style: anstyle::Style) -> Self {
50+
Self { display, style }
51+
}
52+
}
53+
54+
impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
55+
#[inline]
56+
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
57+
if f.alternate() {
58+
write!(
59+
f,
60+
"{}{}{}",
61+
self.style.render(),
62+
self.display,
63+
self.style.render_reset()
64+
)
65+
} else {
66+
self.display.fmt(f)
4867
}
4968
}
5069
}

src/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ impl reflection::PredicateReflection for BooleanPredicate {
4141

4242
impl fmt::Display for BooleanPredicate {
4343
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44-
let palette = crate::Palette::current();
45-
write!(f, "{}", palette.expected.paint(self.retval))
44+
let palette = crate::Palette::new(f.alternate());
45+
write!(f, "{}", palette.expected(self.retval))
4646
}
4747
}
4848

src/float/close.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,13 @@ impl reflection::PredicateReflection for IsClosePredicate {
124124

125125
impl fmt::Display for IsClosePredicate {
126126
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
127-
let palette = crate::Palette::current();
127+
let palette = crate::Palette::new(f.alternate());
128128
write!(
129129
f,
130130
"{} {} {}",
131-
palette.var.paint("var"),
132-
palette.description.paint("!="),
133-
palette.expected.paint(self.target),
131+
palette.var("var"),
132+
palette.description("!="),
133+
palette.expected(self.target),
134134
)
135135
}
136136
}

src/function.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use crate::Predicate;
1717

1818
/// Predicate that wraps a function over a reference that returns a `bool`.
1919
/// This type is returned by the `predicate::function` function.
20+
#[allow(clippy::derive_partial_eq_without_eq)]
2021
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
2122
pub struct FnPredicate<F, T>
2223
where
@@ -96,12 +97,12 @@ where
9697
T: ?Sized,
9798
{
9899
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99-
let palette = crate::Palette::current();
100+
let palette = crate::Palette::new(f.alternate());
100101
write!(
101102
f,
102103
"{}({})",
103-
palette.description.paint(self.name),
104-
palette.var.paint("var"),
104+
palette.description(self.name),
105+
palette.var("var"),
105106
)
106107
}
107108
}

src/iter.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,13 @@ where
109109
T: PartialEq + fmt::Debug,
110110
{
111111
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112-
let palette = crate::Palette::current();
112+
let palette = crate::Palette::new(f.alternate());
113113
write!(
114114
f,
115115
"{} {} {}",
116-
palette.var.paint("var"),
117-
palette.description.paint("in"),
118-
palette.expected.paint("values")
116+
palette.var("var"),
117+
palette.description("in"),
118+
palette.expected("values")
119119
)
120120
}
121121
}
@@ -218,13 +218,13 @@ where
218218
T: Ord + fmt::Debug,
219219
{
220220
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
221-
let palette = crate::Palette::current();
221+
let palette = crate::Palette::new(f.alternate());
222222
write!(
223223
f,
224224
"{} {} {}",
225-
palette.var.paint("var"),
226-
palette.description.paint("in"),
227-
palette.expected.paint("values")
225+
palette.var("var"),
226+
palette.description("in"),
227+
palette.expected("values")
228228
)
229229
}
230230
}
@@ -280,13 +280,13 @@ where
280280
T: Hash + Eq + fmt::Debug,
281281
{
282282
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
283-
let palette = crate::Palette::current();
283+
let palette = crate::Palette::new(f.alternate());
284284
write!(
285285
f,
286286
"{} {} {}",
287-
palette.var.paint("var"),
288-
palette.description.paint("in"),
289-
palette.expected.paint("values")
287+
palette.var("var"),
288+
palette.description("in"),
289+
palette.expected("values")
290290
)
291291
}
292292
}

0 commit comments

Comments
 (0)