From 14183da3aec26db80ce265d0b0b57c9f3c4f508f Mon Sep 17 00:00:00 2001 From: grimhilt Date: Thu, 1 Jun 2023 22:48:09 +0200 Subject: [PATCH] status working without filename --- Cargo.lock | 12 ++++++++ Cargo.toml | 1 + src/commands/status.rs | 62 +++++++++++++++++++++++++----------------- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b668afc..f8bc798 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -79,6 +79,17 @@ dependencies = [ "vec_map", ] +[[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + [[package]] name = "core-foundation" version = "0.9.3" @@ -501,6 +512,7 @@ name = "nextsync" version = "0.1.0" dependencies = [ "clap", + "colored", "dotenv", "reqwest", "rust-crypto", diff --git a/Cargo.toml b/Cargo.toml index 939ffd4..09590b1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,4 @@ tokio = { version = "1", features = ["full"] } dotenv ="0.15.0" clap = "2.33" rust-crypto = "0.2.36" +colored = "2.0.0" diff --git a/src/commands/status.rs b/src/commands/status.rs index b0ff8bc..9ef0666 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -6,20 +6,14 @@ use std::path::PathBuf; use crypto::digest::Digest; use crypto::sha1::Sha1; use std::collections::HashSet; +use colored::Colorize; +use std::fs; - +// todo: relative path, filename pub fn status() { - let mut a = vec![]; - - { - let b = 2; - a.push(b.clone()); - } - - println!("Vector a: {:?}", a); let mut new_files: Vec = Vec::new(); let mut hashes = HashSet::new(); - let mut objects: Vec<&str> = vec![]; + let mut objects: Vec = vec![]; let path = env::current_dir().unwrap(); let mut next_sync_path = path.clone(); @@ -29,7 +23,10 @@ pub fn status() { if let Ok(lines) = read_head(next_sync_path.clone()) { for line in lines { if let Ok(ip) = line { - hashes.insert(String::from(ip).as_str()); + dbg!(ip.clone().len()); + if ip.clone().len() > 5 { + hashes.insert(String::from(ip)); + } } } } @@ -38,17 +35,34 @@ pub fn status() { for entry in entries { if !is_nextsync_config(entry.clone()) { let object_path = entry.strip_prefix(path.clone()).unwrap(); - objects.push(object_path.to_str().unwrap().clone()); + objects.push(String::from(object_path.to_str().unwrap())); } } } find_missing_elements(&mut hashes, &mut objects); + print_status(hashes.clone(), objects.clone()); dbg!(hashes); dbg!(objects); } -fn find_missing_elements(hashes: &mut HashSet<&str>, objects: &mut Vec<&str>) { +fn print_status(hashes: HashSet, objects: Vec) { + if objects.len() == 0 { + println!("No new file"); + } else { + for object in objects { + println!("{} {}", String::from("Added:").green(), object.green()); + } + } + + if hashes.len() != 0 { + for hash in hashes { + println!("{} {}", String::from("Deleted:").red(), hash.red()); + } + } +} + +fn find_missing_elements(hashes: &mut HashSet, objects: &mut Vec) { let mut hasher = Sha1::new(); let mut to_remove: Vec = vec![]; let mut i = 0; @@ -60,8 +74,8 @@ fn find_missing_elements(hashes: &mut HashSet<&str>, objects: &mut Vec<&str>) { hasher.reset(); // find it on the list of hashes - if hashes.contains(hash.as_str()) { - hashes.remove(hash.as_str()); + if hashes.contains(&hash) { + hashes.remove(&hash); to_remove.push(i); } i += 1; @@ -84,8 +98,6 @@ fn read_head(mut path: PathBuf) -> io::Result>> { read_lines(path) } -use std::fs; - fn read_folder(path: PathBuf) -> io::Result> { let mut entries = fs::read_dir(path)? .map(|res| res.map(|e| e.path())) @@ -123,18 +135,18 @@ mod tests { hasher.reset(); let mut hashes = HashSet::new(); - hashes.insert(hash1.as_str()); - hashes.insert(hash2.as_str()); - hashes.insert(hash4.as_str()); + hashes.insert(hash1.clone()); + hashes.insert(hash2.clone()); + hashes.insert(hash4.clone()); - let mut objects: Vec<&str> = vec![]; - objects.push("file1"); - objects.push("file2"); - objects.push("file3"); + let mut objects: Vec = vec![]; + objects.push(String::from("file1")); + objects.push(String::from("file2")); + objects.push(String::from("file3")); find_missing_elements(&mut hashes, &mut objects); dbg!(hashes.clone()); dbg!(objects.clone()); - assert_eq!(hashes.contains(hash4.as_str()), true); + assert_eq!(hashes.contains(&hash4), true); assert_eq!(hashes.len(), 1); assert_eq!(objects, vec!["file3"]); }