Skip to content

Commit 1846b82

Browse files
authored
feat: Allow creation of directory (#14)
This PR make the creation of directory possible.
1 parent 1bf90a5 commit 1846b82

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/ssh.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,4 +192,13 @@ impl SSHConnection {
192192
Err("SFTP session not initialized.".to_string())
193193
}
194194
}
195+
196+
pub fn create_directory(&self, path: &str) -> Result<(), String> {
197+
if let Some(sftp) = &self.sftp {
198+
sftp.mkdir(Path::new(path), 0o755)
199+
.map_err(|e| format!("Failed to create directory: {}", e))
200+
} else {
201+
Err("SFTP subsystem not initialized.".to_string())
202+
}
203+
}
195204
}

src/ui.rs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ pub struct UIState {
2020
pub file_content: String,
2121
pub renaming_file: Option<String>,
2222
pub new_name: String,
23+
pub new_directory_name: String,
2324
}
2425

2526
impl Default for UIState {
@@ -39,6 +40,7 @@ impl Default for UIState {
3940
file_content: String::new(),
4041
renaming_file: None,
4142
new_name: String::new(),
43+
new_directory_name: String::new(),
4244
}
4345
}
4446
}
@@ -163,6 +165,37 @@ pub fn render_ui(ui: &mut egui::Ui, state: &mut UIState, connection: &mut Option
163165
}
164166
});
165167

168+
ui.horizontal(|ui| {
169+
ui.label("Create Directory:");
170+
ui.text_edit_singleline(&mut state.new_directory_name);
171+
if ui.button("Create").clicked() {
172+
if !state.new_directory_name.is_empty() {
173+
if let Some(conn) = connection {
174+
let full_path =
175+
format!("{}/{}", state.current_path, state.new_directory_name);
176+
match conn.create_directory(&full_path) {
177+
Ok(_) => {
178+
state.error_message =
179+
Some("Directory created successfully.".to_string());
180+
state.new_directory_name.clear(); // Clear the input field &&
181+
// Refresh directory listing
182+
match conn.list_directory(&state.current_path) {
183+
Ok(files) => state.files = files,
184+
Err(e) => state.error_message = Some(e),
185+
}
186+
}
187+
Err(e) => {
188+
state.error_message =
189+
Some(format!("Failed to create directory: {}", e));
190+
}
191+
}
192+
}
193+
} else {
194+
state.error_message = Some("Directory name cannot be empty.".to_string());
195+
}
196+
}
197+
});
198+
166199
ui.horizontal(|ui| {
167200
if ui.button("Up").clicked() {
168201
if let Some(pos) = state.current_path.rfind('/') {

0 commit comments

Comments
 (0)