improve outputs

This commit is contained in:
grimhilt 2023-06-03 18:15:31 +02:00
parent 1a1b98729a
commit b92454522f
2 changed files with 47 additions and 22 deletions

View File

@ -15,6 +15,8 @@ pub fn add(files: Values<'_>) {
let mut index_path = root.clone(); let mut index_path = root.clone();
index_path.push(".nextsync"); index_path.push(".nextsync");
let mut index_file = utils::index::open(index_path); let mut index_file = utils::index::open(index_path);
// todo avoid duplicate
// ./folder ./folder/file
let file_vec: Vec<&str> = files.collect(); let file_vec: Vec<&str> = files.collect();
for file in file_vec { for file in file_vec {

View File

@ -7,6 +7,13 @@ use colored::Colorize;
use std::path::PathBuf; use std::path::PathBuf;
use std::io; use std::io;
#[derive(PartialEq)]
enum RemoveSide {
Left,
Both,
Right,
}
// todo: relative path, filename, get modified // todo: relative path, filename, get modified
pub fn status() { pub fn status() {
let mut hashes = HashSet::new(); let mut hashes = HashSet::new();
@ -24,7 +31,6 @@ pub fn status() {
let mut next_sync_path = root.clone(); let mut next_sync_path = root.clone();
next_sync_path.push(".nextsync"); next_sync_path.push(".nextsync");
if let Ok(lines) = read_head(next_sync_path.clone()) { if let Ok(lines) = read_head(next_sync_path.clone()) {
for line in lines { for line in lines {
if let Ok(ip) = line { if let Ok(ip) = line {
@ -45,9 +51,11 @@ pub fn status() {
} }
} }
find_missing_elements(&mut hashes, &mut objects); let ok = find_missing_elements(&mut hashes, &mut objects, RemoveSide::Both);
dbg!(ok);
if let Ok(entries) = utils::index::read_line(next_sync_path.clone()) { if let Ok(entries) = utils::index::read_line(next_sync_path.clone()) {
for entry in entries { for entry in entries {
// todo hash this
staged_objects.push(String::from(entry.unwrap())); staged_objects.push(String::from(entry.unwrap()));
} }
} }
@ -57,33 +65,39 @@ pub fn status() {
} }
fn print_status(staged_objects: Vec<String>, hashes: HashSet<String>, objects: Vec<String>) { fn print_status(staged_objects: Vec<String>, hashes: HashSet<String>, objects: Vec<String>) {
if staged_objects.len() == 0 {
println!("No staged file"); if staged_objects.len() == 0 && hashes.len() == 0 && objects.len() == 0 {
} else { println!("Nothing to push, working tree clean");
println!("Staged files: "); return;
for staged in staged_objects {
println!("{} {}", String::from("Staged:").green(), staged.green());
}
} }
if objects.len() == 0 { // staged file
println!("No new file"); if staged_objects.len() != 0 {
} else { println!("Changes to be pushed:");
for object in objects { println!(" (Use \"nextsync reset\" to unstage)");
println!("{} {}", String::from("Added:").green(), object.green()); for staged in staged_objects {
println!(" {} {}", String::from("staged:").green(), staged.green());
} }
} }
if hashes.len() != 0 { // not staged files
for hash in hashes { if objects.len() != 0 || hashes.len() != 0 {
println!("{} {}", String::from("Deleted:").red(), hash.red()); println!("Changes not staged for push:");
} println!(" (Use\"nextsync add <file>...\" to update what will be pushed)");
}
for object in objects {
println!(" {} {}", String::from("added:").red(), object.red());
}
for hash in hashes {
println!(" {} {}", String::from("deleted:").red(), hash.red());
} }
} }
fn find_missing_elements(hashes: &mut HashSet<String>, objects: &mut Vec<String>) {
fn find_missing_elements(hashes: &mut HashSet<String>, objects: &mut Vec<String>, remove_option: RemoveSide) -> Vec<String> {
let mut hasher = Sha1::new(); let mut hasher = Sha1::new();
let mut to_remove: Vec<usize> = vec![]; let mut to_remove: Vec<usize> = vec![];
let mut i = 0; let mut i = 0;
let mut duplicate = vec![];
for object in &mut *objects { for object in &mut *objects {
// hash the object // hash the object
@ -93,8 +107,15 @@ fn find_missing_elements(hashes: &mut HashSet<String>, objects: &mut Vec<String>
// find it on the list of hashes // find it on the list of hashes
if hashes.contains(&hash) { if hashes.contains(&hash) {
hashes.remove(&hash); println!("o");
to_remove.push(i); println!("{}", object.clone());
duplicate.push(object.clone());
if remove_option == RemoveSide::Left || remove_option == RemoveSide::Both {
hashes.remove(&hash);
}
if remove_option == RemoveSide::Right || remove_option == RemoveSide::Both {
to_remove.push(i);
}
} }
i += 1; i += 1;
} }
@ -105,6 +126,8 @@ fn find_missing_elements(hashes: &mut HashSet<String>, objects: &mut Vec<String>
objects.remove(index-i); objects.remove(index-i);
i += 1; i += 1;
} }
duplicate
} }
fn is_nextsync_config(path: PathBuf) -> bool { fn is_nextsync_config(path: PathBuf) -> bool {
@ -144,7 +167,7 @@ mod tests {
objects.push(String::from("file1")); objects.push(String::from("file1"));
objects.push(String::from("file2")); objects.push(String::from("file2"));
objects.push(String::from("file3")); objects.push(String::from("file3"));
find_missing_elements(&mut hashes, &mut objects); find_missing_elements(&mut hashes, &mut objects, RemoveSide::Both);
dbg!(hashes.clone()); dbg!(hashes.clone());
dbg!(objects.clone()); dbg!(objects.clone());
assert_eq!(hashes.contains(&hash4), true); assert_eq!(hashes.contains(&hash4), true);