Skip to content

Commit

Permalink
remove -e flag and add -E
Browse files Browse the repository at this point in the history
add --from-file alias for --map

remove -p flag and add -q
  • Loading branch information
yaa110 committed Jan 13, 2025
1 parent ead5cf5 commit 1426f43
Show file tree
Hide file tree
Showing 9 changed files with 74 additions and 40 deletions.
55 changes: 30 additions & 25 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

Batch rename utility for developers

![Alt text](/screenshots/usage.png?raw=true "Regex Screenshot")
![Alt text](/screenshots/regex.png?raw=true "Example Screenshot")

## How to install

Expand All @@ -28,30 +28,35 @@ If you prefer to build nomino manually, or a pre-compiled executable is not prov
## Usage

```bash
USAGE:
nomino [FLAGS] [OPTIONS] [[SOURCE] OUTPUT]...

FLAGS:
-e, --extension Preserves the extension of input files in 'sort' and 'regex' options
-h, --help Prints help information
-k, --mkdir Recursively creates all parent directories of '<OUTPUT>' if they are missing
-w, --overwrite Overwrites output files, otherwise, a '_' is prepended to filename
-p, --print Prints the map table to stdout
-t, --test Runs in test mode without renaming actual files [aliases: dry-run]
--dry-run Alias for --test
-V, --version Prints version information

OPTIONS:
--depth <DEPTH> Optional value to overwrite inferred subdirectory depth value in 'regex' mode
--max-depth <DEPTH> Optional value to set the maximum of subdirectory 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
-r, --regex <PATTERN> Regex pattern to match by filenames
-s, --sort <ORDER> Sets the order of natural sorting (by name) to rename files using enumerator [possible values: ASC, DESC]

ARGS:
<[SOURCE] OUTPUT>... OUTPUT is the pattern to be used for renaming files, and SOURCE is the optional regex pattern to match by filenames. SOURCE has the same function as -r option
Usage:
nomino [OPTIONS] [[SOURCE] OUTPUT]...

Arguments:
[[SOURCE] OUTPUT]...
OUTPUT is the pattern to be used for renaming files, and SOURCE is the optional regex pattern to match by filenames. SOURCE has the same function as -r option

Options:
-d, --dir <PATH> Sets the working directory
--depth <DEPTH> Optional value to overwrite inferred subdirectory depth value in 'regex' mode
-E, --no-extension Does not preserve the extension of input files in 'sort' and 'regex' options
-g, --generate <PATH> Stores a JSON map file in '<PATH>' after renaming files
-h, --help Print help (see a summary with '-h')
-k, --mkdir Recursively creates all parent directories of '<OUTPUT>' if they are missing
-m, --map <PATH> Sets the path of map file to be used for renaming files
--from-file <PATH> Alias for --map
--max-depth <DEPTH> Optional value to set the maximum of subdirectory depth value in 'regex' mode
-q, --quiet Does not print the map table to stdout
-r, --regex <PATTERN> Regex pattern to match by filenames
-s, --sort <ORDER> Sets the order of natural sorting (by name) to rename files using enumerator
Possible ORDER values:
- asc: Sort in ascending order
- desc: Sort in descending order
-t, --test Runs in test mode without renaming actual files
--dry-run Alias for --test
-V, --version Print version
-w, --overwrite Overwrites output files, otherwise, a '_' is prepended to filename

OUTPUT pattern accepts placeholders that have the format of '{I:P}' where 'I' is the index of captured group and 'P' is the padding of digits with `0`. Please refer to https://github.com/yaa110/nomino for more information.
```
## Map file format
Expand Down
Binary file added screenshots/regex.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed screenshots/usage.png
Binary file not shown.
12 changes: 6 additions & 6 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ pub struct Cli {
/// Overwrites output files, otherwise, a '_' is prepended to filename.
#[arg(short = 'w', long)]
pub overwrite: bool,
/// Preserves the extension of input files in 'sort' and 'regex' options.
#[arg(short, long)]
pub extension: bool,
/// Does not preserve the extension of input files in 'sort' and 'regex' options.
#[arg(short = 'E', long = "no-extension")]
pub no_extension: bool,
/// Sets the working directory.
#[arg(short, long = "dir", value_name = "PATH")]
pub directory: Option<PathBuf>,
Expand All @@ -36,11 +36,11 @@ pub struct Cli {
/// Stores a JSON map file in '<PATH>' after renaming files.
#[arg(short, long, value_name = "PATH")]
pub generate: Option<PathBuf>,
/// Prints the map table to stdout.
/// Does not print the map table to stdout.
#[arg(short, long)]
pub print: bool,
pub quiet: bool,
/// Sets the path of map file to be used for renaming files.
#[arg(short, long, value_name = "PATH")]
#[arg(short, long, value_name = "PATH", visible_alias = "from-file")]
pub map: Option<PathBuf>,
/// Sets the order of natural sorting (by name) to rename files using enumerator.
#[arg(short, long, value_name = "ORDER", ignore_case = true)]
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,15 @@ fn rename_files(
fn print_map_table(map: Map<String, Value>) {
let mut table = Table::new();
table.set_format(*format::consts::FORMAT_NO_LINESEP_WITH_TITLE);
table.set_titles(row!["Input".cyan(), "Output".cyan()]);
table.set_titles(row![Fc => "Input", "Output"]);
map.into_iter()
.enumerate()
.for_each(|(i, (output, input))| {
if let Value::String(input) = input {
if i % 2 == 0 {
table.add_row(row![input.as_str(), output.as_str()]);
table.add_row(row![input.as_str().normal(), output.as_str().normal()]);
} else {
table.add_row(row![input.as_str().purple(), output.as_str().purple()]);
table.add_row(row![Fm => input.as_str(), output.as_str()]);
}
}
});
Expand Down Expand Up @@ -147,12 +147,12 @@ fn run_app() -> Result<bool> {
opts.map.as_deref(),
)?,
read_output(output.as_deref())?,
opts.extension,
!opts.no_extension,
)?;
let (map, with_err) = rename_files(
input_iter,
opts.test,
opts.print || opts.generate.is_some(),
!opts.quiet || opts.generate.is_some(),
opts.overwrite,
opts.mkdir,
);
Expand All @@ -162,7 +162,7 @@ fn run_app() -> Result<bool> {
serde_json::to_vec_pretty(map.as_ref().unwrap())?.as_slice(),
)?;
}
if let Some(map) = map.filter(|map| opts.print && !map.is_empty()) {
if let Some(map) = map.filter(|map| !opts.quiet && !map.is_empty()) {
colored::control::set_override(std::io::stdout().is_terminal());
print_map_table(map);
}
Expand Down
15 changes: 14 additions & 1 deletion tests/default_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn test_default() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
r".*E(\d+).*",
Expand Down Expand Up @@ -69,7 +70,13 @@ fn test_default_not_overwrite() {

let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&["-d", dir.path().to_str().unwrap(), r".*E(\d+).*", "1.mkv"])
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
r".*E(\d+).*",
"1.mkv",
])
.unwrap();

let mut files: Vec<String> = read_dir(dir.path())
Expand Down Expand Up @@ -109,6 +116,7 @@ fn test_default_overwrite() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-w",
Expand Down Expand Up @@ -159,6 +167,7 @@ fn test_default_subdir() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-k",
Expand Down Expand Up @@ -218,6 +227,7 @@ fn test_default_subdir_depth() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"--depth",
"2",
"-d",
Expand Down Expand Up @@ -279,6 +289,7 @@ fn test_default_subdir_max_depth() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"--depth",
"3",
"--max-depth",
Expand Down Expand Up @@ -342,6 +353,7 @@ fn test_default_subdir_not_overwrite() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-k",
Expand Down Expand Up @@ -401,6 +413,7 @@ fn test_default_subdir_overwrite() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-k",
Expand Down
3 changes: 2 additions & 1 deletion tests/map_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ fn test_map() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-m",
Expand All @@ -74,7 +75,7 @@ fn test_map() {

let cmd_undo = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&["-d", dir.path().to_str().unwrap(), "-m", "undo.json"])
.args(&["-E", "-d", dir.path().to_str().unwrap(), "-m", "undo.json"])
.unwrap();

let mut files_undo: Vec<String> = read_dir(dir.path())
Expand Down
8 changes: 8 additions & 0 deletions tests/regex_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ fn test_regex() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-r",
Expand Down Expand Up @@ -71,6 +72,7 @@ fn test_regex_not_overwrite() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-r",
Expand Down Expand Up @@ -116,6 +118,7 @@ fn test_regex_overwrite() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-w",
Expand Down Expand Up @@ -167,6 +170,7 @@ fn test_regex_subdir() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-k",
Expand Down Expand Up @@ -227,6 +231,7 @@ fn test_regex_subdir_depth() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"--depth",
"2",
"-d",
Expand Down Expand Up @@ -289,6 +294,7 @@ fn test_regex_subdir_max_depth() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"--depth",
"3",
"--max-depth",
Expand Down Expand Up @@ -353,6 +359,7 @@ fn test_regex_subdir_not_overwrite() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-k",
Expand Down Expand Up @@ -413,6 +420,7 @@ fn test_regex_subdir_overwrite() {
let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-k",
Expand Down
9 changes: 8 additions & 1 deletion tests/sort_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,14 @@ fn test_sort() {

let cmd = Command::cargo_bin(env!("CARGO_PKG_NAME"))
.unwrap()
.args(&["-d", dir.path().to_str().unwrap(), "-s", "asc", "{:3}.mkv"])
.args(&[
"-E",
"-d",
dir.path().to_str().unwrap(),
"-s",
"asc",
"{:3}.mkv",
])
.unwrap();

let mut files: Vec<String> = read_dir(dir.path())
Expand Down

0 comments on commit 1426f43

Please sign in to comment.