From f59c5966f6c44824c4cf7c26be7fe4f0d550fd0c Mon Sep 17 00:00:00 2001 From: Nikita Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Mon, 17 Feb 2025 23:37:58 +0000 Subject: [PATCH] feat: implement creating new files and directory --- helix-term/src/ui/mod.rs | 50 +++++++++++++++++++++++++++++++++------- 1 file changed, 42 insertions(+), 8 deletions(-) diff --git a/helix-term/src/ui/mod.rs b/helix-term/src/ui/mod.rs index b91cfe1cb..c01400638 100644 --- a/helix-term/src/ui/mod.rs +++ b/helix-term/src/ui/mod.rs @@ -34,7 +34,8 @@ use helix_view::Editor; use tui::text::Span; use std::collections::HashMap; -use std::path::Path; +use std::fs; +use std::path::{Path, MAIN_SEPARATOR}; use std::{error::Error, path::PathBuf}; use self::picker::PickerKeyHandler; @@ -289,7 +290,7 @@ fn create_file_operation_prompt( prompt: &'static str, cx: &mut Context, path: &Path, - callback: fn(&Path, &str), + callback: fn(&Path, &str) -> Result, ) { cx.editor.path_editing = Some(path.to_path_buf()); let callback = Box::pin(async move { @@ -299,12 +300,20 @@ fn create_file_operation_prompt( prompt.into(), None, crate::ui::completers::none, - move |_cx, input: &str, event: PromptEvent| { + move |cx, input: &str, event: PromptEvent| { if event != PromptEvent::Validate { return; }; - callback(path, input); + if let Some(path) = &cx.editor.path_editing { + match callback(path, input) { + Ok(msg) => cx.editor.set_status(msg), + Err(msg) => cx.editor.set_error(msg), + }; + } else { + cx.editor + .set_error("Unable to determine path of selected file") + } }, ); @@ -370,19 +379,44 @@ pub fn file_explorer(root: PathBuf, editor: &Editor) -> Result { - create_file_operation_prompt("create:", cx, path, |path, input| ()) + create_file_operation_prompt("create:", cx, path, |_path, create| { + let path = helix_stdx::path::expand_tilde(PathBuf::from(create)); + + if path.exists() { + return Err(format!("Path {create} already exists.")) + }; + + if create.ends_with(std::path::MAIN_SEPARATOR) { + fs::create_dir_all(path).map_err(|err| format!("Unable to create directory {create}: {err}"))?; + + Ok(format!("Created directory: {create}")) + } else { + fs::File::create(path).map_err(|err| format!("Unable to create file {create}: {err}"))?; + + Ok(format!("Created file: {create}")) + } + }) }, // move alt!('m') => { - create_file_operation_prompt("move:", cx, path, |path, input| ()) + create_file_operation_prompt("move:", cx, path, |path, input| { + Ok("".into()) + + }) }, // delete alt!('d') => { - create_file_operation_prompt("delete? (y/n):", cx, path, |path, input| ()) + create_file_operation_prompt("delete? (y/n):", cx, path, |path, input| { + Ok("".into()) + + }) }, // copy alt!('y') => { - create_file_operation_prompt("copy-to:", cx, path, |path, input| ()) + create_file_operation_prompt("copy-to:", cx, path, |path, input| { + Ok("".into()) + + }) }, });