diff --git a/src/ssh.rs b/src/ssh.rs index bfd0e00..f19be6a 100644 --- a/src/ssh.rs +++ b/src/ssh.rs @@ -201,4 +201,17 @@ impl SSHConnection { Err("SFTP subsystem not initialized.".to_string()) } } + + pub fn create_file(&self, path: &str) -> Result<(), String> { + if let Some(sftp) = &self.sftp { + let mut file = sftp + .create(Path::new(path)) + .map_err(|e| format!("Failed to create file: {}", e))?; + file.write_all(b"") + .map_err(|e| format!("Failed to initialize file: {}", e))?; + Ok(()) + } else { + Err("SFTP subsystem not initialized.".to_string()) + } + } } diff --git a/src/ui.rs b/src/ui.rs index 8c8e8ab..8be19bf 100644 --- a/src/ui.rs +++ b/src/ui.rs @@ -21,6 +21,7 @@ pub struct UIState { pub renaming_file: Option, pub new_name: String, pub new_directory_name: String, + pub new_file_name: String, } impl Default for UIState { @@ -41,6 +42,7 @@ impl Default for UIState { renaming_file: None, new_name: String::new(), new_directory_name: String::new(), + new_file_name: String::new(), } } } @@ -196,6 +198,35 @@ pub fn render_ui(ui: &mut egui::Ui, state: &mut UIState, connection: &mut Option } }); + ui.horizontal(|ui| { + ui.label("Create File:"); + ui.text_edit_singleline(&mut state.new_file_name); + if ui.button("Create").clicked() { + if !state.new_file_name.is_empty() { + if let Some(conn) = connection { + let full_path = format!("{}/{}", state.current_path, state.new_file_name); + match conn.create_file(&full_path) { + Ok(_) => { + state.error_message = + Some("File created successfully.".to_string()); + state.new_file_name.clear(); + + match conn.list_directory(&state.current_path) { + Ok(files) => state.files = files, + Err(e) => state.error_message = Some(e), + } + } + Err(e) => { + state.error_message = Some(format!("Failed to create file: {}", e)); + } + } + } + } else { + state.error_message = Some("File name cannot be empty.".to_string()); + } + } + }); + ui.horizontal(|ui| { if ui.button("Up").clicked() { if let Some(pos) = state.current_path.rfind('/') {