mirror of
https://github.com/ouch-org/ouch.git
synced 2025-06-07 20:15:27 +00:00
chore: cargo fmt
This commit is contained in:
parent
782cd27580
commit
3626e7196f
@ -14,7 +14,11 @@ use crate::{
|
|||||||
Extension,
|
Extension,
|
||||||
},
|
},
|
||||||
utils::{
|
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,
|
QuestionAction, QuestionPolicy, BUFFER_CAPACITY,
|
||||||
};
|
};
|
||||||
|
@ -19,19 +19,18 @@ pub fn is_path_stdin(path: &Path) -> bool {
|
|||||||
path.as_os_str() == "-"
|
path.as_os_str() == "-"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub fn resolve_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result<Option<PathBuf>> {
|
pub fn resolve_path(path: &Path, question_policy: QuestionPolicy) -> crate::Result<Option<PathBuf>> {
|
||||||
if path.exists() {
|
if path.exists() {
|
||||||
match user_wants_to_overwrite(path, question_policy)? {
|
match user_wants_to_overwrite(path, question_policy)? {
|
||||||
FileConflitOperation::Cancel => return Ok(None),
|
FileConflitOperation::Cancel => Ok(None),
|
||||||
FileConflitOperation::Overwrite => {
|
FileConflitOperation::Overwrite => {
|
||||||
remove_file_or_dir(path)?;
|
remove_file_or_dir(path)?;
|
||||||
Ok(Some(path.to_path_buf()))
|
Ok(Some(path.to_path_buf()))
|
||||||
},
|
}
|
||||||
FileConflitOperation::Rename => {
|
FileConflitOperation::Rename => {
|
||||||
let renamed_path = rename_for_available_filename(path);
|
let renamed_path = rename_for_available_filename(path);
|
||||||
Ok(Some(renamed_path))
|
Ok(Some(renamed_path))
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok(Some(path.to_path_buf()))
|
Ok(Some(path.to_path_buf()))
|
||||||
@ -64,7 +63,7 @@ pub fn rename_or_increment_filename(path: &Path) -> PathBuf {
|
|||||||
Some((base, number_str)) if number_str.chars().all(char::is_numeric) => {
|
Some((base, number_str)) if number_str.chars().all(char::is_numeric) => {
|
||||||
let number = number_str.parse::<u32>().unwrap_or(0);
|
let number = number_str.parse::<u32>().unwrap_or(0);
|
||||||
format!("{}_{}", base, number + 1)
|
format!("{}_{}", base, number + 1)
|
||||||
},
|
}
|
||||||
_ => format!("{}_1", filename),
|
_ => format!("{}_1", filename),
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -18,10 +18,13 @@ pub use self::{
|
|||||||
EscapedPathDisplay,
|
EscapedPathDisplay,
|
||||||
},
|
},
|
||||||
fs::{
|
fs::{
|
||||||
cd_into_same_dir_as, resolve_path, create_dir_if_non_existent, is_path_stdin, remove_file_or_dir,
|
cd_into_same_dir_as, create_dir_if_non_existent, is_path_stdin, remove_file_or_dir,
|
||||||
try_infer_extension, rename_for_available_filename
|
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},
|
utf8::{get_invalid_utf8_paths, is_invalid_utf8},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -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.
|
/// Check if QuestionPolicy flags were set, otherwise, ask user if they want to overwrite.
|
||||||
pub fn ask_file_conflict_operation(path: &Path) -> Result<FileConflitOperation> {
|
pub fn ask_file_conflict_operation(path: &Path) -> Result<FileConflitOperation> {
|
||||||
use FileConflitOperation as Op;
|
use FileConflitOperation as Op;
|
||||||
@ -90,12 +89,12 @@ pub fn ask_to_create_file(path: &Path, question_policy: QuestionPolicy) -> Resul
|
|||||||
FileConflitOperation::Overwrite => {
|
FileConflitOperation::Overwrite => {
|
||||||
utils::remove_file_or_dir(path)?;
|
utils::remove_file_or_dir(path)?;
|
||||||
Ok(Some(fs::File::create(path)?))
|
Ok(Some(fs::File::create(path)?))
|
||||||
},
|
}
|
||||||
FileConflitOperation::Cancel => Ok(None),
|
FileConflitOperation::Cancel => Ok(None),
|
||||||
FileConflitOperation::Rename => {
|
FileConflitOperation::Rename => {
|
||||||
let renamed_file_path = utils::rename_for_available_filename(path);
|
let renamed_file_path = utils::rename_for_available_filename(path);
|
||||||
Ok(Some(fs::File::create(renamed_file_path)?))
|
Ok(Some(fs::File::create(renamed_file_path)?))
|
||||||
},
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(e) => Err(Error::from(e)),
|
Err(e) => Err(Error::from(e)),
|
||||||
@ -120,17 +119,13 @@ pub fn user_wants_to_continue(
|
|||||||
|
|
||||||
ChoicePrompt::new(
|
ChoicePrompt::new(
|
||||||
format!("Do you want to {action} {path}?"),
|
format!("Do you want to {action} {path}?"),
|
||||||
[
|
[("yes", true, *colors::GREEN), ("no", false, *colors::RED)],
|
||||||
("yes", true, *colors::GREEN),
|
|
||||||
("no", false, *colors::RED),
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
.ask()
|
.ask()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Choise dialog for end user with [option1/option2/...] question.
|
/// 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.
|
/// If the placeholder is found in the prompt text, it will be replaced to form the final message.
|
||||||
@ -176,8 +171,7 @@ impl<'a, T: Default> ChoicePrompt<'a, T> {
|
|||||||
// Ask the same question to end while no valid answers are given
|
// Ask the same question to end while no valid answers are given
|
||||||
loop {
|
loop {
|
||||||
let choice_prompt = if is_running_in_accessible_mode() {
|
let choice_prompt = if is_running_in_accessible_mode() {
|
||||||
self
|
self.choises
|
||||||
.choises
|
|
||||||
.iter()
|
.iter()
|
||||||
.map(|choise| format!("{}{}{}", choise.color, choise.label, *colors::RESET))
|
.map(|choise| format!("{}{}{}", choise.color, choise.label, *colors::RESET))
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
@ -186,15 +180,24 @@ impl<'a, T: Default> ChoicePrompt<'a, T> {
|
|||||||
let choises = self
|
let choises = self
|
||||||
.choises
|
.choises
|
||||||
.iter()
|
.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::<Vec<_>>()
|
.collect::<Vec<_>>()
|
||||||
.join("/");
|
.join("/");
|
||||||
|
|
||||||
format!("[{}]", choises)
|
format!("[{}]", choises)
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
// TODO: use accessible mode
|
|
||||||
eprintln!("{} {}", message, choice_prompt);
|
eprintln!("{} {}", message, choice_prompt);
|
||||||
|
|
||||||
let mut answer = String::new();
|
let mut answer = String::new();
|
||||||
@ -213,12 +216,12 @@ impl<'a, T: Default> ChoicePrompt<'a, T> {
|
|||||||
answer.make_ascii_lowercase();
|
answer.make_ascii_lowercase();
|
||||||
let answer = answer.trim();
|
let answer = answer.trim();
|
||||||
|
|
||||||
let choosed_index = self
|
let chosen_index = self
|
||||||
.choises
|
.choises
|
||||||
.iter()
|
.iter()
|
||||||
.position(|choise| answer == &choise.label[0..answer.len()]);
|
.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);
|
return Ok(self.choises.remove(i).value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user