You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.3 KiB
Rust

3 years ago
use std::str::FromStr;
use crate::{Directory, State};
3 years ago
static PROJECTS_DIR: &'static str = "stuff";
static HOME_DIR: &'static str = "projects";
3 years ago
pub fn handle_command(command_string : String, state : &mut State) {
let mut words = command_string.split(' ');
let command = words.next().unwrap();
let args = words.collect::<Vec<&str>>();
match command {
"clear" => {
state.output.clear();
}
"cd" => {
if args.len() == 0 {
state.current_working_directory = Directory::Home;
} else {
match state.current_working_directory {
Directory::Home => {
match args[0] {
"~" => (),
3 years ago
"~/" => (),
3 years ago
".." => state.output.push_back(String::from_str("bash: cd: ..: Permission denied").unwrap()),
3 years ago
"../" => state.output.push_back(String::from_str("bash: cd: ..: Permission denied").unwrap()),
3 years ago
"projects" => state.current_working_directory = Directory::Projects,
3 years ago
"projects/" => state.current_working_directory = Directory::Projects,
3 years ago
_ => {
if args[0].chars().next().unwrap() == '/' {
state.output.push_back(format!("bash: cd: {}: Permission denied", args[0]));
} else {
state.output.push_back(format!("bash: cd: {}: No such file or directory", args[0]));
}
}
}
},
Directory::Projects => {
match args[0] {
"~" => state.current_working_directory = Directory::Home,
3 years ago
"~/" => state.current_working_directory = Directory::Home,
3 years ago
".." => state.current_working_directory = Directory::Home,
3 years ago
"../" => state.current_working_directory = Directory::Home,
3 years ago
_ => {
if args[0].chars().next().unwrap() == '/' {
state.output.push_back(format!("bash: cd: {}: Permission denied", args[0]));
} else {
state.output.push_back(format!("bash: cd: {}: No such file or directory", args[0]));
}
}
}
}
}
}
}
3 years ago
"ls" => match state.current_working_directory {
Directory::Home => match args.len() {
0 => state.output.push_back(String::from_str(HOME_DIR).unwrap()),
1 => match args[0] {
"projects" => state.output.push_back(String::from_str(PROJECTS_DIR).unwrap()),
"projects/" => state.output.push_back(String::from_str(PROJECTS_DIR).unwrap()),
_ => state.output.push_back(format!("bash: ls: '{}': No such file or directory", args[0]))
},
_ => state.output.push_back(format!("bash: ls: too many arguments"))
},
Directory::Projects => match args.len() {
0 => state.output.push_back(String::from_str(PROJECTS_DIR).unwrap()),
1 => match args[0] {
".." => state.output.push_back(String::from_str(HOME_DIR).unwrap()),
"../" => state.output.push_back(String::from_str(HOME_DIR).unwrap()),
_ => state.output.push_back(format!("bash: ls: '{}': No such file or directory", args[0]))
},
_ => state.output.push_back(format!("bash: ls: too many arguments"))
}
}
3 years ago
_ => {
state.output.push_back(format!("bash: {}: command not found", command));
}
}
}