From 8593142c2627912a9e64cc015d85af126794bac0 Mon Sep 17 00:00:00 2001 From: Gilles Meunier Date: Fri, 3 May 2024 12:17:59 +0200 Subject: [PATCH] Make `ceprint!` use `cformat!` --- Cargo.toml | 2 +- src/ceprint/mod.rs | 25 ++----------------------- src/cformat/mod.rs | 14 +------------- src/lib.rs | 3 +++ src/utils.rs | 18 ++++++++++++++++++ 5 files changed, 25 insertions(+), 37 deletions(-) create mode 100644 src/utils.rs diff --git a/Cargo.toml b/Cargo.toml index da484a5..1f9849d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ default = ["cprint"] coloration = [] cformat = ["coloration"] cprint = ["coloration", "cformat"] -ceprint = ["coloration"] +ceprint = ["coloration", "cformat"] [package.metadata.docs.rs] all-features = true diff --git a/src/ceprint/mod.rs b/src/ceprint/mod.rs index c04ae26..94d61e7 100644 --- a/src/ceprint/mod.rs +++ b/src/ceprint/mod.rs @@ -11,31 +11,10 @@ macro_rules! ceprint { ($msg:expr) => {{ use $crate::coloration::{colorize_string, Color::Red}; - let mut still_at_start = true; - - let white_spaces = $msg - .chars() - .filter_map(|c| { - if c.is_whitespace() && still_at_start { - Some(c) - } else { - still_at_start = false; - None - } - }) - .collect::(); + let white_spaces = $crate::_get_white_spaces_at_start!($msg); let msg = $msg.trim_start(); - print!( - "{}{} {}", - white_spaces, - format!( - "{}{}", - " ".repeat(12 - "Error".len()), - colorize_string("Error", Red) - ), - msg - ); + print!("{}{}", white_spaces, $crate::cformat!("Error", msg, Red)); }}; } diff --git a/src/cformat/mod.rs b/src/cformat/mod.rs index a1dc78d..ea7cfa2 100644 --- a/src/cformat/mod.rs +++ b/src/cformat/mod.rs @@ -12,19 +12,7 @@ macro_rules! cformat { ($title:expr, $msg:expr, $color:expr) => {{ use $crate::coloration::Coloration; - let mut still_at_start = true; - - let white_spaces = $title - .chars() - .filter_map(|c| { - if c.is_whitespace() && still_at_start { - Some(c) - } else { - still_at_start = false; - None - } - }) - .collect::(); + let white_spaces = $crate::_get_white_spaces_at_start!($title); let title = $title.trim_start(); format!( diff --git a/src/lib.rs b/src/lib.rs index 467874a..f682826 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,3 +23,6 @@ mod ceprint; #[cfg(feature = "cformat")] mod cformat; + +#[cfg(any(feature = "cprint", feature = "ceprint", feature = "cformat"))] +mod utils; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..67a29d1 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,18 @@ +#[macro_export] +macro_rules! _get_white_spaces_at_start { + ($text:expr) => {{ + let mut still_at_start = true; + + $text + .chars() + .filter_map(|c| { + if c.is_whitespace() && still_at_start { + Some(c) + } else { + still_at_start = false; + None + } + }) + .collect::() + }}; +}