Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
34 changes: 24 additions & 10 deletions src/parse/gen_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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); )+
}
Expand All @@ -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};

Expand All @@ -63,14 +68,20 @@ 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) => {
println!(
" - {}",
Line::new("generator")
.with_duration(elapsed)
.disable_duration_color($opt.get_flag("color"))
.with_state(msg.red())
);
None
Expand All @@ -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;

Expand All @@ -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!(
" - {}",
Expand Down
18 changes: 17 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const DEFAULT_WIDTH: usize = 30;
pub struct Line {
text: String,
duration: Option<Duration>,
disable_duration_color: bool,
state: Option<ColoredString>,
}

Expand All @@ -54,6 +55,7 @@ impl Line {
Self {
text: text.into(),
duration: None,
disable_duration_color: false,
state: None,
}
}
Expand All @@ -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 {
Expand All @@ -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();
Expand Down