diff --git a/src/commands/decompress.rs b/src/commands/decompress.rs index 3b9f248..e3654e9 100644 --- a/src/commands/decompress.rs +++ b/src/commands/decompress.rs @@ -14,7 +14,11 @@ use crate::{ Extension, }, utils::{ - self, io::lock_and_flush_output_stdio, is_path_stdin, logger::{info, info_accessible}, nice_directory_display, user_wants_to_continue + self, + io::lock_and_flush_output_stdio, + is_path_stdin, + logger::{info, info_accessible}, + nice_directory_display, user_wants_to_continue, }, QuestionAction, QuestionPolicy, BUFFER_CAPACITY, }; diff --git a/src/utils/fs.rs b/src/utils/fs.rs index b92e909..9c95a79 100644 --- a/src/utils/fs.rs +++ b/src/utils/fs.rs @@ -19,19 +19,18 @@ pub fn is_path_stdin(path: &Path) -> bool { path.as_os_str() == "-" } - pub fn resolve_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result> { if path.exists() { match user_wants_to_overwrite(path, question_policy)? { - FileConflitOperation::Cancel => return Ok(None), + FileConflitOperation::Cancel => Ok(None), FileConflitOperation::Overwrite => { remove_file_or_dir(path)?; Ok(Some(path.to_path_buf())) - }, + } FileConflitOperation::Rename => { let renamed_path = rename_for_available_filename(path); Ok(Some(renamed_path)) - }, + } } } else { Ok(Some(path.to_path_buf())) @@ -64,10 +63,10 @@ pub fn rename_or_increment_filename(path: &Path) -> PathBuf { Some((base, number_str)) if number_str.chars().all(char::is_numeric) => { let number = number_str.parse::().unwrap_or(0); format!("{}_{}", base, number + 1) - }, + } _ => format!("{}_1", filename), }; - + let mut new_path = parent.join(new_filename); if !extension.is_empty() { new_path.set_extension(extension); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index a334bac..18f5b46 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -18,10 +18,13 @@ pub use self::{ EscapedPathDisplay, }, fs::{ - cd_into_same_dir_as, resolve_path, create_dir_if_non_existent, is_path_stdin, remove_file_or_dir, - try_infer_extension, rename_for_available_filename + cd_into_same_dir_as, create_dir_if_non_existent, is_path_stdin, remove_file_or_dir, + rename_for_available_filename, resolve_path, try_infer_extension, + }, + question::{ + ask_to_create_file, user_wants_to_continue, user_wants_to_overwrite, FileConflitOperation, QuestionAction, + QuestionPolicy, }, - question::{ask_to_create_file, user_wants_to_continue, user_wants_to_overwrite, FileConflitOperation, QuestionAction, QuestionPolicy}, utf8::{get_invalid_utf8_paths, is_invalid_utf8}, }; diff --git a/src/utils/question.rs b/src/utils/question.rs index 7510b49..2206f92 100644 --- a/src/utils/question.rs +++ b/src/utils/question.rs @@ -56,7 +56,6 @@ pub fn user_wants_to_overwrite(path: &Path, question_policy: QuestionPolicy) -> } } - /// Check if QuestionPolicy flags were set, otherwise, ask user if they want to overwrite. pub fn ask_file_conflict_operation(path: &Path) -> Result { use FileConflitOperation as Op; @@ -90,12 +89,12 @@ pub fn ask_to_create_file(path: &Path, question_policy: QuestionPolicy) -> Resul FileConflitOperation::Overwrite => { utils::remove_file_or_dir(path)?; Ok(Some(fs::File::create(path)?)) - }, + } FileConflitOperation::Cancel => Ok(None), FileConflitOperation::Rename => { let renamed_file_path = utils::rename_for_available_filename(path); Ok(Some(fs::File::create(renamed_file_path)?)) - }, + } } } Err(e) => Err(Error::from(e)), @@ -120,17 +119,13 @@ pub fn user_wants_to_continue( ChoicePrompt::new( format!("Do you want to {action} {path}?"), - [ - ("yes", true, *colors::GREEN), - ("no", false, *colors::RED), - ], + [("yes", true, *colors::GREEN), ("no", false, *colors::RED)], ) .ask() } } } - /// Choise dialog for end user with [option1/option2/...] question. /// /// If the placeholder is found in the prompt text, it will be replaced to form the final message. @@ -176,25 +171,33 @@ impl<'a, T: Default> ChoicePrompt<'a, T> { // Ask the same question to end while no valid answers are given loop { let choice_prompt = if is_running_in_accessible_mode() { - self - .choises + self.choises .iter() .map(|choise| format!("{}{}{}", choise.color, choise.label, *colors::RESET)) .collect::>() - .join("/") + .join("/") } else { let choises = self .choises .iter() - .map(|choise| format!("{}{}{}", choise.color, choise.label.chars().nth(0).expect("dev error"), *colors::RESET)) + .map(|choise| { + format!( + "{}{}{}", + choise.color, + choise + .label + .chars() + .nth(0) + .expect("dev error, should be reported, we checked this won't happen"), + *colors::RESET + ) + }) .collect::>() .join("/"); format!("[{}]", choises) }; - - // TODO: use accessible mode eprintln!("{} {}", message, choice_prompt); let mut answer = String::new(); @@ -213,12 +216,12 @@ impl<'a, T: Default> ChoicePrompt<'a, T> { answer.make_ascii_lowercase(); let answer = answer.trim(); - let choosed_index = self + let chosen_index = self .choises .iter() .position(|choise| answer == &choise.label[0..answer.len()]); - if let Some(i) = choosed_index { + if let Some(i) = chosen_index { return Ok(self.choises.remove(i).value); } }