cargolive 0.1.0

A fast watcher that automatically executes 'cargo run' when it detects changes.
Documentation
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::process::Command;
use std::sync::mpsc::channel;

pub fn startlive() -> notify::Result<()> {
    let (tx, rx) = channel();
    let mut watcher = RecommendedWatcher::new(tx, Config::default())?;
    watcher.watch(".".as_ref(), RecursiveMode::Recursive)?;
    for res in rx {
        match res {
            Ok(event) => {
                if event.kind.is_modify() {
                    let is_valid_change = event.paths.iter().any(|p| {
                        let path_str = p.to_string_lossy();
                        !path_str.contains("target") && !path_str.contains(".git")
                    });

                    if is_valid_change {
                        runcmd();
                    }
                }
            }
            Err(e) => println!("Error: {:?}", e),
        }
    }
    Ok(())
}

fn runcmd() {
    if cfg!(target_os = "windows") {
        let _ = Command::new("cmd").args(["/C", "cls"]).status();
    } else {
        let _ = Command::new("clear").status();
    }
    let _ = Command::new("cargo").arg("run").status();
}