5 changed files with 99 additions and 10 deletions
			
			
		- 
					23Cargo.lock
 - 
					1Cargo.toml
 - 
					19src/duration.rs
 - 
					32src/lib.rs
 - 
					34src/main.rs
 
@ -0,0 +1,19 @@ | 
				
			|||
use std::convert::From;
 | 
				
			|||
use std::{fmt, time};
 | 
				
			|||
 | 
				
			|||
#[derive(Debug)]
 | 
				
			|||
pub struct FmtDuration(time::Duration);
 | 
				
			|||
 | 
				
			|||
impl fmt::Display for FmtDuration {
 | 
				
			|||
    fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
 | 
				
			|||
        let secs = self.0.as_secs();
 | 
				
			|||
        let millis = self.0.subsec_millis();
 | 
				
			|||
        write!(f, "{:02}:{:02}:{:03}", secs / 60, secs % 60, millis)
 | 
				
			|||
    }
 | 
				
			|||
}
 | 
				
			|||
 | 
				
			|||
impl From<time::Duration> for FmtDuration {
 | 
				
			|||
    fn from(dur: time::Duration) -> Self {
 | 
				
			|||
        FmtDuration(dur)
 | 
				
			|||
    }
 | 
				
			|||
}
 | 
				
			|||
@ -0,0 +1,32 @@ | 
				
			|||
use crossbeam_channel::{bounded, Receiver};
 | 
				
			|||
use duration::*;
 | 
				
			|||
use signal_hook::iterator::Signals;
 | 
				
			|||
use signal_hook::SIGINT;
 | 
				
			|||
use std::io::{Result, StdoutLock, Write};
 | 
				
			|||
use std::thread;
 | 
				
			|||
 | 
				
			|||
pub mod duration;
 | 
				
			|||
 | 
				
			|||
pub fn signal_handler() -> Result<Receiver<()>> {
 | 
				
			|||
    let (s, r) = bounded(1);
 | 
				
			|||
    let signals = Signals::new(&[SIGINT])?;
 | 
				
			|||
 | 
				
			|||
    thread::spawn(move || {
 | 
				
			|||
        for _ in signals.forever() {
 | 
				
			|||
            if s.send(()).is_err() {
 | 
				
			|||
                break;
 | 
				
			|||
            }
 | 
				
			|||
        }
 | 
				
			|||
    });
 | 
				
			|||
 | 
				
			|||
    Ok(r)
 | 
				
			|||
}
 | 
				
			|||
 | 
				
			|||
/// Resets the current line on stdout
 | 
				
			|||
///
 | 
				
			|||
/// # Arguments
 | 
				
			|||
///
 | 
				
			|||
/// * w - A reference to the locked stdout handle
 | 
				
			|||
pub fn reset_line(w: &mut StdoutLock) -> Result<()> {
 | 
				
			|||
    write!(w, "\r[2K")
 | 
				
			|||
}
 | 
				
			|||
						Write
						Preview
					
					
					Loading…
					
					Cancel
						Save
					
		Reference in new issue