From cbbf185b1a0278b09436eeecad0da8c48e661363 Mon Sep 17 00:00:00 2001 From: grimhilt Date: Fri, 16 Jun 2023 18:49:08 +0200 Subject: [PATCH] use retain --- src/commands/add.rs | 1 - src/commands/status.rs | 47 +++++++++++++------------------------ src/utils/nextsyncignore.rs | 6 ++--- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/src/commands/add.rs b/src/commands/add.rs index d5de5a9..bdbcf88 100644 --- a/src/commands/add.rs +++ b/src/commands/add.rs @@ -3,7 +3,6 @@ use crate::utils::{self, nextsyncignore}; use crate::store; use std::path::{Path, PathBuf}; use std::io::Write; -use glob::glob; pub struct AddArgs<'a> { pub files: Values<'a>, diff --git a/src/commands/status.rs b/src/commands/status.rs index 8d2c26f..082bb28 100644 --- a/src/commands/status.rs +++ b/src/commands/status.rs @@ -76,23 +76,14 @@ fn get_staged(objs: &mut Vec) -> Vec { } } - let mut to_remove: Vec = vec![]; - let mut index = 0; - for obj in &mut *objs { - dbg!(obj.clone().path.to_str().unwrap()); + objs.retain(|obj| { if indexes.contains(obj.clone().path.to_str().unwrap()) { staged_objs.push(obj.clone()); - to_remove.push(index); + false + } else { + true } - index += 1; - } - - let mut offset = 0; - for i in to_remove { - objs.remove(i + offset.clone()); - offset += 1; - } - + }); staged_objs } @@ -260,35 +251,29 @@ fn print_staged_object(obj: Obj) { fn remove_duplicate(hashes: &mut HashMap, objects: &mut Vec, remove_option: RemoveSide) -> Vec { let mut hasher = Sha1::new(); - let mut to_remove: Vec = vec![]; - let mut i = 0; let mut duplicate = vec![]; - for object in &mut *objects { + objects.retain(|obj| { // hash the object - hasher.input_str(object); + hasher.input_str(obj); let hash = hasher.result_str(); hasher.reset(); // find it on the list of hashes if hashes.contains_key(&hash) { - duplicate.push(object.clone()); + duplicate.push(obj.clone()); + + // remove from hashes 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; - } - // remove all objects existing in the list of hashes - i = 0; - for index in to_remove { - objects.remove(index-i); - i += 1; - } + // remove from objects + remove_option != RemoveSide::Right && remove_option != RemoveSide::Both + } else { + true + } + }); duplicate } diff --git a/src/utils/nextsyncignore.rs b/src/utils/nextsyncignore.rs index 642de50..2957c36 100644 --- a/src/utils/nextsyncignore.rs +++ b/src/utils/nextsyncignore.rs @@ -1,7 +1,7 @@ -use crate::utils::{read, path}; +use crate::utils::path; use regex::Regex; use std::fs::File; -use std::io::{Cursor, Lines, BufReader, empty, BufRead}; +use std::io::{Cursor, BufReader, BufRead}; fn read_lines() -> Result, ()> { if let Some(path) = path::nextsyncignore() { @@ -56,7 +56,7 @@ fn normalize_rule(l: String) -> String { pub fn ignore_file(path: &String, lines: Vec, ignored_f: &mut Vec) -> bool { let mut ignored = false; - for mut line in lines { + for line in lines { if line.starts_with("!") { if !ignored { continue;