diff --git a/Cargo.lock b/Cargo.lock index cc4e1f3..7604f86 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -11,6 +11,7 @@ name = "gclone" version = "0.1.0" dependencies = [ "colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)", + "walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)", ] [[package]] @@ -18,6 +19,57 @@ name = "lazy_static" version = "1.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "same-file" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "walkdir" +version = "2.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi" +version = "0.3.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-util" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] "checksum colored 1.7.0 (registry+https://github.com/rust-lang/crates.io-index)" = "6e9a455e156a4271e12fd0246238c380b1e223e3736663c7a18ed8b6362028a9" "checksum lazy_static 1.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a374c89b9db55895453a74c1e38861d9deec0b01b405a82516e9d5de4820dea1" +"checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267" +"checksum walkdir 2.2.7 (registry+https://github.com/rust-lang/crates.io-index)" = "9d9d7ed3431229a144296213105a390676cc49c9b6a72bd19f3176c98e129fa1" +"checksum winapi 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)" = "92c1eb33641e276cfa214a0522acad57be5c56b10cb348b3c5117db75f3ac4b0" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-util 0.1.2 (registry+https://github.com/rust-lang/crates.io-index)" = "7168bab6e1daee33b4557efd0e95d5ca70a03706d39fa5f3fe7a236f584b03c9" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 76320e7..d7c04fa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,6 +6,7 @@ edition = "2018" [dependencies] colored = "1.7.0" +walkdir = "2.2.7" [[bin]] name = "gclone" diff --git a/src/lib.rs b/src/lib.rs index 7328637..d76ca8c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,6 +6,7 @@ use std::fs::File; use std::io::{Write, BufRead, BufReader}; use colored::*; +use walkdir::WalkDir; pub fn git_dir() -> String { match env::var("GCLONE_PATH") { @@ -24,6 +25,16 @@ pub fn git_dir() -> String { pub struct Cache(Vec); impl Cache { + + pub fn new() -> Cache { + Cache(WalkDir::new(git_dir()) + .max_depth(3) + .into_iter() + .filter_map(|x| x.ok()) + .map(|x| x.path().display().to_string()) + .collect()) + } + pub fn read() -> Cache { let mut path = PathBuf::from(git_dir()); @@ -31,7 +42,7 @@ impl Cache { let file = match File::open(&path) { Ok(f) => f, - Err(_) => return Cache(vec![]), + Err(_) => return Cache::new(), }; let file = BufReader::new(file); diff --git a/src/pgd.rs b/src/pgd.rs index 603dec2..632e88a 100644 --- a/src/pgd.rs +++ b/src/pgd.rs @@ -11,8 +11,6 @@ fn flush_stdout() { } fn main() { - let cache = Cache::read(); - let request = match env::args().nth(1) { Some(arg) => arg, None => { @@ -24,15 +22,44 @@ fn main() { }, }; + main_with_cache(&request, true); +} + +fn main_with_cache(request: &str, regen: bool) { + + let cache = Cache::read(); + let matches = cache.find(&request); match matches.len() { 0 => { - eprintln!("{} {}", - "error:".bold().red(), - "no such directory".bold()); - exit(1); + if regen { + eprintln!("{} {}", + "warning:".bold().yellow(), + "directory not found, regenerating cache...".bold()); + + let cache = Cache::new(); + + match cache.write() { + Ok(_) => (), + Err(e) => { + eprintln!("{} {} {}", + "warning:".bold().yellow(), + "couldn't write cache:".bold(), + e.description()); + }, + } + + return main_with_cache(request, false); + + } else { + eprintln!("{} {}", + "error:".bold().red(), + "no such directory".bold()); + + exit(1); + } }, 1 => { @@ -41,15 +68,15 @@ fn main() { len => { - eprintln!("{} {}", - "info:".bold(), - "multiple entries found, please select one"); + eprintln!("{}", "info: multiple entries found, please select one".bold()); for (i, j) in matches.iter().enumerate() { - eprintln!("[{}] {}", i + 1, j); + eprintln!("{} {}", + &format!("[{}]", i + 1).bold().blue(), + &format!("{}", j).yellow()); } - eprint!("Enter your choice: "); + eprint!("{}", "Enter your choice: ".bold()); flush_stdout(); let mut string = String::new(); @@ -90,7 +117,6 @@ fn main() { } println!("{}", matches[value]); - }, }