Skip to content

Commit

Permalink
Merge pull request #136 from epage/anstream
Browse files Browse the repository at this point in the history
feat!: Always print color when color is enabled
  • Loading branch information
epage committed Mar 15, 2023
2 parents 9777b50 + 13baa4c commit a877a73
Show file tree
Hide file tree
Showing 18 changed files with 169 additions and 160 deletions.
2 changes: 1 addition & 1 deletion .clippy.toml
@@ -1 +1 @@
msrv = "1.60.0" # MSRV
msrv = "1.64.0" # MSRV
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Expand Up @@ -51,15 +51,15 @@ jobs:
- name: No-default features
run: cargo test --workspace --no-default-features
msrv:
name: "Check MSRV: 1.60.0"
name: "Check MSRV: 1.64.0"
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.60.0 # MSRV
toolchain: 1.64.0 # MSRV
profile: minimal
override: true
- uses: Swatinem/rust-cache@v2
Expand Down Expand Up @@ -113,7 +113,7 @@ jobs:
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: 1.60.0 # MSRV
toolchain: 1.64.0 # MSRV
profile: minimal
override: true
components: clippy
Expand Down
37 changes: 22 additions & 15 deletions Cargo.toml
@@ -1,27 +1,36 @@
[workspace]
members = ["crates/*"]
resolver = "2"

[workspace.package]
license = "MIT OR Apache-2.0"
edition = "2021"
rust-version = "1.64.0" # MSRV
include = [
"build.rs",
"src/**/*",
"Cargo.toml",
"LICENSE*",
"README.md",
"benches/**/*",
"examples/**/*"
]

[package]
name = "predicates"
version = "2.1.5"
description = "An implementation of boolean-valued predicate functions."
authors = ["Nick Stevens <nick@bitcurry.com>"]
license = "MIT OR Apache-2.0"
repository = "https://github.com/assert-rs/predicates-rs"
homepage = "https://github.com/assert-rs/predicates-rs"
documentation = "https://docs.rs/predicates"
readme = "README.md"
categories = ["data-structures", "rust-patterns"]
keywords = ["predicate", "boolean", "combinatorial", "match", "logic"]
edition = "2021"
rust-version = "1.60.0" # MSRV
include = [
"src/**/*",
"Cargo.toml",
"LICENSE*",
"README.md",
"examples/**/*"
]
license.workspace = true
edition.workspace = true
rust-version.workspace = true
include.workspace = true

