A very simple Stopwatch for the command line
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

55 lines
1.5 KiB

//! stopwatch is a simple CLI stopwatch
use crossbeam_channel::tick;
use std::io::{stdout, Write};
use std::time::Instant;
use stopwatch::duration::FmtDuration;
use stopwatch::*;
#[macro_use]
extern crate crossbeam_channel;
fn main() {
if let Err(err) = run() {
println!("{}", err);
std::process::exit(1);
}
}
fn run() -> std::io::Result<()> {
// 30 fps should be enough
let step_duration = std::time::Duration::from_millis(1000 / 30);
let start = Instant::now();
let ticker = tick(step_duration);
let signals = signal_handler()?;
let input = input_handler()?;
let out = stdout();
let mut out = out.lock();
let mut elapsed = FmtDuration::from(start.elapsed());
let mut last_round = start.clone();
let mut round: u32 = 1;
loop {
select! {
recv(ticker) -> now => {
elapsed = FmtDuration::from(now.unwrap().duration_since(start));
reset_line(&mut out).unwrap();
write!(out, "{}",elapsed).unwrap();
out.flush().unwrap();
}
recv(signals) -> _ => {
reset_line(&mut out).unwrap();
writeln!(out, "Total time: {}", elapsed).unwrap();
return Ok(());
}
recv(input) -> _ => {
reset_line(&mut out)?;
writeln!(out, "Lap {}: {}", round, FmtDuration::from(last_round.elapsed()))?;
last_round = Instant::now();
round +=1;
}
}
}
}