Skip to content

Commit e134e7d

Browse files
committed
(delegation) added cmdline integration
1 parent 279b92e commit e134e7d

File tree

10 files changed

+424
-60
lines changed

10 files changed

+424
-60
lines changed

directory_2/dir2_history.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#V2
2+
/e
3+
cls
4+
/e

directory_2/src/commands.rs

Lines changed: 176 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,8 @@ pub fn execute_command(
1515
favorites_manager: &mut FavoritesManager,
1616
) -> Result<String, String> {
1717
match command {
18+
// Meta/System Commands
1819
Command::ListCommands => execute_list_all_cmd(),
19-
Command::DodgeDirectory => execute_dodge_directory(file_system_state),
20-
Command::WatchDirectory {
21-
directory: _directory,
22-
} => execute_watch_directory(file_system_state, &_directory),
23-
Command::ListDirectory => execute_list_directory(file_system_state),
24-
Command::ChangeDrive { drive: _drive } => execute_change_drive(file_system_state, _drive),
2520
Command::ClearScreen => {
2621
// Windows-specific
2722
std::process::Command::new("cmd")
@@ -31,22 +26,50 @@ pub fn execute_command(
3126
return Ok(String::from("Executed: Clear Screen"));
3227
}
3328
Command::Exit => Ok("exited!".to_string()),
34-
Command::Select {
35-
filename: _filename,
29+
Command::Unknown { command } => {
30+
return Ok(String::from(format!(
31+
"Unexecuted: Unknown command {}",
32+
command
33+
)));
34+
}
35+
36+
// Directory Navigation Commands
37+
Command::DodgeDirectory => execute_dodge_directory(file_system_state),
38+
Command::WatchDirectory {
3639
directory: _directory,
37-
} => {
38-
// return Ok(String::from("Executed: Select"))
39-
execute_select(file_system_state, _filename, _directory)
40+
} => execute_watch_directory(file_system_state, &_directory),
41+
Command::ListDirectory => execute_list_directory(file_system_state),
42+
Command::ChangeDrive { drive: _drive } => execute_change_drive(file_system_state, _drive),
43+
Command::MakeDirectory { directory } => {
44+
execute_make_directory(file_system_state, &directory)
4045
}
41-
Command::ViewState => {
42-
execute_view_state(file_system_state)
43-
// return Ok(String::from("Executed: View State"))
46+
Command::RemoveDirectory { directory } => {
47+
execute_remove_directory(file_system_state, &directory)
4448
}
49+
Command::RenameDirectory {
50+
old_directory,
51+
new_directory,
52+
} => execute_rename_directory(file_system_state, &old_directory, &new_directory),
53+
54+
// File Management Commands
55+
Command::MakeFile { filename } => execute_make_file(file_system_state, &filename),
56+
Command::RemoveFile { filename } => execute_remove_file(file_system_state, &filename),
57+
Command::RenameFile {
58+
old_filename,
59+
new_filename,
60+
} => execute_rename_file(file_system_state, &old_filename, &new_filename),
61+
62+
// State Management Commands
63+
Command::Select {
64+
filename: _filename,
65+
directory: _directory,
66+
} => execute_select(file_system_state, _filename, _directory),
67+
Command::ViewState => execute_view_state(file_system_state),
4568
Command::ClearState => execute_clear_state(file_system_state),
46-
Command::MetaState => {
47-
execute_meta_state(file_system_state)
48-
// return Ok(String::from("Executed: Meta State"))
49-
}
69+
Command::MetaState => execute_meta_state(file_system_state),
70+
Command::RunState => execute_run_state(file_system_state),
71+
72+
// Search Commands
5073
Command::FindExact {
5174
filename: _filename,
5275
} => {
@@ -55,38 +78,19 @@ pub fn execute_command(
5578
_filename.to_string().yellow()
5679
);
5780
execute_find_exact(&_filename)
58-
// return Ok(String::from("Executed: Find Exact"))
5981
}
6082
Command::Search {
6183
engine,
6284
filename: _filename,
6385
} => execute_search(&engine, &_filename),
64-
Command::RunState => {
65-
execute_run_state(file_system_state)
66-
// return Ok(String::from("Executed: Run State"))
67-
}
68-
Command::FavView => {
69-
execute_fav_view(favorites_manager)
70-
// return Ok(String::from("Executed: Fav View"))
71-
}
86+
87+
// Favorites Management Commands
88+
Command::FavView => execute_fav_view(favorites_manager),
7289
Command::FavRm { index: _index } => execute_remove_fav(_index, favorites_manager),
73-
Command::FavSet => {
74-
execute_fav_set(file_system_state, favorites_manager)
75-
// return Ok(String::from("Executed: Fav Set"))
76-
}
77-
Command::RunFav { index: _index } => {
78-
execute_run_fav(_index, favorites_manager)
79-
// return Ok(String::from("Executed: Run Fav"))
80-
}
81-
Command::Unknown { command } => {
82-
return Ok(String::from(format!(
83-
"Unexecuted: Unknown command {}",
84-
command
85-
)));
86-
}
90+
Command::FavSet => execute_fav_set(file_system_state, favorites_manager),
91+
Command::RunFav { index: _index } => execute_run_fav(_index, favorites_manager),
8792
}
8893
}
89-
9094
pub fn execute_change_drive(
9195
file_system_state: &mut FileSystemState,
9296
drive: String,
@@ -138,13 +142,15 @@ pub fn execute_list_all_cmd() -> Result<String, String> {
138142
"DIR2 Commands (All Case-insensitive)",
139143
"---------------------",
140144
"Meta Commands:",
145+
"Directory/File Commands:",
141146
"State Commands:",
142147
"Favorites Commands:",
143148
"Search Commands:",
144149
];
145150

146151
let meta_commands = [
147152
("CLS | /C", "Clear Screen"),
153+
("CML <command>", "Executes a command in the terminal"),
148154
("LC", "Lists Commands"),
149155
("WD", "Watch Directory"),
150156
("LD", "List Directory"),
@@ -153,6 +159,18 @@ pub fn execute_list_all_cmd() -> Result<String, String> {
153159
("EXIT | /E", "Exit Terminal"),
154160
];
155161

162+
let dir_file_commands = [
163+
("MKDIR <directory>", "Creates a directory"),
164+
("RMDIR <directory>", "Removes a directory"),
165+
(
166+
"RENDIR <old_directory> <new_directory>",
167+
"Renames a directory",
168+
),
169+
("MKFILE <filename>", "Creates a file"),
170+
("RMFILE <filename>", "Removes a file"),
171+
("RENFILE <old_filename> <new_filename>", "Renames a file"),
172+
];
173+
156174
let state_commands = [
157175
(
158176
"SELECT <filename.ext> FROM <directory>",
@@ -207,16 +225,21 @@ pub fn execute_list_all_cmd() -> Result<String, String> {
207225
}
208226

209227
println!("\n{}", titles[3]);
228+
for (command, description) in dir_file_commands.iter() {
229+
println!("{} : {}", command.bright_cyan(), description);
230+
}
231+
232+
println!("\n{}", titles[4]);
210233
for (command, description) in state_commands.iter() {
211234
println!("{} : {}", command.yellow(), description);
212235
}
213236

214-
println!("\n{}", titles[4]);
237+
println!("\n{}", titles[5]);
215238
for (command, description) in fav_commands.iter() {
216239
println!("{} : {}", command.green(), description);
217240
}
218241

219-
println!("\n{}", titles[5]);
242+
println!("\n{}", titles[6]);
220243
for (command, description) in search_commands.iter() {
221244
println!("{} : {}", command.bright_purple(), description);
222245
}
@@ -462,6 +485,111 @@ pub fn execute_list_directory(sys_state: &mut FileSystemState) -> Result<String,
462485
}
463486
return Ok(String::from("Executed: List Directory"));
464487
}
488+
489+
fn execute_make_directory(
490+
sys_state: &mut FileSystemState,
491+
directory: &String,
492+
) -> Result<String, String> {
493+
let current_path = sys_state.get_current_path();
494+
let new_path = current_path.join(directory);
495+
if filesystem::path_exists(&new_path) {
496+
return Err(format!("Directory '{}' already exists", directory));
497+
}
498+
if filesystem::is_dir(&new_path) {
499+
return Err(format!("'{}' is already a directory", directory));
500+
}
501+
if filesystem::create_dir(&new_path) {
502+
sys_state.set_current_directory(new_path);
503+
return Ok(format!("Created directory '{}'", directory));
504+
}
505+
return Err(format!("Failed to create directory '{}'", directory));
506+
}
507+
508+
fn execute_remove_directory(
509+
sys_state: &mut FileSystemState,
510+
directory: &String,
511+
) -> Result<String, String> {
512+
let current_path = sys_state.get_current_path();
513+
let new_path = current_path.join(directory);
514+
if filesystem::is_dir(&new_path) {
515+
if filesystem::remove_dir(&new_path) {
516+
return Ok(format!("Removed directory '{}'", directory));
517+
} else {
518+
return Err(format!("Failed to remove directory '{}'", directory));
519+
}
520+
}
521+
return Err(format!("Failed to create directory '{}'", directory));
522+
}
523+
524+
fn execute_rename_directory(
525+
sys_state: &mut FileSystemState,
526+
directory: &String,
527+
new_name: &String,
528+
) -> Result<String, String> {
529+
let current_path = sys_state.get_current_path();
530+
let new_path = current_path.join(directory);
531+
if filesystem::path_exists(&new_path) {
532+
if filesystem::is_dir(&new_path) {
533+
if filesystem::rename(&new_path, &current_path.join(new_name)) {
534+
return Ok(format!(
535+
"Renamed directory '{}' to '{}'",
536+
directory, new_name
537+
));
538+
} else {
539+
return Err(format!(
540+
"Failed to rename directory '{}' to '{}'",
541+
directory, new_name
542+
));
543+
}
544+
}
545+
}
546+
return Err(format!("Failed to create directory '{}'", directory));
547+
}
548+
549+
fn execute_make_file(sys_state: &mut FileSystemState, file: &String) -> Result<String, String> {
550+
let current_path = sys_state.get_current_path();
551+
let new_path = current_path.join(file);
552+
if filesystem::path_exists(&new_path) {
553+
return Err(format!("File '{}' already exists", file));
554+
}
555+
if filesystem::create_file(&new_path) {
556+
return Ok(format!("Created file '{}'", file));
557+
}
558+
return Err(format!("Failed to create file '{}'", file));
559+
}
560+
561+
fn execute_remove_file(sys_state: &mut FileSystemState, file: &String) -> Result<String, String> {
562+
let current_path = sys_state.get_current_path();
563+
let new_path = current_path.join(file);
564+
if filesystem::path_exists(&new_path) {
565+
if filesystem::remove_file(&new_path) {
566+
return Ok(format!("Removed file '{}'", file));
567+
} else {
568+
return Err(format!("Failed to remove file '{}'", file));
569+
}
570+
}
571+
return Err(format!("Failed to remove file '{}'", file));
572+
}
573+
574+
fn execute_rename_file(
575+
sys_state: &mut FileSystemState,
576+
file: &String,
577+
new_name: &String,
578+
) -> Result<String, String> {
579+
let current_path = sys_state.get_current_path();
580+
let new_path = current_path.join(file);
581+
if filesystem::path_exists(&new_path) {
582+
return Err(format!("File '{}' already exists", file));
583+
}
584+
if filesystem::rename(&new_path, &current_path.join(new_name)) {
585+
return Ok(format!("Renamed file '{}' to '{}'", file, new_name));
586+
} else {
587+
return Err(format!(
588+
"Failed to rename file '{}' to '{}'",
589+
file, new_name
590+
));
591+
}
592+
}
465593
pub fn execute_meta_state(sys_state: &mut FileSystemState) -> Result<String, String> {
466594
let current_state = sys_state.get_current_state();
467595

@@ -603,7 +731,10 @@ pub fn execute_fav_view(favorites_manager: &mut FavoritesManager) -> Result<Stri
603731
state.get_path().to_string_lossy()
604732
)
605733
}
606-
println!("\nUse {} to run Favorite at index.","RUN FAV <index> or RF <index>".yellow());
734+
println!(
735+
"\nUse {} to run Favorite at index.",
736+
"RUN FAV <index> or RF <index>".yellow()
737+
);
607738
return Ok(String::from("Done!"));
608739
}
609740

directory_2/src/completion.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+

directory_2/src/delegation.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#[cfg(windows)]
2+
use std::process::{Command, Stdio};
3+
4+
pub fn execute_with_piping(input: &str) -> Result<(), String> {
5+
#[cfg(windows)]
6+
let _ = Command::new("cmd")
7+
.args(&["/C", input])
8+
.current_dir("./")
9+
.status();
10+
11+
#[cfg(unix)]
12+
let _ = Command::new("sh")
13+
.args(&["-c", input])
14+
.current_dir("./")
15+
.status()?;
16+
Ok(())
17+
}
18+
19+
pub fn execute_using_cmd(input: &str) -> Result<(), String> {
20+
#[cfg(windows)]
21+
let status = Command::new("cmd")
22+
.args(&["/C", input])
23+
.current_dir("./")
24+
.stdin(Stdio::inherit())
25+
.stdout(Stdio::inherit())
26+
.stderr(Stdio::inherit())
27+
.status();
28+
29+
match status {
30+
Ok(status) => {
31+
if status.success() {
32+
return Ok(());
33+
} else {
34+
return Err(format!(
35+
"Command failed with exit code: {}",
36+
status.code().unwrap()
37+
));
38+
}
39+
}
40+
Err(e) => Err(format!("Command execution failed: {}", e)),
41+
}
42+
// Ok(())
43+
}

directory_2/src/file_system_state.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ use std::path::PathBuf;
44
#[derive(Debug)]
55
pub struct FileSystemState {
66
state: Option<PathBuf>,
7+
index: Vec<String>,
78
current_path: PathBuf,
89
}
910

1011
impl FileSystemState {
1112
pub fn new() -> Self {
1213
Self {
1314
state: None,
15+
index: Vec::new(),
1416
current_path: env::current_dir().unwrap_or_else(|_| PathBuf::from("/")),
1517
}
1618
}
@@ -23,6 +25,14 @@ impl FileSystemState {
2325
&self.current_path
2426
}
2527

28+
pub fn get_all_indexed(&self) -> &Vec<String> {
29+
&self.index
30+
}
31+
32+
pub fn set_index(&mut self, index: Vec<String>) {
33+
self.index = index;
34+
}
35+
2636
pub fn set_current_state(&mut self, new_path: PathBuf) {
2737
self.state = Some(new_path);
2838
}
@@ -35,4 +45,4 @@ impl FileSystemState {
3545
pub fn clear_state(&mut self) {
3646
self.state = None;
3747
}
38-
}
48+
}

0 commit comments

Comments
 (0)