From 4958c3d311ab26a06d3015dde3ff2d363f0b5006 Mon Sep 17 00:00:00 2001 From: JarredAllen Date: Sat, 3 Aug 2019 08:12:50 -0700 Subject: [PATCH] Implemented core functionality --- src/main.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main.rs b/src/main.rs index ccb6e1a..2b4d2fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,7 +3,40 @@ extern crate clap; fn main() { use clap::App; - + let yml = load_yaml!("arguments.yml"); let m = App::from_yaml(yml).get_matches(); + + if let Some(delay) = m.value_of("DELAY") { + match delay.parse() { + Ok(time) => print_delay(m.value_of("STRING").unwrap(), time), + Err(_) => unreachable!(), + } + } else { + print_no_delay(m.value_of("STRING").unwrap()); + } +} + +fn print_delay(text: &str, delay: f64) { + use std::thread::sleep; + use std::time::{Duration,Instant}; + + let mut time = Instant::now(); + let secs = delay as u64; + let nanos = ((delay - secs as f64) * 1e9) as u32; + let duration = Duration::new(secs, nanos); + loop { + println!("{}", text); + time += duration; + let now = Instant::now(); + if time > now { + sleep(time.duration_since(now)); + } + } +} + +fn print_no_delay(text: &str) { + loop { + println!("{}", text); + } }