~cypheon/livetools

a6225c922a43d6180e7eba239f28fd222606c71a — Johann Rudloff 2 years ago e368036
Split into two separate binaries.
3 files changed, 36 insertions(+), 22 deletions(-)

A src/bin/ldate.rs
R src/{main.rs => bin/lwc.rs}
A src/lib.rs
A src/bin/ldate.rs => src/bin/ldate.rs +31 -0
@@ 0,0 1,31 @@
extern crate livetools;

use chrono::{Local};
use std::time::{Duration};
use std::io::prelude::*;
use std::io;
use std::thread;

use livetools::{CLEAR_LINE};

fn live_date() {
    loop {
        let now = Local::now();
        let time_fmt = "%Y-%m-%d %H:%M:%S%.3f %Z";
        //let time_fmt = "%a %b %e %T %Z %Y";
        //let time_fmt = "%+";
        let time_str = now.format(time_fmt).to_string();
        io::stdout().write(
            format!("{}{}", CLEAR_LINE, time_str).as_bytes()
            ).expect("Write failed");
        io::stdout().flush().expect("Flush failed");

        let nanos_elapsed = Local::now().timestamp_subsec_nanos() % 1_000_000_000;
        let nanos_until_next_full_second = 1_000_000_000 - nanos_elapsed;
        thread::sleep(Duration::new(0, nanos_until_next_full_second));
    }
}

fn main() {
    live_date()
}

R src/main.rs => src/bin/lwc.rs +4 -22
@@ 1,28 1,10 @@
use chrono::{Local};
use std::time::{Duration, Instant};
extern crate livetools;

use std::time::{Instant};
use std::io::prelude::*;
use std::io;
use std::thread;

const CLEAR_LINE: &'static str = "\r\x1b[K";

fn live_date() {
    loop {
        let now = Local::now();
        let time_fmt = "%Y-%m-%d %H:%M:%S%.3f %Z";
        //let time_fmt = "%a %b %e %T %Z %Y";
        //let time_fmt = "%+";
        let time_str = now.format(time_fmt).to_string();
        io::stdout().write(
            format!("{}{}", CLEAR_LINE, time_str).as_bytes()
            ).expect("Write failed");
        io::stdout().flush().expect("Flush failed");

        let nanos_elapsed = Local::now().timestamp_subsec_nanos() % 1_000_000_000;
        let nanos_until_next_full_second = 1_000_000_000 - nanos_elapsed;
        thread::sleep(Duration::new(0, nanos_until_next_full_second));
    }
}
use livetools::*;

#[derive(Debug, Copy, Clone)]
struct WordCount {

A src/lib.rs => src/lib.rs +1 -0
@@ 0,0 1,1 @@
pub const CLEAR_LINE: &'static str = "\r\x1b[K";