Skip to content

Commit

Permalink
Implemented core functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
JarredAllen committed Aug 3, 2019
1 parent 4d33492 commit 4958c3d
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 4958c3d

Please sign in to comment.