Skip to content

Commit

Permalink
switch-case (downcase only currently)
Browse files Browse the repository at this point in the history
  • Loading branch information
janpipek committed Aug 14, 2024
1 parent da1245c commit 97854db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use colored::Colorize;
use regex::Regex;
use std::fmt::{Display, Formatter, Result};
use std::fs::rename;
use std::io::ErrorKind;
use std::path::{Path, PathBuf};
use std::process::{self, exit};
use std::fmt::{Display, Formatter, Result};

extern crate unidecode;
use unidecode::unidecode;
Expand All @@ -30,11 +30,7 @@ impl Display for RenameIntent {
self.new_name.to_string_lossy().green()
)
} else {
write!(
f,
"{0} =",
self.path.to_string_lossy(),
)
write!(f, "{0} =", self.path.to_string_lossy(),)
}
}
}
Expand All @@ -46,6 +42,7 @@ pub enum RenameCommand {
FixExtension,
Normalize,
Replace(String, String, bool),
ChangeCase,
}

pub struct Config {
Expand Down Expand Up @@ -136,6 +133,14 @@ fn suggest_renames(files: &[PathBuf], command: &RenameCommand) -> Vec<RenameInte
new_name: PathBuf::from(new_name),
}
}
RenameCommand::ChangeCase => {
let path_str = path.to_string_lossy().to_string();
let new_name = path_str.to_lowercase();
RenameIntent {
path: path.clone(),
new_name: PathBuf::from(new_name),
}
}
})
.collect()
}
Expand Down Expand Up @@ -240,7 +245,13 @@ fn try_rename(path: &Path, new_name: &Path) -> bool {
}
}

fn process_command(command: &RenameCommand, files: &[PathBuf], dry: bool, auto_confirm: bool, show_unchanged: bool) {
fn process_command(
command: &RenameCommand,
files: &[PathBuf],
dry: bool,
auto_confirm: bool,
show_unchanged: bool,
) {
let intents = suggest_renames(files, command);
if dry {
print_intents(&intents, show_unchanged);
Expand Down Expand Up @@ -272,6 +283,6 @@ pub fn run(config: &Config) {
&config.files,
config.dry,
config.auto_confirm,
config.show_unchanged
config.show_unchanged,
);
}
6 changes: 6 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ fn extract_command(args_matches: &ArgMatches) -> Option<RenameCommand> {
matches.get_one::<String>("replacement").unwrap().clone(),
matches.get_flag("regex"),
)),
Some(("change-case", _)) => Some(RenameCommand::ChangeCase),
_ => None,
}
}
Expand Down Expand Up @@ -142,6 +143,11 @@ fn create_cli_command() -> Command {
)
.arg(path_arg.clone()),
)
.subcommand(
Command::new("change-case")
.about("Change case of all files.")
.arg(path_arg.clone()),
)
}

fn main() {
Expand Down

0 comments on commit 97854db

Please sign in to comment.