Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/remove against hash #4

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::PathBuf;
use std::env;
use std::path::PathBuf;

use clap::{arg, command, value_parser, ArgAction};

Expand Down
18 changes: 13 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use std::path::PathBuf;
use colored::*;
use walkdir::WalkDir;

mod cli;
mod data;
mod fnmatch_regex;
mod p2tree;
mod tprint;
mod pmatcher;
mod pconfig;
mod cli;
mod pmatcher;
mod tprint;
mod util;

fn main() -> std::io::Result<()> {
Expand Down Expand Up @@ -62,7 +62,11 @@ fn main() -> std::io::Result<()> {
if app_options.enable_renaming {
let new_filename = pattern_matcher.clean_filename(filename);
if new_filename != filename {
operation_list.push((filepath.to_path_buf(), new_filename, data::Operation::Rename));
operation_list.push((
filepath.to_path_buf(),
new_filename,
data::Operation::Rename,
));
continue;
}
}
Expand All @@ -78,7 +82,11 @@ fn main() -> std::io::Result<()> {
))
}

operation_list.push((filepath.to_path_buf(), "".to_string(), data::Operation::None));
operation_list.push((
filepath.to_path_buf(),
"".to_string(),
data::Operation::None,
));
}

if app_options.is_debug_mode() {
Expand Down
4 changes: 2 additions & 2 deletions src/p2tree.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::path::{Path,PathBuf};
use std::collections::HashMap;
use std::fs::read_link;
use std::path::{Path, PathBuf};

use nary_tree::{NodeId,TreeBuilder,Tree};
use colored::*;
use nary_tree::{NodeId, Tree, TreeBuilder};

use crate::data::Operation;

Expand Down
32 changes: 14 additions & 18 deletions src/pmatcher.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use std::path::{Path,PathBuf};
use std::collections::HashMap;
use std::fs::File;
use std::io::Read;
use std::path::{Path, PathBuf};

use fancy_regex::Regex;
use md5::{Digest, Md5};

use crate::pconfig;
use crate::fnmatch_regex;
use crate::pconfig;

#[derive(Debug)]
pub struct PatternMatcher {
Expand Down Expand Up @@ -79,31 +79,27 @@ impl PatternMatcher {
fn create_patterns_with_hash(patterns: HashMap<String, Vec<String>>) -> Vec<(Regex, Vec<String>)> {
patterns
.into_iter()
.map(|(key, value)| {
// println!("hash --> {}", key);
(
Regex::new(fnmatch_regex::glob_to_regex_string(&key).as_str()).unwrap(),
value,
)
})
.map(|(key, value)| (parse_mixed_regex(&key), value))
.collect()
}

fn parse_mixed_regex(pattern: &str) -> Regex {
let pattern = pattern.trim();
// println!(">>> {:#?}", pattern);
if let Some(stripped) = pattern.strip_prefix('/') {
Regex::new(stripped).unwrap()
} else {
Regex::new(fnmatch_regex::glob_to_regex_string(pattern).as_str()).unwrap()
}
}

/**
* 创建正则表达式列表,通配符形式转为正则表达式
*/
fn create_mixed_regex_list(patterns: Vec<&str>) -> Vec<Regex> {
patterns
.iter()
.map(|pattern| {
let pattern = pattern.trim();
// println!(">>> {:#?}", pattern);
if let Some(stripped) = pattern.strip_prefix('/') {
Regex::new(stripped).unwrap()
} else {
Regex::new(fnmatch_regex::glob_to_regex_string(pattern).as_str()).unwrap()
}
})
.map(|pattern| parse_mixed_regex(pattern))
.collect()
}

Expand Down
Loading