Skip to content

Commit

Permalink
feat: Color output
Browse files Browse the repository at this point in the history
Ideally more would be colored but I'm unsure about coloring the values
passed to reflection, which means it needs to be colored in `tree`  and
requires duplicating some logic.  Ugh.
  • Loading branch information
epage committed Oct 7, 2021
1 parent 19019fb commit 0e97658
Show file tree
Hide file tree
Showing 20 changed files with 264 additions and 29 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Expand Up @@ -33,6 +33,8 @@ 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.0", optional = true }
concolor-control = { version = "0.0.7", optional = true }

[dev-dependencies]
predicates-tree = { version = "1.0", path = "crates/tree" }
Expand All @@ -41,3 +43,5 @@ predicates-tree = { version = "1.0", path = "crates/tree" }
default = ["diff", "regex", "float-cmp", "normalize-line-endings"]
diff = ["difflib"]
unstable = []
color = ["yansi", "concolor-control/std"]
color-auto = ["color", "concolor-control/auto"]
2 changes: 1 addition & 1 deletion crates/tree/Cargo.toml
Expand Up @@ -17,4 +17,4 @@ predicates-core = { version = "1.0", path = "../core" }
termtree = "0.2"

[dev-dependencies]
predicates = { version = "2.0", path = "../.." }
predicates = { version = "2.0", path = "../..", features = ["color-auto"] }
2 changes: 1 addition & 1 deletion src/boxed.rs
Expand Up @@ -60,7 +60,7 @@ where
Item: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
self.0.fmt(f)
}
}

Expand Down
50 changes: 50 additions & 0 deletions src/color.rs
@@ -0,0 +1,50 @@
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct Palette {
pub(crate) description: styled::Style,
pub(crate) var: styled::Style,
pub(crate) expected: styled::Style,
}

impl Palette {
#[cfg(feature = "color")]
pub(crate) fn current() -> Self {
if concolor_control::get(concolor_control::Stream::Either).ansi_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()),
}
} else {
Self::default()
}
}

#[cfg(not(feature = "color"))]
pub(crate) fn current() -> Self {
Self::default()
}
}

#[cfg(feature = "color")]
mod styled {
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct Style(pub(crate) yansi::Style);

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

#[cfg(not(feature = "color"))]
mod styled {
#[derive(Copy, Clone, Debug, Default)]
pub(crate) struct Style;

impl Style {
pub(crate) fn paint<T: std::fmt::Display>(self, item: T) -> impl std::fmt::Display {
item
}
}
}
3 changes: 2 additions & 1 deletion src/constant.rs
Expand Up @@ -41,7 +41,8 @@ impl reflection::PredicateReflection for BooleanPredicate {

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

Expand Down
9 changes: 8 additions & 1 deletion src/float/close.rs
Expand Up @@ -124,7 +124,14 @@ impl reflection::PredicateReflection for IsClosePredicate {

impl fmt::Display for IsClosePredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var ~= {}", self.target)
let palette = crate::Palette::current();
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("!="),
palette.expected.paint(self.target),
)
}
}

Expand Down
8 changes: 7 additions & 1 deletion src/function.rs
Expand Up @@ -96,7 +96,13 @@ where
T: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}(var)", self.name)
let palette = crate::Palette::current();
write!(
f,
"{}({})",
palette.description.paint(self.name),
palette.var.paint("var"),
)
}
}

Expand Down
27 changes: 24 additions & 3 deletions src/iter.rs
Expand Up @@ -111,7 +111,14 @@ where
T: PartialEq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var in values")
let palette = crate::Palette::current();
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("in"),
palette.expected.paint("values")
)
}
}

Expand Down Expand Up @@ -212,7 +219,14 @@ where
T: Ord + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var in values")
let palette = crate::Palette::current();
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("in"),
palette.expected.paint("values")
)
}
}

Expand Down Expand Up @@ -274,7 +288,14 @@ where
T: Hash + Eq + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var in values")
let palette = crate::Palette::current();
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("in"),
palette.expected.paint("values")
)
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Expand Up @@ -221,4 +221,6 @@ pub mod float;
pub mod path;
pub mod str;

mod color;
use color::Palette;
mod utils;
3 changes: 2 additions & 1 deletion src/name.rs
Expand Up @@ -75,7 +75,8 @@ where
Item: ?Sized,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.name)
let palette = crate::Palette::current();
write!(f, "{}", palette.description.paint(self.name))
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/ord.rs
Expand Up @@ -82,7 +82,14 @@ where
T: fmt::Debug + PartialEq,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var {} {:?}", self.op, self.constant)
let palette = crate::Palette::current();
write!(
f,
"{} {} {:?}",
palette.var.paint("var"),
palette.description.paint(self.op),
self.constant
)
}
}

