Finishes cache management
This commit is contained in:
13
src/lib.rs
13
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<String>);
|
||||
|
||||
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);
|
||||
|
||||
50
src/pgd.rs
50
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]);
|
||||
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user