From e177c48208cfb954eada1ffba96284f4f93d2dff Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Tue, 18 Feb 2025 11:42:03 +0000 Subject: [PATCH] refactor: use Option to indicate if a status message should not be changed --- helix-term/src/ui/mod.rs | 80 +++++++++++++++++++++++----------------- 1 file changed, 47 insertions(+), 33 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index abc766640..bd7a2e441 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -290,7 +290,7 @@ fn create_file_operation_prompt( prompt: &'static str, cx: &mut Context, path: &Path, - callback: fn(&mut Context, &Path, &str) -> Result, + callback: fn(&mut Context, &Path, &str) -> Option>, ) { cx.editor.file_explorer_selected_path = Some(path.to_path_buf()); let callback = Box::pin(async move { @@ -308,8 +308,9 @@ fn create_file_operation_prompt( if let Some(path) = path { match callback(cx, &path, input) { - Ok(msg) => cx.editor.set_status(msg), - Err(msg) => cx.editor.set_error(msg), + Some(Ok(msg)) => cx.editor.set_status(msg), + Some(Err(msg)) => cx.editor.set_error(msg), + None => (), }; } else { cx.editor @@ -384,21 +385,26 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result Result {}: {err}", if move_to_str.ends_with(std::path::MAIN_SEPARATOR) { @@ -422,8 +428,10 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result Result { create_file_operation_prompt("copy-to:", cx, path, |_, copy_from, copy_to_str| { let copy_to = helix_stdx::path::expand_tilde(PathBuf::from(copy_to_str)); if copy_from.is_dir() || copy_to_str.ends_with('/') { // TODO: support copying directories (recursively)?. This isn't built-in to the standard library - Err(format!( + Some(Err(format!( "Copying directories is not supported: {} is a directory", copy_from.display() - )) + ))) } else if copy_to.exists() { - // TODO: confirmation prompt when overwriting - Err(format!("Path {} exists", copy_to.display())) + // TODO: confirmation prompt + Some(Err(format!("Path {} exists", copy_to.display()))) } else { - std::fs::copy(copy_from, ©_to).map_err( + if let Err(err) = std::fs::copy(copy_from, ©_to).map_err( |err| format!("Unable to copy from file {} to {}: {err}", copy_from.display(), copy_to.display() - ))?; + )) { + return Some(Err(err)); + }; - Ok(format!( + Some(Ok(format!( "Copied contents of file {} to {}", copy_from.display(), copy_to.display() - )) + ))) } }) }, - // copy path + // copy path into register alt!('y') => { let register = cx.editor.selected_register.unwrap_or( cx.editor.config().default_yank_register