[package.metadata.release]
pre-release-replacements = [
Expand All @@ -41,15 +50,13 @@ normalize-line-endings = { version = "0.3.0", optional = true }
regex = { version="1.0", optional = true }
float-cmp = { version="0.9", optional = true }
itertools = "0.10"
yansi = { version = "0.5.1", optional = true }
concolor = { version = "0.0.12", optional = true }
anstyle = "0.3.0"

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

[features]
default = ["diff", "regex", "float-cmp", "normalize-line-endings"]
default = ["diff", "regex", "float-cmp", "normalize-line-endings", "color"]
diff = ["dep:difflib"]
unstable = []
color = ["dep:yansi", "dep:concolor", "concolor?/std"]
color-auto = ["color", "concolor?/auto"]
color = []
14 changes: 4 additions & 10 deletions crates/core/Cargo.toml
Expand Up @@ -3,19 +3,13 @@ name = "predicates-core"
version = "1.0.5"
description = "An API for boolean-valued predicate functions."
authors = ["Nick Stevens <nick@bitcurry.com>"]
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/assert-rs/predicates-rs/tree/master/crates/core"
homepage = "https://github.com/assert-rs/predicates-rs/tree/master/crates/core"
documentation = "https://docs.rs/predicates-core"
categories = ["data-structures", "rust-patterns"]
keywords = ["predicate", "boolean", "combinatorial", "match", "logic"]
edition = "2021"
rust-version = "1.60.0" # MSRV
include = [
"src/**/*",
"Cargo.toml",
"LICENSE*",
"README.md",
"examples/**/*"
]
license.workspace = true
edition.workspace = true
rust-version.workspace = true
include.workspace = true
18 changes: 6 additions & 12 deletions crates/tree/Cargo.toml
Expand Up @@ -3,26 +3,20 @@ name = "predicates-tree"
version = "1.0.7"
authors = ["Nick Stevens <nick@bitcurry.com>"]
description = "Render boolean-valued predicate functions results as a tree."
license = "MIT OR Apache-2.0"
readme = "README.md"
repository = "https://github.com/assert-rs/predicates-rs/tree/master/crates/tree"
homepage = "https://github.com/assert-rs/predicates-rs/tree/master/crates/tree"
documentation = "https://docs.rs/predicates-tree"
categories = ["data-structures", "rust-patterns"]
keywords = ["predicate", "boolean", "combinatorial", "match", "logic"]
edition = "2021"
rust-version = "1.60.0" # MSRV
include = [
"src/**/*",
"Cargo.toml",
"LICENSE*",
"README.md",
"examples/**/*"
]
license.workspace = true
edition.workspace = true
rust-version.workspace = true
include.workspace = true

[dependencies]
predicates-core = { version = "1.0", path = "../core" }
termtree = "0.4"
termtree = "0.4.1"

[dev-dependencies]
predicates = { version = "2.1", path = "../..", features = ["color-auto"] }
predicates = { version = "2.1", path = "../..", features = ["color"] }
77 changes: 48 additions & 29 deletions src/color.rs
@@ -1,50 +1,69 @@
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct Palette {
pub(crate) description: styled::Style,
pub(crate) var: styled::Style,
pub(crate) expected: styled::Style,
description: anstyle::Style,
var: anstyle::Style,
expected: anstyle::Style,
}

impl Palette {
#[cfg(feature = "color")]
pub(crate) fn current() -> Self {
if concolor::get(concolor::Stream::Either).ansi_color() {
pub(crate) fn new(alternate: bool) -> Self {
if alternate && cfg!(feature = "color") {
Self {
description: styled::Style(yansi::Style::new(yansi::Color::Blue).bold()),
var: styled::Style(yansi::Style::new(yansi::Color::Red).bold()),
expected: styled::Style(yansi::Style::new(yansi::Color::Green).bold()),
description: anstyle::AnsiColor::Blue | anstyle::Effects::BOLD,
var: anstyle::AnsiColor::Red | anstyle::Effects::BOLD,
expected: anstyle::AnsiColor::Green | anstyle::Effects::BOLD,
}
} else {
Self::default()
Self::plain()
}
}

#[cfg(not(feature = "color"))]
pub(crate) fn current() -> Self {
Self::default()
pub(crate) fn plain() -> Self {
Self {
description: Default::default(),
var: Default::default(),
expected: Default::default(),
}
}

pub(crate) fn description<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.description)
}
}

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

impl Style {
pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> impl std::fmt::Display {
self.0.paint(item)
}
pub(crate) fn expected<D: std::fmt::Display>(self, display: D) -> Styled<D> {
Styled::new(display, self.expected)
}
}

#[cfg(not(feature = "color"))]
mod styled {
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct Style;
#[derive(Debug)]
pub(crate) struct Styled<D> {
display: D,
style: anstyle::Style,
}

impl Style {
pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> impl std::fmt::Display {
item
impl<D: std::fmt::Display> Styled<D> {
pub(crate) fn new(display: D, style: anstyle::Style) -> Self {
Self { display, style }
}
}

impl<D: std::fmt::Display> std::fmt::Display for Styled<D> {
#[inline]
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
if f.alternate() {
write!(
f,
"{}{}{}",
self.style.render(),
self.display,
self.style.render_reset()
)
} else {
self.display.fmt(f)
}
}
}
4 changes: 2 additions & 2 deletions src/constant.rs
Expand Up @@ -41,8 +41,8 @@ impl reflection::PredicateReflection for BooleanPredicate {

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

Expand Down
8 changes: 4 additions & 4 deletions src/float/close.rs
Expand Up @@ -124,13 +124,13 @@ impl reflection::PredicateReflection for IsClosePredicate {

impl fmt::Display for IsClosePredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let palette = crate::Palette::current();
let palette = crate::Palette::new(f.alternate());
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("!="),
palette.expected.paint(self.target),
palette.var("var"),
palette.description("!="),
palette.expected(self.target),
)
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/function.rs
Expand Up @@ -17,6 +17,7 @@ use crate::Predicate;

/// Predicate that wraps a function over a reference that returns a `bool`.
/// This type is returned by the `predicate::function` function.
#[allow(clippy::derive_partial_eq_without_eq)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct FnPredicate<F, T>
where
Expand Down Expand Up @@ -96,12 +97,12 @@ where
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let palette = crate::Palette::current();
let palette = crate::Palette::new(f.alternate());
write!(
f,
"{}({})",
palette.description.paint(self.name),
palette.var.paint("var"),
palette.description(self.name),
palette.var("var"),
)
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/iter.rs
Expand Up @@ -109,13 +109,13 @@ where
T: PartialEq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let palette = crate::Palette::current();
let palette = crate::Palette::new(f.alternate());
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("in"),
palette.expected.paint("values")
palette.var("var"),
palette.description("in"),
palette.expected("values")
)
}
}
Expand Down Expand Up @@ -218,13 +218,13 @@ where
T: Ord + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let palette = crate::Palette::current();
let palette = crate::Palette::new(f.alternate());
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("in"),
palette.expected.paint("values")
palette.var("var"),
palette.description("in"),
palette.expected("values")
)
}
}
Expand Down Expand Up @@ -280,13 +280,13 @@ where
T: Hash + Eq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let palette = crate::Palette::current();
let palette = crate::Palette::new(f.alternate());
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("in"),
palette.expected.paint("values")
palette.var("var"),
palette.description("in"),
palette.expected("values")
)
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/name.rs
Expand Up @@ -75,8 +75,8 @@ where
Item: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let palette = crate::Palette::current();
write!(f, "{}", palette.description.paint(self.name))
let palette = crate::Palette::new(f.alternate());
write!(f, "{}", palette.description(self.name))
}
}

Expand Down

0 comments on commit a877a73

Please sign in to comment.