Skip to content

Commit

Permalink
Add depth argument to overwrite inferred subdirectory depth value in …
Browse files Browse the repository at this point in the history
…'regex' mode
  • Loading branch information
yaa110 committed Apr 3, 2021
1 parent 88ca0f6 commit 65457f4
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 93 deletions.
106 changes: 19 additions & 87 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ license = "MIT OR Apache-2.0"
name = "nomino"
readme = "README.md"
repository = "https://github.com/yaa110/nomino"
version = "0.4.6"
version = "0.5.0"

[dependencies]
atty = "0.2"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ FLAGS:
-V, --version Prints version information

OPTIONS:
--depth <DEPTH> Optional value to overwrite inferred depth value in 'regex' mode
-d, --dir <PATH> Sets the working directory
-g, --generate <PATH> Stores a JSON map file in '<PATH>' after renaming files
-m, --map <PATH> Sets the path of map file to be used for renaming files
Expand Down
6 changes: 6 additions & 0 deletions data/opts-template.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ args:
multiple: false
takes_value: true
value_name: PATH
- depth:
long: depth
help: Optional value to overwrite inferred subdirectory depth value in 'regex' mode
multiple: false
takes_value: true
value_name: DEPTH
- generate:
short: g
long: generate
Expand Down
4 changes: 2 additions & 2 deletions src/input/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ pub enum Source {
}

impl Source {
pub fn new_regex(pattern: &str) -> Result<Self, Box<dyn Error>> {
pub fn new_regex(pattern: &str, depth: Option<usize>) -> Result<Self, Box<dyn Error>> {
Ok(Self::Regex(
Regex::new(pattern)?,
pattern.chars().filter(|c| *c == MAIN_SEPARATOR).count() + 1,
depth.unwrap_or(pattern.chars().filter(|c| *c == MAIN_SEPARATOR).count() + 1),
))
}

Expand Down
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use std::path::Path;
use std::process::exit;

fn read_source(
regex: Option<&str>,
regex: Option<(&str, Option<usize>)>,
sort: Option<&str>,
map: Option<&str>,
) -> Result<Source, Box<dyn Error>> {
match (regex, sort, map) {
(Some(pattern), _, _) => Source::new_regex(pattern),
(Some((pattern, depth)), _, _) => Source::new_regex(pattern, depth),
(_, Some(order), _) => Source::new_sort(order),
(_, _, Some(filename)) => Source::new_map(filename),
_ => {
Expand Down Expand Up @@ -124,9 +124,12 @@ fn run_app() -> Result<bool, Box<dyn Error>> {
if let Some(cwd) = opts.value_of("directory").map(Path::new) {
set_current_dir(cwd)?;
}
let depth = opts
.value_of("depth")
.and_then(|depth| depth.parse::<usize>().ok());
let input_iter = InputIterator::new(
read_source(
opts.value_of("regex"),
opts.value_of("regex").map(|pattern| (pattern, depth)),
opts.value_of("sort"),
opts.value_of("map"),
)?,
Expand Down
62 changes: 62 additions & 0 deletions tests/regex_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,68 @@ fn test_regex_subdir() {
dir.close().unwrap();
}

#[cfg(not(target_os = "windows"))]
#[test]
fn test_regex_subdir_depth() {
let dir = TempDir::new("nomino_test").unwrap();

create_dir_all(dir.path().join("s1")).unwrap();
create_dir_all(dir.path().join("s2")).unwrap();
create_dir_all(dir.path().join("a")).unwrap();

let inputs = vec![
("s1", "Nomino (2020) S1.E1.1080p.mkv"),
("s1", "Nomino (2020) S1.E2.1080p.mkv"),
("s2", "Nomino (2020) S1.E3.1080p.mkv"),
("s2", "Nomino (2020) S1.E4.1080p.mkv"),
("a", "Nomino (2020) S1.E5.1080p.mkv"),
];

let mut outputs_01 = vec!["01.mkv", "02.mkv"];
let mut outputs_02 = vec!["03.mkv", "04.mkv"];

for (d, input) in inputs {
let _ = File::create(dir.path().join(d).join(input)).unwrap();
}

let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"--depth",
"2",
"-d",
dir.path().to_str().unwrap(),
"-k",
"-r",
format!(r"s(\d+){}.*E(\d+).*", MAIN_SEPARATOR).as_str(),
format!("{{:2}}{}{{:2}}.mkv", MAIN_SEPARATOR).as_str(),
])
.unwrap();

let mut files_01: Vec<String> = read_dir(dir.path().join("01"))
.unwrap()
.map(|entry| entry.unwrap().file_name().to_str().unwrap().to_string())
.collect();

let mut files_02: Vec<String> = read_dir(dir.path().join("02"))
.unwrap()
.map(|entry| entry.unwrap().file_name().to_str().unwrap().to_string())
.collect();

files_01.sort();
files_02.sort();
outputs_01.sort();
outputs_02.sort();

assert!(cmd.status.success());
assert_eq!(files_01.len(), outputs_01.len());
assert!(outputs_01.iter().zip(files_01.iter()).all(|(a, b)| a == b));
assert_eq!(files_02.len(), outputs_02.len());
assert!(outputs_02.iter().zip(files_02.iter()).all(|(a, b)| a == b));

dir.close().unwrap();
}

#[cfg(not(target_os = "windows"))]
#[test]
fn test_regex_subdir_not_overwrite() {
Expand Down

0 comments on commit 65457f4

Please sign in to comment.