2016-09-14 15:30:59 +02:00
|
|
|
extern crate notify;
|
|
|
|
extern crate libc;
|
|
|
|
|
2016-09-14 22:11:35 +02:00
|
|
|
use std::env;
|
2016-09-14 15:30:59 +02:00
|
|
|
use libc::system;
|
2016-09-14 21:43:58 +02:00
|
|
|
use notify::{Event, RecommendedWatcher, Watcher};
|
2016-09-14 15:30:59 +02:00
|
|
|
use std::ffi::CString;
|
2016-09-14 22:50:14 +02:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::BufRead;
|
|
|
|
use std::io::BufReader;
|
2016-09-14 22:11:35 +02:00
|
|
|
use std::path::{Path,PathBuf};
|
2016-09-14 15:30:59 +02:00
|
|
|
use std::string::String;
|
2016-09-14 21:43:58 +02:00
|
|
|
use std::sync::mpsc::{channel, Receiver, RecvError};
|
|
|
|
use std::{thread, time};
|
|
|
|
|
|
|
|
fn clear() {
|
|
|
|
let s = CString::new("clear").unwrap();
|
|
|
|
unsafe {
|
|
|
|
system(s.as_ptr());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2016-09-14 15:30:59 +02:00
|
|
|
|
2016-09-14 22:50:14 +02:00
|
|
|
fn ignored(relpath: &Path, ignores: &Vec<String>) -> bool {
|
|
|
|
for i in ignores.iter() {
|
|
|
|
if relpath.to_str().unwrap().starts_with(i) {
|
|
|
|
//println!("Ignoring {} because {}", relpath.to_str().unwrap(), i);
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-14 22:11:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2016-09-14 15:30:59 +02:00
|
|
|
fn invoke(cmd: &String) {
|
|
|
|
let s = CString::new(cmd.clone()).unwrap();
|
|
|
|
unsafe {
|
|
|
|
system(s.as_ptr());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-14 22:50:14 +02:00
|
|
|
fn read_gitignore(path: &str) -> Result<Vec<String>, std::io::Error> {
|
|
|
|
let f = try!(File::open(path));
|
|
|
|
let reader = BufReader::new(f);
|
|
|
|
|
|
|
|
let mut entries = vec![];
|
|
|
|
for line in reader.lines() {
|
|
|
|
let l = try!(line).trim().to_string();
|
|
|
|
|
|
|
|
if l.starts_with("#") || l.len() == 0 {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
//println!("Read {}", l);
|
|
|
|
entries.push(l);
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(entries)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn wait(rx: &Receiver<Event>, cwd: &PathBuf, ignore: &Vec<String>) -> Result<Event, RecvError> {
|
2016-09-14 22:11:35 +02:00
|
|
|
loop {
|
|
|
|
let e = try!(rx.recv());
|
|
|
|
|
|
|
|
let ignored = match e.path {
|
|
|
|
Some(ref path) => {
|
|
|
|
let stripped = path.strip_prefix(cwd).unwrap();
|
2016-09-14 22:50:14 +02:00
|
|
|
ignored(stripped, &ignore)
|
2016-09-14 22:11:35 +02:00
|
|
|
},
|
|
|
|
None => false
|
|
|
|
};
|
2016-09-14 21:43:58 +02:00
|
|
|
|
2016-09-14 22:11:35 +02:00
|
|
|
if ignored {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-14 21:43:58 +02:00
|
|
|
|
2016-09-14 22:11:35 +02:00
|
|
|
thread::sleep(time::Duration::from_millis(250));
|
|
|
|
loop {
|
|
|
|
match rx.try_recv() {
|
|
|
|
Ok(_) => continue,
|
|
|
|
Err(_) => break,
|
|
|
|
}
|
2016-09-14 21:43:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-14 22:11:35 +02:00
|
|
|
return Ok(e);
|
|
|
|
}
|
2016-09-14 21:43:58 +02:00
|
|
|
}
|
|
|
|
|
2016-09-14 15:30:59 +02:00
|
|
|
fn main() {
|
2016-09-14 22:11:35 +02:00
|
|
|
let cmd = env::args().nth(1).expect("Argument 1 needs to be a command");
|
|
|
|
let cwd = env::current_dir().unwrap();
|
2016-09-14 15:30:59 +02:00
|
|
|
|
2016-09-14 22:50:14 +02:00
|
|
|
let mut ignored = vec![];
|
|
|
|
ignored.push(String::from("."));
|
|
|
|
match read_gitignore(".gitignore") {
|
|
|
|
Ok(gitignores) => ignored.extend(gitignores),
|
|
|
|
Err(_) => ()
|
|
|
|
}
|
|
|
|
|
2016-09-14 15:30:59 +02:00
|
|
|
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 {
|
2016-09-14 22:50:14 +02:00
|
|
|
//clear();
|
|
|
|
let e = wait(&rx, &cwd, &ignored)
|
2016-09-14 21:43:58 +02:00
|
|
|
.expect("error when waiting for filesystem changes");
|
|
|
|
|
2016-09-14 22:50:14 +02:00
|
|
|
//println!("{:?} {:?}", e.op, e.path);
|
2016-09-14 21:43:58 +02:00
|
|
|
invoke(&cmd);
|
2016-09-14 15:30:59 +02:00
|
|
|
}
|
|
|
|
}
|