diff --git a/src/lib.rs b/src/lib.rs index b6ff718..ae6095e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,13 @@ pub fn args(year: u16) -> Command { .conflicts_with("days") .help("Run all days"), ) + .arg( + Arg::new("color") + .short('c') + .long("no-color") + .action(ArgAction::SetTrue) + .help("Disable colored timings"), + ) } #[macro_export] diff --git a/src/parse/gen_run.rs b/src/parse/gen_run.rs index b60480c..85c8b4f 100644 --- a/src/parse/gen_run.rs +++ b/src/parse/gen_run.rs @@ -25,8 +25,8 @@ macro_rules! run_day { } }; - if let Some(input) = $crate::run_gen!($day, &data, $gen) { - $( $crate::run_sol!($day, &input, $sol); )+ + if let Some(input) = $crate::run_gen!($day, &data, $opt, $gen) { + $( $crate::run_sol!($day, &input, $opt, $sol); )+ } else { $( $crate::skip_sol!($sol); )+ } @@ -37,23 +37,28 @@ macro_rules! run_day { #[macro_export] macro_rules! run_gen { // No generator is needed: default behavior is to just pass input &str - ( $day: ident, $data: expr, { gen_default } ) => {{ + ( $day: ident, $data: expr, $opt: expr, { gen_default } ) => {{ Some($data) }}; // Run generator - ( $day: ident, $data: expr, { gen $generator: ident } ) => {{ + ( $day: ident, $data: expr, $opt: expr, { gen $generator: ident }) => {{ use $crate::utils::Line; let start = Instant::now(); let input = $day::$generator($data); let elapsed = start.elapsed(); - println!(" - {}", Line::new("generator").with_duration(elapsed)); + println!( + " - {}", + Line::new("generator") + .with_duration(elapsed) + .disable_duration_color($opt.get_flag("color")) + ); Some(input) }}; // Run fallible generator - ( $day: ident, $data: expr, { gen_fallible $generator: ident } ) => {{ + ( $day: ident, $data: expr, $opt: expr, { gen_fallible $generator: ident }) => {{ use $crate::colored::*; use $crate::utils::{Line, TryUnwrap}; @@ -63,7 +68,12 @@ macro_rules! run_gen { match result.try_unwrap() { Ok(input) => { - println!(" - {}", Line::new("generator").with_duration(elapsed)); + println!( + " - {}", + Line::new("generator") + .with_duration(elapsed) + .disable_duration_color($opt.get_flag("color")) + ); Some(input) } Err(msg) => { @@ -71,6 +81,7 @@ macro_rules! run_gen { " - {}", Line::new("generator") .with_duration(elapsed) + .disable_duration_color($opt.get_flag("color")) .with_state(msg.red()) ); None @@ -82,7 +93,7 @@ macro_rules! run_gen { #[macro_export] macro_rules! run_sol { // Run solution - ( $day: ident, $input: expr, { sol $solution: ident } ) => {{ + ( $day: ident, $input: expr, $opt: expr, { sol $solution: ident } ) => {{ use $crate::colored::*; use $crate::utils::Line; @@ -94,19 +105,22 @@ macro_rules! run_sol { " - {}", Line::new(stringify!($solution)) .with_duration(elapsed) + .disable_duration_color($opt.get_flag("color")) .with_state(format!("{}", response).normal()) ); }}; // Run fallible solution - ( $day: ident, $input: expr, { sol_fallible $solution: ident } ) => {{ + ( $day: ident, $input: expr, $opt: expr, { sol_fallible $solution: ident } ) => {{ use $crate::colored::*; use $crate::utils::{Line, TryUnwrap}; let start = Instant::now(); let response = $day::$solution($input); let elapsed = start.elapsed(); - let line = Line::new(stringify!($solution)).with_duration(elapsed); + let line = Line::new(stringify!($solution)) + .with_duration(elapsed) + .disable_duration_color($opt.get_flag("color")); println!( " - {}", diff --git a/src/utils.rs b/src/utils.rs index 73e3176..9f292b7 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -46,6 +46,7 @@ const DEFAULT_WIDTH: usize = 30; pub struct Line { text: String, duration: Option, + disable_duration_color: bool, state: Option, } @@ -54,6 +55,7 @@ impl Line { Self { text: text.into(), duration: None, + disable_duration_color: false, state: None, } } @@ -67,6 +69,11 @@ impl Line { self.state = Some(state.into()); self } + + pub fn disable_duration_color(mut self, disable: bool) -> Self { + self.disable_duration_color = disable; + self + } } impl fmt::Display for Line { @@ -78,7 +85,16 @@ impl fmt::Display for Line { .map(|duration| format!(" ({:.2?})", duration)) .unwrap_or_else(String::new); - write!(f, "{}{}", self.text, duration.bright_black())?; + let duration = match self.duration { + _ if self.disable_duration_color => duration.bright_black(), + Some(d) if d < Duration::from_nanos(1000) => duration.bright_magenta(), + Some(d) if d < Duration::from_micros(1000) => duration.green(), + Some(d) if d < Duration::from_millis(1000) => duration.bright_yellow(), + Some(d) if d < Duration::from_secs(60) => duration.bright_red(), + _ => duration.bright_black(), + }; + + write!(f, "{}{}", self.text, duration)?; if let Some(state) = &self.state { let width = self.text.chars().count() + 1 + duration.chars().count();