watchexec/src/main.rs

37 lines
944 B
Rust
Raw Normal View History

2016-09-14 15:30:59 +02:00
extern crate notify;
extern crate libc;
use libc::system;
use notify::{RecommendedWatcher, Watcher};
use std::ffi::CString;
use std::string::String;
use std::sync::mpsc::channel;
fn invoke(cmd: &String) {
let s = CString::new(cmd.clone()).unwrap();
unsafe {
system(s.as_ptr());
}
}
fn main() {
let cmd = std::env::args().nth(1).expect("Argument 1 needs to be a command");
let (tx, rx) = channel();
let mut watcher: RecommendedWatcher = Watcher::new(tx)
.expect("unable to create watcher");
2016-09-14 20:19:05 +02:00
watcher.watch(".")
2016-09-14 15:30:59 +02:00
.expect("unable to start watching directory");
loop {
match rx.recv() {
2016-09-14 20:19:05 +02:00
Ok(notify::Event{ path: Some(path), op:Ok(op) }) => {
2016-09-14 15:30:59 +02:00
println!("{:?} {:?}", op, path);
invoke(&cmd);
},
Ok(event) => println!("broken event: {:?}", event),
Err(e) => println!("watch error: {}", e),
}
}
}