diff --git a/Cargo.toml b/Cargo.toml index cfe6be9..5d63405 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "dirwatcher" -version = "0.1.0" +version = "0.4.0" authors = ["Matt Green "] [dependencies] diff --git a/src/main.rs b/src/main.rs index e6a7d07..d0184e1 100644 --- a/src/main.rs +++ b/src/main.rs @@ -5,6 +5,9 @@ use std::env; use libc::system; use notify::{Event, RecommendedWatcher, Watcher}; use std::ffi::CString; +use std::fs::File; +use std::io::BufRead; +use std::io::BufReader; use std::path::{Path,PathBuf}; use std::string::String; use std::sync::mpsc::{channel, Receiver, RecvError}; @@ -18,9 +21,12 @@ fn clear() { } -fn ignored(relpath: &Path) -> bool { - if relpath.to_str().unwrap().starts_with(".") { - return true; +fn ignored(relpath: &Path, ignores: &Vec) -> bool { + for i in ignores.iter() { + if relpath.to_str().unwrap().starts_with(i) { + //println!("Ignoring {} because {}", relpath.to_str().unwrap(), i); + return true; + } } false @@ -33,14 +39,33 @@ fn invoke(cmd: &String) { } } -fn wait(rx: &Receiver, cwd: &PathBuf) -> Result { +fn read_gitignore(path: &str) -> Result, 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, cwd: &PathBuf, ignore: &Vec) -> Result { loop { let e = try!(rx.recv()); let ignored = match e.path { Some(ref path) => { let stripped = path.strip_prefix(cwd).unwrap(); - ignored(stripped) + ignored(stripped, &ignore) }, None => false }; @@ -65,6 +90,13 @@ fn main() { let cmd = env::args().nth(1).expect("Argument 1 needs to be a command"); let cwd = env::current_dir().unwrap(); + let mut ignored = vec![]; + ignored.push(String::from(".")); + match read_gitignore(".gitignore") { + Ok(gitignores) => ignored.extend(gitignores), + Err(_) => () + } + let (tx, rx) = channel(); let mut watcher: RecommendedWatcher = Watcher::new(tx) .expect("unable to create watcher"); @@ -72,11 +104,11 @@ fn main() { .expect("unable to start watching directory"); loop { - clear(); - let e = wait(&rx, &cwd) + //clear(); + let e = wait(&rx, &cwd, &ignored) .expect("error when waiting for filesystem changes"); - println!("{:?} {:?}", e.op, e.path); + //println!("{:?} {:?}", e.op, e.path); invoke(&cmd); } }