mirror of https://github.com/helix-editor/helix
refactor: use Option<Result> to indicate if a status message should not be changed
parent
a099ae1dbe
commit
e177c48208
|
@ -290,7 +290,7 @@ fn create_file_operation_prompt(
|
||||||
prompt: &'static str,
|
prompt: &'static str,
|
||||||
cx: &mut Context,
|
cx: &mut Context,
|
||||||
path: &Path,
|
path: &Path,
|
||||||
callback: fn(&mut Context, &Path, &str) -> Result<String, String>,
|
callback: fn(&mut Context, &Path, &str) -> Option<Result<String, String>>,
|
||||||
) {
|
) {
|
||||||
cx.editor.file_explorer_selected_path = Some(path.to_path_buf());
|
cx.editor.file_explorer_selected_path = Some(path.to_path_buf());
|
||||||
let callback = Box::pin(async move {
|
let callback = Box::pin(async move {
|
||||||
|
@ -308,8 +308,9 @@ fn create_file_operation_prompt(
|
||||||
|
|
||||||
if let Some(path) = path {
|
if let Some(path) = path {
|
||||||
match callback(cx, &path, input) {
|
match callback(cx, &path, input) {
|
||||||
Ok(msg) => cx.editor.set_status(msg),
|
Some(Ok(msg)) => cx.editor.set_status(msg),
|
||||||
Err(msg) => cx.editor.set_error(msg),
|
Some(Err(msg)) => cx.editor.set_error(msg),
|
||||||
|
None => (),
|
||||||
};
|
};
|
||||||
} else {
|
} else {
|
||||||
cx.editor
|
cx.editor
|
||||||
|
@ -384,21 +385,26 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
||||||
let to_create = helix_stdx::path::expand_tilde(PathBuf::from(to_create_str));
|
let to_create = helix_stdx::path::expand_tilde(PathBuf::from(to_create_str));
|
||||||
|
|
||||||
if to_create.exists() {
|
if to_create.exists() {
|
||||||
return Err(format!("Path {} already exists", to_create.display()))
|
// TODO: confirmation prompt
|
||||||
|
return Some(Err(format!("Path {} already exists", to_create.display())))
|
||||||
};
|
};
|
||||||
|
|
||||||
if to_create_str.ends_with(std::path::MAIN_SEPARATOR) {
|
if to_create_str.ends_with(std::path::MAIN_SEPARATOR) {
|
||||||
fs::create_dir_all(&to_create).map_err(
|
if let Err(err) = fs::create_dir_all(&to_create).map_err(
|
||||||
|err| format!("Unable to create directory {}: {err}", to_create.display())
|
|err| format!("Unable to create directory {}: {err}", to_create.display())
|
||||||
)?;
|
) {
|
||||||
|
return Some(Err(err));
|
||||||
|
}
|
||||||
|
|
||||||
Ok(format!("Created directory: {}", to_create.display()))
|
Some(Ok(format!("Created directory: {}", to_create.display())))
|
||||||
} else {
|
} else {
|
||||||
fs::File::create(&to_create).map_err(
|
if let Err(err) = fs::File::create(&to_create).map_err(
|
||||||
|err| format!("Unable to create file {}: {err}", to_create.display())
|
|err| format!("Unable to create file {}: {err}", to_create.display())
|
||||||
)?;
|
) {
|
||||||
|
return Some(Err(err));
|
||||||
|
};
|
||||||
|
|
||||||
Ok(format!("Created file: {}", to_create.display()))
|
Some(Ok(format!("Created file: {}", to_create.display())))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -408,10 +414,10 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
||||||
let move_to = helix_stdx::path::expand_tilde(PathBuf::from(move_to_str));
|
let move_to = helix_stdx::path::expand_tilde(PathBuf::from(move_to_str));
|
||||||
|
|
||||||
if move_to.exists() {
|
if move_to.exists() {
|
||||||
// TODO: overwrite prompt
|
// TODO: confirmation prompt
|
||||||
Err(format!("Path {} already exists", move_to.display()))
|
Some(Err(format!("Path {} already exists", move_to.display())))
|
||||||
} else {
|
} else {
|
||||||
fs::rename(move_from, &move_to).map_err(|err|
|
if let Err(err) = fs::rename(move_from, &move_to).map_err(|err|
|
||||||
format!(
|
format!(
|
||||||
"Unable to move {} {} -> {}: {err}",
|
"Unable to move {} {} -> {}: {err}",
|
||||||
if move_to_str.ends_with(std::path::MAIN_SEPARATOR) {
|
if move_to_str.ends_with(std::path::MAIN_SEPARATOR) {
|
||||||
|
@ -422,8 +428,10 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
||||||
move_from.display(),
|
move_from.display(),
|
||||||
move_to.display()
|
move_to.display()
|
||||||
)
|
)
|
||||||
)?;
|
) {
|
||||||
Ok("".into())
|
return Some(Err(err))
|
||||||
|
};
|
||||||
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
@ -433,56 +441,62 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result<FileExplorer, std
|
||||||
let to_delete = helix_stdx::path::expand_tilde(PathBuf::from(to_delete_str));
|
let to_delete = helix_stdx::path::expand_tilde(PathBuf::from(to_delete_str));
|
||||||
if to_delete_str == "y" {
|
if to_delete_str == "y" {
|
||||||
if !to_delete.exists() {
|
if !to_delete.exists() {
|
||||||
return Err(format!("Path {} does not exist", to_delete.display()))
|
return Some(Err(format!("Path {} does not exist", to_delete.display())))
|
||||||
};
|
};
|
||||||
|
|
||||||
if to_delete_str.ends_with(std::path::MAIN_SEPARATOR) {
|
if to_delete_str.ends_with(std::path::MAIN_SEPARATOR) {
|
||||||
fs::remove_dir_all(&to_delete).map_err(
|
if let Err(err) = fs::remove_dir_all(&to_delete).map_err(
|
||||||
|err| format!(
|
|err| format!(
|
||||||
"Unable to delete directory {}: {err}", to_delete.display()
|
"Unable to delete directory {}: {err}", to_delete.display()
|
||||||
)
|
)
|
||||||
)?;
|
) {
|
||||||
|
return Some(Err(err));
|
||||||
|
};
|
||||||
|
|
||||||
Ok(format!("Deleted directory: {}", to_delete.display()))
|
Some(Ok(format!("Deleted directory: {}", to_delete.display())))
|
||||||
} else {
|
} else {
|
||||||
fs::remove_file(&to_delete).map_err(
|
if let Err(err) = fs::remove_file(&to_delete).map_err(
|
||||||
|err| format!(
|
|err| format!(
|
||||||
"Unable to delete file {}: {err}", to_delete.display()
|
"Unable to delete file {}: {err}", to_delete.display()
|
||||||
)
|
)
|
||||||
)?;
|
) {
|
||||||
|
return Some(Err(err));
|
||||||
|
};
|
||||||
|
|
||||||
Ok(format!("Deleted file: {}", to_delete.display()))
|
Some(Ok(format!("Deleted file: {}", to_delete.display())))
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Ok("".into())
|
None
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
// copy contents
|
// copy file / directory
|
||||||
alt!('c') => {
|
alt!('c') => {
|
||||||
create_file_operation_prompt("copy-to:", cx, path, |_, copy_from, copy_to_str| {
|
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));
|
let copy_to = helix_stdx::path::expand_tilde(PathBuf::from(copy_to_str));
|
||||||
if copy_from.is_dir() || copy_to_str.ends_with('/') {
|
if copy_from.is_dir() || copy_to_str.ends_with('/') {
|
||||||
// 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
|
||||||
Err(format!(
|
Some(Err(format!(
|
||||||
"Copying directories is not supported: {} is a directory", copy_from.display()
|
"Copying directories is not supported: {} is a directory", copy_from.display()
|
||||||
))
|
)))
|
||||||
} else if copy_to.exists() {
|
} else if copy_to.exists() {
|
||||||
// TODO: confirmation prompt when overwriting
|
// TODO: confirmation prompt
|
||||||
Err(format!("Path {} exists", copy_to.display()))
|
Some(Err(format!("Path {} exists", copy_to.display())))
|
||||||
} else {
|
} 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}",
|
|err| format!("Unable to copy from file {} to {}: {err}",
|
||||||
copy_from.display(), copy_to.display()
|
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()
|
"Copied contents of file {} to {}", copy_from.display(), copy_to.display()
|
||||||
))
|
)))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
// copy path
|
// copy path into register
|
||||||
alt!('y') => {
|
alt!('y') => {
|
||||||
let register = cx.editor.selected_register.unwrap_or(
|
let register = cx.editor.selected_register.unwrap_or(
|
||||||
cx.editor.config().default_yank_register
|
cx.editor.config().default_yank_register
|
||||||
|
|
Loading…
Reference in New Issue