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.
48 lines
1003 B
48 lines
1003 B
use crossbeam_channel::{bounded, Receiver};
|
|
use duration::*;
|
|
use signal_hook::iterator::Signals;
|
|
use signal_hook::SIGINT;
|
|
use std::io::{stdin, 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)
|
|
}
|
|
|
|
pub fn input_handler() -> Result<Receiver<()>> {
|
|
let (s, r) = bounded(1);
|
|
let mut buf = String::new();
|
|
|
|
thread::spawn(move || loop {
|
|
if stdin().read_line(&mut buf).is_err() {
|
|
break;
|
|
}
|
|
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")
|
|
}
|