Skip to content

Commit

Permalink
feat: change option name --end_mistake to --oops
Browse files Browse the repository at this point in the history
  • Loading branch information
urin committed Feb 9, 2023
1 parent 2c027cc commit 12b5517
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 24 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Options:
-w, --with-hidden Include hidden files
-e, --exclude-pattern <PATTERN> Exclude regular expression pattern
-c, --copy Copy without moving
-o, --oops Abort in case of collision
-h, --help Print help
-V, --version Print version
```
Expand Down Expand Up @@ -102,7 +103,8 @@ cargo make

## TODOs ✅

- Order option
- Ignore case in case of Windows
- Natural sort option
- Recursive option
- Maximum depth option
- Depth option
Expand Down
49 changes: 26 additions & 23 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ pub struct CommandLine {
#[arg(short, long)]
pub copy: bool,
/// Abort in case of collision
#[arg(short = 'n', long)]
pub end_mistake: bool,
#[arg(short, long)]
pub oops: bool,
}

#[derive(Debug)]
Expand Down Expand Up @@ -88,7 +88,7 @@ pub fn try_main(args: &CommandLine) -> Result<usize> {

pub fn sources_from(args: &CommandLine) -> Result<Vec<Source>> {
let mut sources: Vec<Source> = Vec::new();
let paths = arg_paths(&args.paths)?;
let paths = list_files(&args.paths)?;
for p in paths.iter().map(|p| p.trim_end_matches(SEPARATORS)) {
let path = &PathBuf::from(if cfg!(target_family = "windows") {
p.replace('/', "\\")
Expand Down Expand Up @@ -124,10 +124,13 @@ pub fn sources_from(args: &CommandLine) -> Result<Vec<Source>> {
}
}
}
if args.sort {
sources.sort_unstable_by(|a, b| natord::compare(&a.text, &b.text));
}
Ok(sources)
}

pub fn arg_paths(args: &[String]) -> Result<Vec<String>> {
pub fn list_files(args: &[String]) -> Result<Vec<String>> {
use glob::glob;
let mut paths = Vec::new();
for arg in args.iter() {
Expand Down Expand Up @@ -203,6 +206,23 @@ pub fn put_source(sources: &mut Vec<Source>, path: &Path, args: &CommandLine) ->
Ok(())
}

pub fn normalize(path: &Path) -> PathBuf {
let mut normalized = PathBuf::new();
for component in path.components() {
match component {
Component::ParentDir => {
if !normalized.pop() {
normalized.push(component);
}
}
_ => {
normalized.push(component);
}
}
}
normalized
}

/// TODO Can be replaced with `std::path::absolute` in the future.
#[cfg(target_family = "windows")]
pub fn absolute(path: &Path) -> Result<normpath::BasePathBuf> {
Expand Down Expand Up @@ -290,7 +310,7 @@ pub fn operations_from(sources: &Vec<Source>, args: &CommandLine) -> Result<Vec<
lines.len().to_string().yellow(),
sources.len().to_string().yellow()
);
if !args.end_mistake {
if !args.oops {
println!("{}", message.to_string().yellow());
if prompt_redo()? {
continue 'redo;
Expand All @@ -313,7 +333,7 @@ pub fn operations_from(sources: &Vec<Source>, args: &CommandLine) -> Result<Vec<
},
};
if let Err(message) = is_operational(&operations, &new_operation) {
if !args.end_mistake {
if !args.oops {
println!("{}", message.to_string().yellow());
if prompt_redo()? {
continue 'redo;
Expand Down Expand Up @@ -351,23 +371,6 @@ pub fn prompt_redo() -> Result<bool> {
}
}

pub fn normalize(path: &Path) -> PathBuf {
let mut normalized = PathBuf::new();
for component in path.components() {
match component {
Component::ParentDir => {
if !normalized.pop() {
normalized.push(component);
}
}
_ => {
normalized.push(component);
}
}
}
normalized
}

pub fn is_operational(operations: &[Operation], new_operation: &Operation) -> Result<()> {
let src = &new_operation.src;
let dst = &new_operation.dst;
Expand Down

0 comments on commit 12b5517

Please sign in to comment.