Skip to content

Commit

Permalink
Merge pull request #4 from kenchou/feature/remove-against-hash
Browse files Browse the repository at this point in the history
Feature/remove against hash
  • Loading branch information
kenchou authored Aug 31, 2024
2 parents adfa758 + 418e9f2 commit 0286dcf
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 26 deletions.
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

0 comments on commit 0286dcf

Please sign in to comment.