mirror of
https://github.com/watchexec/watchexec.git
synced 2024-11-13 07:41:11 +01:00
Simple screen clear & debounce
This commit is contained in:
parent
37f0b062a5
commit
08d21a6dad
1 changed files with 34 additions and 10 deletions
44
src/main.rs
44
src/main.rs
|
@ -1,11 +1,22 @@
|
|||
extern crate notify;
|
||||
extern crate libc;
|
||||
|
||||
use std::io;
|
||||
use std::io::Write;
|
||||
use libc::system;
|
||||
use notify::{RecommendedWatcher, Watcher};
|
||||
use notify::{Event, RecommendedWatcher, Watcher};
|
||||
use std::ffi::CString;
|
||||
use std::string::String;
|
||||
use std::sync::mpsc::channel;
|
||||
use std::sync::mpsc::{channel, Receiver, RecvError};
|
||||
use std::{thread, time};
|
||||
|
||||
fn clear() {
|
||||
let s = CString::new("clear").unwrap();
|
||||
unsafe {
|
||||
system(s.as_ptr());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn invoke(cmd: &String) {
|
||||
let s = CString::new(cmd.clone()).unwrap();
|
||||
|
@ -14,6 +25,21 @@ fn invoke(cmd: &String) {
|
|||
}
|
||||
}
|
||||
|
||||
fn wait(rx: &Receiver<Event>) -> Result<Event, RecvError> {
|
||||
let e = try!(rx.recv());
|
||||
|
||||
thread::sleep(time::Duration::from_millis(250));
|
||||
|
||||
loop {
|
||||
match rx.try_recv() {
|
||||
Ok(_) => continue,
|
||||
Err(_) => break,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(e)
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let cmd = std::env::args().nth(1).expect("Argument 1 needs to be a command");
|
||||
|
||||
|
@ -24,13 +50,11 @@ fn main() {
|
|||
.expect("unable to start watching directory");
|
||||
|
||||
loop {
|
||||
match rx.recv() {
|
||||
Ok(notify::Event{ path: Some(path), op:Ok(op) }) => {
|
||||
println!("{:?} {:?}", op, path);
|
||||
invoke(&cmd);
|
||||
},
|
||||
Ok(event) => println!("broken event: {:?}", event),
|
||||
Err(e) => println!("watch error: {}", e),
|
||||
}
|
||||
//clear();
|
||||
let e = wait(&rx)
|
||||
.expect("error when waiting for filesystem changes");
|
||||
|
||||
println!("{:?} {:?}", e.op, e.path);
|
||||
invoke(&cmd);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue