Skip to content

Commit

Permalink
refactor: inversion of control
Browse files Browse the repository at this point in the history
  • Loading branch information
nik-rev committed Feb 22, 2025
1 parent 59a1d24 commit 674afbf
Showing 1 changed file with 41 additions and 39 deletions.
80 changes: 41 additions & 39 deletions helix-term/src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,17 +456,17 @@ pub fn file_explorer(
}
refresh_file_explorer(cursor, cx, root);

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())))
return Some(Ok(format!("Created directory: {}", 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() {
Expand Down Expand Up @@ -555,33 +555,33 @@ pub fn file_explorer(
data,
|_| "".to_string(),
|root, cursor, cx, to_delete, confirmation| {
if confirmation == "y" {
if !to_delete.exists() {
return Some(Err(format!("Path {} does not exist", to_delete.display())));
};
if confirmation != "y" {
return None;
}

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);
if !to_delete.exists() {
return Some(Err(format!("Path {} does not exist", to_delete.display())));
};

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);
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 file: {}", to_delete.display())))
}
} else {
None
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())))
},
)
});
Expand Down Expand Up @@ -627,11 +627,13 @@ pub fn file_explorer(

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
Some(Err(format!(
return Some(Err(format!(
"Copying directories is not supported: {} is a directory",
copy_from.display()
)))
} else if copy_to.exists() {
)));
}

if copy_to.exists() {
create_confirmation_prompt(
cursor,
format!(
Expand All @@ -644,10 +646,10 @@ pub fn file_explorer(
root,
do_copy,
);
None
} else {
do_copy(cursor, cx, root, copy_to_str, copy_from)
return None;
}

do_copy(cursor, cx, root, copy_to_str, copy_from)
},
)
});
Expand Down

0 comments on commit 674afbf

Please sign in to comment.