Expand Down Expand Up @@ -210,7 +217,14 @@ where
T: fmt::Debug + PartialOrd,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var {} {:?}", self.op, self.constant)
let palette = crate::Palette::current();
write!(
f,
"{} {} {:?}",
palette.var.paint("var"),
palette.description.paint(self.op),
self.constant
)
}
}

Expand Down
10 changes: 9 additions & 1 deletion src/path/existence.rs
Expand Up @@ -39,7 +39,15 @@ impl reflection::PredicateReflection for ExistencePredicate {}

impl fmt::Display for ExistencePredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}(var)", if self.exists { "exists" } else { "missing" })
let palette = crate::Palette::current();
write!(
f,
"{}({})",
palette
.description
.paint(if self.exists { "exists" } else { "missing" }),
palette.var.paint("var")
)
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/path/fc.rs
Expand Up @@ -56,7 +56,7 @@ where
P: Predicate<[u8]>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.p)
self.p.fmt(f)
}
}

Expand Down
18 changes: 16 additions & 2 deletions src/path/fs.rs
Expand Up @@ -89,7 +89,14 @@ impl reflection::PredicateReflection for BinaryFilePredicate {

impl fmt::Display for BinaryFilePredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var is {}", self.path.display())
let palette = crate::Palette::current();
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("is"),
palette.expected.paint(self.path.display())
)
}
}

Expand Down Expand Up @@ -160,6 +167,13 @@ impl reflection::PredicateReflection for StrFilePredicate {

impl fmt::Display for StrFilePredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var is {}", self.path.display())
let palette = crate::Palette::current();
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("is"),
palette.expected.paint(self.path.display())
)
}
}
9 changes: 8 additions & 1 deletion src/path/ft.rs
Expand Up @@ -135,7 +135,14 @@ impl reflection::PredicateReflection for FileTypePredicate {

impl fmt::Display for FileTypePredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var is {}", self.ft)
let palette = crate::Palette::current();
write!(
f,
"{} {} {}",
palette.var.paint("var"),
palette.description.paint("is"),
palette.expected.paint(self.ft)
)
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/str/adapters.rs
Expand Up @@ -54,7 +54,7 @@ where
P: Predicate<str>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.p)
self.p.fmt(f)
}
}

Expand Down Expand Up @@ -136,7 +136,7 @@ where
P: Predicate<str>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.p)
self.p.fmt(f)
}
}

Expand Down
44 changes: 39 additions & 5 deletions src/str/basics.rs
Expand Up @@ -32,7 +32,13 @@ impl reflection::PredicateReflection for IsEmptyPredicate {}

impl fmt::Display for IsEmptyPredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var.is_empty()")
let palette = crate::Palette::current();
write!(
f,
"{}.{}()",
palette.var.paint("var"),
palette.description.paint("is_empty"),
)
}
}

Expand Down Expand Up @@ -73,7 +79,14 @@ impl reflection::PredicateReflection for StartsWithPredicate {}

impl fmt::Display for StartsWithPredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var.starts_with({:?})", self.pattern)
let palette = crate::Palette::current();
write!(
f,
"{}.{}({:?})",
palette.var.paint("var"),
palette.description.paint("starts_with"),
self.pattern
)
}
}

Expand Down Expand Up @@ -119,7 +132,14 @@ impl reflection::PredicateReflection for EndsWithPredicate {}

impl fmt::Display for EndsWithPredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var.ends_with({:?})", self.pattern)
let palette = crate::Palette::current();
write!(
f,
"{}.{}({:?})",
palette.var.paint("var"),
palette.description.paint("ends_with"),
self.pattern
)
}
}

Expand Down Expand Up @@ -185,7 +205,14 @@ impl reflection::PredicateReflection for ContainsPredicate {}

impl fmt::Display for ContainsPredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var.contains({:?})", self.pattern)
let palette = crate::Palette::current();
write!(
f,
"{}.{}({})",
palette.var.paint("var"),
palette.description.paint("contains"),
palette.expected.paint(&self.pattern),
)
}
}

Expand Down Expand Up @@ -226,7 +253,14 @@ impl reflection::PredicateReflection for MatchesPredicate {

impl fmt::Display for MatchesPredicate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "var.contains({})", self.pattern)
let palette = crate::Palette::current();
write!(
f,
"{}.{}({})",
palette.var.paint("var"),
palette.description.paint("contains"),
palette.expected.paint(&self.pattern),
)
}
}

Expand Down

0 comments on commit 0e97658

Please sign in to comment.