From ba8f44c4c839b1e9acf92fdf61c129a2efa85ff8 Mon Sep 17 00:00:00 2001 From: Ashelyn Dawn Date: Sat, 11 Dec 2021 23:39:25 -0700 Subject: [PATCH] crappy ls --- src/commands.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/commands.rs b/src/commands.rs index 6afd510..1a1909a 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -2,6 +2,9 @@ use std::str::FromStr; use crate::{Directory, State}; +static PROJECTS_DIR: &'static str = "stuff"; +static HOME_DIR: &'static str = "projects"; + pub fn handle_command(command_string : String, state : &mut State) { let mut words = command_string.split(' '); @@ -21,8 +24,11 @@ pub fn handle_command(command_string : String, state : &mut State) { Directory::Home => { match args[0] { "~" => (), + "~/" => (), ".." => state.output.push_back(String::from_str("bash: cd: ..: Permission denied").unwrap()), + "../" => state.output.push_back(String::from_str("bash: cd: ..: Permission denied").unwrap()), "projects" => state.current_working_directory = Directory::Projects, + "projects/" => state.current_working_directory = Directory::Projects, _ => { if args[0].chars().next().unwrap() == '/' { state.output.push_back(format!("bash: cd: {}: Permission denied", args[0])); @@ -35,7 +41,9 @@ pub fn handle_command(command_string : String, state : &mut State) { Directory::Projects => { match args[0] { "~" => state.current_working_directory = Directory::Home, + "~/" => state.current_working_directory = Directory::Home, ".." => state.current_working_directory = Directory::Home, + "../" => state.current_working_directory = Directory::Home, _ => { if args[0].chars().next().unwrap() == '/' { state.output.push_back(format!("bash: cd: {}: Permission denied", args[0])); @@ -49,6 +57,27 @@ pub fn handle_command(command_string : String, state : &mut State) { } } } + + "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")) + } + } _ => { state.output.push_back(format!("bash: {}: command not found", command));