refactor: inversion of control

pull/12902/head
Nikita Revenco 2025-02-22 13:31:35 +00:00
parent 59a1d244aa
commit 674afbfd89
1 changed files with 44 additions and 42 deletions

View File

@ -456,17 +456,17 @@ pub fn file_explorer(
} }
refresh_file_explorer(cursor, cx, root); refresh_file_explorer(cursor, cx, root);
Some(Ok(format!("Created directory: {}", to_create.display()))) return Some(Ok(format!("Created directory: {}", to_create.display())));
} else {
if let Err(err) = fs::File::create(to_create).map_err(|err| {
format!("Unable to create file {}: {err}", to_create.display())
}) {
return Some(Err(err));
};
refresh_file_explorer(cursor, cx, root);
Some(Ok(format!("Created file: {}", to_create.display())))
} }
if let Err(err) = fs::File::create(to_create).map_err(|err| {
format!("Unable to create file {}: {err}", to_create.display())
}) {
return Some(Err(err));
};
refresh_file_explorer(cursor, cx, root);
Some(Ok(format!("Created file: {}", to_create.display())))
}; };
if to_create.exists() { if to_create.exists() {
@ -555,33 +555,33 @@ pub fn file_explorer(
data, data,
|_| "".to_string(), |_| "".to_string(),
|root, cursor, cx, to_delete, confirmation| { |root, cursor, cx, to_delete, confirmation| {
if confirmation == "y" { if confirmation != "y" {
if !to_delete.exists() { return None;
return Some(Err(format!("Path {} does not exist", to_delete.display())));
};
if to_delete.is_dir() {
if let Err(err) = fs::remove_dir_all(to_delete).map_err(|err| {
format!("Unable to delete directory {}: {err}", to_delete.display())
}) {
return Some(Err(err));
};
refresh_file_explorer(cursor, cx, root);
Some(Ok(format!("Deleted directory: {}", to_delete.display())))
} else {
if let Err(err) = fs::remove_file(to_delete).map_err(|err| {
format!("Unable to delete file {}: {err}", to_delete.display())
}) {
return Some(Err(err));
};
refresh_file_explorer(cursor, cx, root);
Some(Ok(format!("Deleted file: {}", to_delete.display())))
}
} else {
None
} }
if !to_delete.exists() {
return Some(Err(format!("Path {} does not exist", to_delete.display())));
};
if to_delete.is_dir() {
if let Err(err) = fs::remove_dir_all(to_delete).map_err(|err| {
format!("Unable to delete directory {}: {err}", to_delete.display())
}) {
return Some(Err(err));
};
refresh_file_explorer(cursor, cx, root);
return Some(Ok(format!("Deleted directory: {}", to_delete.display())));
}
if let Err(err) = fs::remove_file(to_delete)
.map_err(|err| format!("Unable to delete file {}: {err}", to_delete.display()))
{
return Some(Err(err));
};
refresh_file_explorer(cursor, cx, root);
Some(Ok(format!("Deleted file: {}", to_delete.display())))
}, },
) )
}); });
@ -627,11 +627,13 @@ pub fn file_explorer(
if copy_from.is_dir() || copy_to_str.ends_with(std::path::MAIN_SEPARATOR) { if copy_from.is_dir() || copy_to_str.ends_with(std::path::MAIN_SEPARATOR) {
// TODO: support copying directories (recursively)?. This isn't built-in to the standard library // TODO: support copying directories (recursively)?. This isn't built-in to the standard library
Some(Err(format!( return Some(Err(format!(
"Copying directories is not supported: {} is a directory", "Copying directories is not supported: {} is a directory",
copy_from.display() copy_from.display()
))) )));
} else if copy_to.exists() { }
if copy_to.exists() {
create_confirmation_prompt( create_confirmation_prompt(
cursor, cursor,
format!( format!(
@ -644,10 +646,10 @@ pub fn file_explorer(
root, root,
do_copy, do_copy,
); );
None return None;
} else {
do_copy(cursor, cx, root, copy_to_str, copy_from)
} }
do_copy(cursor, cx, root, copy_to_str, copy_from)
}, },
) )
}); });