1+ use std:: cmp:: min;
12use std:: error:: Error ;
23use std:: fs:: { create_dir_all, read_to_string, File } ;
34use std:: io:: Write ;
45use std:: io:: { stdin, stdout} ;
6+ use std:: iter;
57use std:: path:: PathBuf ;
68use std:: time:: { Duration , Instant } ;
79
810pub use clap:: Clap ;
11+ pub use colored;
12+ use colored:: * ;
913
1014const DISPLAY_WIDTH : usize = 40 ;
1115const BASE_URL : & str = "https://adventofcode.com" ;
@@ -27,13 +31,18 @@ pub struct Opt {
2731 pub days : Vec < String > ,
2832}
2933
30- pub fn print_with_duration ( line : & str , duration : Duration ) {
31- println ! (
32- "{:.<width$} {:.2?}" ,
33- format!( "{} " , line) ,
34- duration,
35- width = DISPLAY_WIDTH
36- ) ;
34+ pub fn print_with_duration ( line : & str , output : Option < & str > , duration : Duration ) {
35+ let duration = format ! ( "({:.2?})" , duration) ;
36+ print ! ( " - {} {}" , line, duration. dimmed( ) ) ;
37+
38+ if let Some ( output) = output {
39+ let width = " - " . len ( ) + line. len ( ) + 1 + duration. len ( ) ;
40+ let dots = DISPLAY_WIDTH - min ( DISPLAY_WIDTH - 5 , width) - 2 ;
41+ let dots: String = iter:: repeat ( '.' ) . take ( dots) . collect ( ) ;
42+ println ! ( " {} {}" , dots. dimmed( ) , output. bold( ) ) ;
43+ } else {
44+ println ! ( )
45+ }
3746}
3847
3948fn input_path ( year : u16 , day : u8 ) -> String {
@@ -68,7 +77,7 @@ pub fn get_input(year: u16, day: u8) -> Result<String, Box<dyn Error>> {
6877 . send ( ) ?;
6978 let elapsed = start. elapsed ( ) ;
7079
71- print_with_duration ( " - downloaded input file" , elapsed) ;
80+ print_with_duration ( "downloaded input file" , None , elapsed) ;
7281 resp. text ( )
7382 } ) ?;
7483
@@ -94,14 +103,6 @@ pub fn get_conn_token() -> Result<String, std::io::Error> {
94103 } )
95104}
96105
97- // Intended usage:
98- //
99- // aoc::main! {
100- // year 2019;
101- // day1: generator => part1, part2;
102- // day2: generator => part1, part2
103- // }
104-
105106#[ macro_export]
106107macro_rules! main {
107108 (
@@ -150,15 +151,16 @@ macro_rules! main {
150151 let start = Instant :: now( ) ;
151152 let input = $day:: $generator( & data) ;
152153 let elapsed = start. elapsed( ) ;
153- $crate:: print_with_duration( " - generator" , elapsed) ;
154+ $crate:: print_with_duration( "generator" , None , elapsed) ;
154155
155156 $( {
156157 let start = Instant :: now( ) ;
157158 let response = $day:: $solution( & input) ;
158159 let elapsed = start. elapsed( ) ;
159160
160161 $crate:: print_with_duration(
161- & format!( " - {}: {}" , stringify!( $solution) , response) ,
162+ stringify!( $solution) ,
163+ Some ( & format!( "{}" , response) ) ,
162164 elapsed,
163165 ) ;
164166 } ) +
0 commit comments