Skip to content

Commit 38ad49c

Browse files
committed
feat!: Always print color when color is enabled
This is in anticipation of callers using `anstyle_stream` to print this which will "Do The Right Thing" BREAKING CHANGE: - `color-auto` is gone - `Display`s won't automatically print ANSI color code when appropriate but instead `format!("{}", case)` will never print color and `format!("{:#}", case)` will always print color
1 parent 7a96143 commit 38ad49c

File tree

15 files changed

+135
-124
lines changed

15 files changed

+135
-124
lines changed

Cargo.toml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ normalize-line-endings = { version = "0.3.0", optional = true }
5050
regex = { version="1.0", optional = true }
5151
float-cmp = { version="0.9", optional = true }
5252
itertools = "0.10"
53-
yansi = { version = "0.5.1", optional = true }
54-
concolor = { version = "0.0.12", optional = true }
53+
anstyle = "0.3.0"
5554

5655
[dev-dependencies]
5756
predicates-tree = { version = "1.0", path = "crates/tree" }
@@ -60,5 +59,4 @@ predicates-tree = { version = "1.0", path = "crates/tree" }
6059
default = ["diff", "regex", "float-cmp", "normalize-line-endings"]
6160
diff = ["dep:difflib"]
6261
unstable = []
63-
color = ["dep:yansi", "dep:concolor", "concolor?/std"]
64-
color-auto = ["color", "concolor?/auto"]
62+
color = []

crates/tree/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ include.workspace = true
1616

1717
[dependencies]
1818
predicates-core = { version = "1.0", path = "../core" }
19-
termtree = "0.4"
19+
termtree = "0.4.1"
2020

2121
[dev-dependencies]
22-
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,12 @@ where
9696
T: ?Sized,
9797
{
9898
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
99-
let palette = crate::Palette::current();
99+
let palette = crate::Palette::new(f.alternate());
100100
write!(
101101
f,
102102
"{}({})",
103-
palette.description.paint(self.name),
104-
palette.var.paint("var"),
103+
palette.description(self.name),
104+
palette.var("var"),
105105
)
106106
}
107107
}

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
}

src/name.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,8 @@ where
7575
Item: ?Sized,
7676
{
7777
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
78-
let palette = crate::Palette::current();
79-
write!(f, "{}", palette.description.paint(self.name))
78+
let palette = crate::Palette::new(f.alternate());
79+
write!(f, "{}", palette.description(self.name))
8080
}
8181
}
8282

src/ord.rs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,13 @@ where
6969
T: fmt::Debug,
7070
{
7171
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
72-
let palette = crate::Palette::current();
72+
let palette = crate::Palette::new(f.alternate());
7373
write!(
7474
f,
7575
"{} {} {}",
76-
palette.var.paint("var"),
77-
palette.description.paint(self.op),
78-
palette
79-
.expected
80-
.paint(utils::DebugAdapter::new(&self.constant)),
76+
palette.var("var"),
77+
palette.description(self.op),
78+
palette.expected(utils::DebugAdapter::new(&self.constant)),
8179
)
8280
}
8381
}
@@ -195,15 +193,13 @@ where
195193
T: fmt::Debug,
196194
{
197195
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
198-
let palette = crate::Palette::current();
196+
let palette = crate::Palette::new(f.alternate());
199197
write!(
200198
f,
201199
"{} {} {}",
202-
palette.var.paint("var"),
203-
palette.description.paint(self.op),
204-
palette
205-
.expected
206-
.paint(utils::DebugAdapter::new(&self.constant)),
200+
palette.var("var"),
201+
palette.description(self.op),
202+
palette.expected(utils::DebugAdapter::new(&self.constant)),
207203
)
208204
}
209205
}

src/path/existence.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,12 @@ impl reflection::PredicateReflection for ExistencePredicate {}
4444

4545
impl fmt::Display for ExistencePredicate {
4646
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
47-
let palette = crate::Palette::current();
47+
let palette = crate::Palette::new(f.alternate());
4848
write!(
4949
f,
5050
"{}({})",
51-
palette
52-
.description
53-
.paint(if self.exists { "exists" } else { "missing" }),
54-
palette.var.paint("var")
51+
palette.description(if self.exists { "exists" } else { "missing" }),
52+
palette.var("var")
5553
)
5654
}
5755
}

0 commit comments

Comments
 (0)