Skip to content

Commit

Permalink
some updates and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Aloso committed May 17, 2024
1 parent 31558bf commit 8420d48
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 105 deletions.
4 changes: 2 additions & 2 deletions pomsky-lib/src/exprs/range.rs
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ enum Rule {
Alt(Alt),
}

#[cfg(FALSE)]
#[cfg(any())]
impl std::fmt::Debug for Rule {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand Down Expand Up @@ -410,7 +410,7 @@ impl Rule {
}
}

#[cfg(FALSE)]
#[cfg(any())]
fn debug_size(&self) -> u64 {
match self {
Rule::Empty => 1,
Expand Down
109 changes: 55 additions & 54 deletions pomsky-lib/tests/it/color.rs
Original file line number Diff line number Diff line change
@@ -1,72 +1,73 @@
use std::{fmt, io::IsTerminal, sync::OnceLock};

pub(crate) enum Color<T> {
Red(T),
Green(T),
Blue(T),
Yellow(T),
NoColor(T),
pub(crate) enum Color {
Red,
Green,
Blue,
Yellow,
}

impl<T> Color<T> {
pub(crate) fn color_if(self, condition: bool) -> Self {
if condition {
self
} else {
Color::NoColor(self.into_inner())
}
pub(crate) struct Colored<T> {
pub(crate) inner: T,
color: Option<Color>,
}

/// A `Display` wrapper for two values to format after another.
pub(crate) struct D2<A, B>(pub(crate) A, pub(crate) B);

impl<A: fmt::Display, B: fmt::Display> fmt::Display for D2<A, B> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)?;
self.1.fmt(f)
}
}

pub(crate) fn inner(&self) -> &T {
match self {
Color::Red(inner)
| Color::Green(inner)
| Color::Blue(inner)
| Color::Yellow(inner)
| Color::NoColor(inner) => inner,
}
pub(crate) mod prelude {
use super::{Color, Colored};

pub(crate) fn red<T>(inner: T) -> Colored<T> {
(Color::Red, inner).into()
}

pub(crate) fn into_inner(self) -> T {
match self {
Color::Red(inner)
| Color::Green(inner)
| Color::Blue(inner)
| Color::Yellow(inner)
| Color::NoColor(inner) => inner,
}
pub(crate) fn green<T>(inner: T) -> Colored<T> {
(Color::Green, inner).into()
}

pub(crate) fn blue<T>(inner: T) -> Colored<T> {
(Color::Blue, inner).into()
}

pub(crate) fn yellow<T>(inner: T) -> Colored<T> {
(Color::Yellow, inner).into()
}

pub(crate) fn no_color<T>(inner: T) -> Colored<T> {
Colored { color: None, inner }
}
}

impl<T> Colored<T> {
fn get_markers(&self) -> (&'static str, &'static str) {
const RED: &str = "\x1B[38;5;9m";
const GREEN: &str = "\x1B[38;5;10m";
const BLUE: &str = "\x1B[38;5;14m";
const YELLOW: &str = "\x1B[38;5;11m";
const RESET: &str = "\x1B[0m";

match self {
Color::Red(_) => (RED, RESET),
Color::Green(_) => (GREEN, RESET),
Color::Blue(_) => (BLUE, RESET),
Color::Yellow(_) => (YELLOW, RESET),
Color::NoColor(_) => ("", ""),
match self.color {
None => ("", ""),
Some(Color::Red) => (RED, RESET),
Some(Color::Green) => (GREEN, RESET),
Some(Color::Blue) => (BLUE, RESET),
Some(Color::Yellow) => (YELLOW, RESET),
}
}
}

macro_rules! color {
(~brace $e:expr) => {
"{}"
};
($id:ident if $cond:expr; $($e:expr),* $(,)?) => {
color!($id; $($e),*).color_if($cond)
};
($id:ident; $e:expr) => {
$crate::color::Color::$id($e)
};
($id:ident; $($e:expr),*) => {
$crate::color::Color::$id(format!(concat!($(color!(~brace $e)),*), $($e),*))
};
impl<T> From<(Color, T)> for Colored<T> {
fn from((color, inner): (Color, T)) -> Self {
Colored { inner, color: Some(color) }
}
}

fn stdout_is_terminal() -> bool {
Expand All @@ -75,24 +76,24 @@ fn stdout_is_terminal() -> bool {
*IS_TERMINAL.get_or_init(|| std::io::stdout().is_terminal())
}

impl<T: fmt::Display> fmt::Display for Color<T> {
impl<T: fmt::Display> fmt::Display for Colored<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if stdout_is_terminal() {
let (m1, m2) = self.get_markers();
write!(f, "{m1}{}{m2}", self.inner())
write!(f, "{m1}{}{m2}", self.inner)
} else {
self.inner().fmt(f)
self.inner.fmt(f)
}
}
}

impl<T: fmt::Debug> fmt::Debug for Color<T> {
impl<T: fmt::Debug> fmt::Debug for Colored<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if stdout_is_terminal() {
let (m1, m2) = self.get_markers();
write!(f, "{m1}{:?}{m2}", self.inner())
write!(f, "{m1}{:?}{m2}", self.inner)
} else {
self.inner().fmt(f)
self.inner.fmt(f)
}
}
}
14 changes: 7 additions & 7 deletions pomsky-lib/tests/it/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use pomsky::{
};
use regex_test::RegexTest;

use crate::{args::Args, color::Color::*};
use crate::{args::Args, color::prelude::*};

pub(crate) enum TestResult {
Success,
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Options {
"rust" => RegexFlavor::Rust,
"ruby" => RegexFlavor::Ruby,
_ => {
eprintln!("{}: Unknown flavor {value:?}", Yellow("Warning"));
eprintln!("{}: Unknown flavor {value:?}", yellow("Warning"));
eprintln!(" in {path:?}");
continue;
}
Expand All @@ -76,7 +76,7 @@ impl Options {
"success" => Outcome::Success,
"error" => Outcome::Error,
_ => {
eprintln!("{}: Unknown expected outcome {value:?}", Yellow("Warning"));
eprintln!("{}: Unknown expected outcome {value:?}", yellow("Warning"));
eprintln!(" in {path:?}");
continue;
}
Expand All @@ -87,7 +87,7 @@ impl Options {
"yes" | "true" | "" => true,
"no" | "false" => false,
_ => {
eprintln!("{}: Unknown boolean {value:?}", Yellow("Warning"));
eprintln!("{}: Unknown boolean {value:?}", yellow("Warning"));
eprintln!(" in {path:?}");
continue;
}
Expand All @@ -98,14 +98,14 @@ impl Options {
"yes" | "true" | "" => true,
"no" | "false" => false,
_ => {
eprintln!("{}: Unknown boolean {value:?}", Yellow("Warning"));
eprintln!("{}: Unknown boolean {value:?}", yellow("Warning"));
eprintln!(" in {path:?}");
continue;
}
});
}
_ => {
eprintln!("{}: Unknown option {key:?}", Yellow("Warning"));
eprintln!("{}: Unknown option {key:?}", yellow("Warning"));
eprintln!(" in {path:?}");
}
}
Expand Down Expand Up @@ -231,7 +231,7 @@ pub(crate) fn test_file(
_ => {
eprintln!(
"{}: Flavor {:?} can't be compiled at the moment",
Yellow("Warning"),
yellow("Warning"),
options.flavor
);
eprintln!(" in {path:?}");
Expand Down
8 changes: 4 additions & 4 deletions pomsky-lib/tests/it/fuzzer.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{fmt, time::Instant};

use crate::color::Color::*;
use crate::color::prelude::*;
use pomsky::Expr;
use regex::Regex;

Expand Down Expand Up @@ -50,7 +50,7 @@ pub(crate) fn fuzz_ranges(
Ok(r) => r,
Err(error) => {
let res = FuzzError::InvalidRegex { range: (lo, hi), regex, error };
eprintln!("{}: {res}\n", Red("Error"));
eprintln!("{}: {res}\n", red("Error"));
errors.push(res);
continue;
}
Expand All @@ -69,7 +69,7 @@ pub(crate) fn fuzz_ranges(
regex: regex.as_str().to_string(),
input: strings[i].clone(),
};
eprintln!("{}: {res}\n", Red("Error"));
eprintln!("{}: {res}\n", red("Error"));
errors.push(res);
}
(false, true) => {
Expand All @@ -78,7 +78,7 @@ pub(crate) fn fuzz_ranges(
regex: regex.as_str().to_string(),
input: strings[i].clone(),
};
eprintln!("{}: {res}\n", Red("Error"));
eprintln!("{}: {res}\n", red("Error"));
errors.push(res);
}
}
Expand Down
Loading

0 comments on commit 8420d48

Please sign in to comment.