Skip to content

Commit 42b65bf

Browse files
committed
add some terminal coloring
1 parent 70b2f87 commit 42b65bf

File tree

3 files changed

+33
-18
lines changed

3 files changed

+33
-18
lines changed

Cargo.lock

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,4 @@ edition = "2018"
77
[dependencies]
88
attohttpc = { version = "0.16.0", default_features = false, features = ["tls"] }
99
clap = "3.0.0-beta.2"
10+
colored = "2.0.0"

src/lib.rs

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
use std::cmp::min;
12
use std::error::Error;
23
use std::fs::{create_dir_all, read_to_string, File};
34
use std::io::Write;
45
use std::io::{stdin, stdout};
6+
use std::iter;
57
use std::path::PathBuf;
68
use std::time::{Duration, Instant};
79

810
pub use clap::Clap;
11+
pub use colored;
12+
use colored::*;
913

1014
const DISPLAY_WIDTH: usize = 40;
1115
const 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

3948
fn 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]
106107
macro_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

Comments
 (0)