Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix writer:: Summarize ignoring Coloring options (#186) #189

Merged
merged 5 commits into from
Dec 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,15 @@ All user visible changes to `cucumber` crate will be documented in this file. Th
- Support for [Cucumber Expressions] via `#[given(expr = ...)]`, `#[when(expr = ...)]` and `#[then(expr = ...)]` syntax. ([#157])
- Support for custom parameters in [Cucumber Expressions] via `#[derive(cucumber::Parameter)]` macro. ([#168])
- Merging tags from `Feature` and `Rule` with `Scenario` when filtering with `--tags` CLI option. ([#166])
- `writer::AssertNormalized` forcing `Normalized` implementation. ([#182])
- `writer::AssertNormalized` forcing `Normalized` implementation. ([#182])
- `cli::Colored` trait for propagating `Coloring` to arbitrary `Writer`s. ([#189])

### Fixed

- Template regex in `Scenario Outline` expansion from `<(\S+)>` to `<([^>\s]+)>`. ([#163])
- Multiple `Examples` in `Scenario Outline`. ([#165], [#164])
- Docstring and name expansion in `Scenario Outline`. ([#178], [#172])
- `writer::Summarized` ignoring `Coloring` options. ([#189], [#188])

[#147]: /../../pull/147
[#151]: /../../pull/151
Expand Down
35 changes: 35 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ use gherkin::tagexpr::TagOperation;
use regex::Regex;
use structopt::StructOpt;

use crate::writer::Coloring;

// Workaround for overwritten doc-comments.
// https://github.com/TeXitoi/structopt/issues/333#issuecomment-712265332
#[cfg_attr(
Expand Down Expand Up @@ -147,6 +149,20 @@ where
pub custom: Custom,
}

/// Indication whether a [`Writer`] using CLI options supports colored output.
///
/// [`Writer`]: crate::Writer
pub trait Colored {
/// Returns [`Coloring`] indicating whether a [`Writer`] using CLI options
/// supports colored output or not.
///
/// [`Writer`]: crate::Writer
#[must_use]
fn coloring(&self) -> Coloring {
Coloring::Never
}
}

// Workaround for overwritten doc-comments.
// https://github.com/TeXitoi/structopt/issues/333#issuecomment-712265332
#[cfg_attr(doc, doc = "Empty CLI options.")]
Expand All @@ -163,6 +179,8 @@ pub struct Empty {
skipped: (),
}

impl Colored for Empty {}

// Workaround for overwritten doc-comments.
// https://github.com/TeXitoi/structopt/issues/333#issuecomment-712265332
#[cfg_attr(
Expand Down Expand Up @@ -208,6 +226,8 @@ where

// Useful blanket impls:

impl cli::Colored for Cli {}

#[async_trait(?Send)]
impl<'val, W, Wr, Val> writer::Arbitrary<'val, W, Val> for CustomWriter<Wr>
where
Expand Down Expand Up @@ -276,3 +296,18 @@ impl<L: StructOpt, R: StructOpt> Compose<L, R> {
(left, right)
}
}

impl<L, R> Colored for Compose<L, R>
where
L: Colored + StructOpt,
R: Colored + StructOpt,
{
fn coloring(&self) -> Coloring {
// Basically, founds "maximum" `Coloring` of CLI options.
match (self.left.coloring(), self.right.coloring()) {
(Coloring::Always, _) | (_, Coloring::Always) => Coloring::Always,
(Coloring::Auto, _) | (_, Coloring::Auto) => Coloring::Auto,
(Coloring::Never, Coloring::Never) => Coloring::Never,
}
}
}
13 changes: 8 additions & 5 deletions src/writer/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use regex::CaptureLocations;
use structopt::StructOpt;

use crate::{
cli::Colored,
event::{self, Info},
parser,
writer::{
Expand Down Expand Up @@ -54,6 +55,12 @@ pub struct Cli {
pub color: Coloring,
}

impl Colored for Cli {
fn coloring(&self) -> Coloring {
self.color
}
}

/// Possible policies of a [`console`] output coloring.
#[derive(Clone, Copy, Debug)]
pub enum Coloring {
Expand Down Expand Up @@ -221,11 +228,7 @@ impl<Out: io::Write> Basic<Out> {
if cli.verbose {
self.verbose = true;
}
match cli.color {
Coloring::Auto => {}
Coloring::Always => self.styles.is_present = true,
Coloring::Never => self.styles.is_present = false,
}
self.styles.apply_coloring(cli.color);
}

/// Clears last `n` lines if [`Coloring`] is enabled.
Expand Down
11 changes: 11 additions & 0 deletions src/writer/out.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ use std::{borrow::Cow, io, str};
use console::Style;
use derive_more::{Deref, DerefMut, Display, From, Into};

use super::Coloring;

/// [`Style`]s for terminal output.
#[derive(Debug)]
pub struct Styles {
Expand Down Expand Up @@ -58,6 +60,15 @@ impl Styles {
Self::default()
}

/// Applies the given [`Coloring`] to these [`Styles`].
pub fn apply_coloring(&mut self, color: Coloring) {
match color {
Coloring::Auto => {}
Coloring::Always => self.is_present = true,
Coloring::Never => self.is_present = false,
}
}

/// If terminal is present colors `input` with [`Styles::ok`] color or
/// leaves "as is" otherwise.
#[must_use]
Expand Down
7 changes: 6 additions & 1 deletion src/writer/summarize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use derive_more::Deref;
use itertools::Itertools as _;

use crate::{
cli::Colored,
event, parser,
writer::{self, out::Styles},
Event, World, Writer,
Expand Down Expand Up @@ -165,6 +166,7 @@ impl<W, Wr> Writer<W> for Summarize<Wr>
where
W: World,
Wr: for<'val> writer::Arbitrary<'val, W, String> + Summarizable,
Wr::Cli: Colored,
{
type Cli = Wr::Cli;

Expand Down Expand Up @@ -204,7 +206,10 @@ where

if let State::FinishedButNotOutput = self.state {
self.state = State::FinishedAndOutput;
self.writer.write(Styles::new().summary(self)).await;

let mut styles = Styles::new();
styles.apply_coloring(cli.coloring());
self.writer.write(styles.summary(self)).await;
}
}
}
Expand Down