diff --git a/noshx.cpp b/noshx.cpp index ef3aaa496233641e33e3e71cfa3821ad20fd03e6..08729dc402172df090663a47cb875bdda1005dd8 100644 --- a/noshx.cpp +++ b/noshx.cpp @@ -30,9 +30,14 @@ * * CHANGELOG * - * Noémi Shell X ver. 0.1 + * Noémi Shell X ver. 0.2 * - * THos version is the first version of the Noémi Shell for UNIX like systems + * This version introduses the cd command. In additon, this version is changing + * the prompt, to be more 'UNIX' like. + * The prompt will now always show the current working directory, without requiring + * the 'pwd' command. The 'pwd' command is NOT removed. + * This version also introduces colors to the terminal, as highlighting. This is NOT syntax highligthing + * that may come in a future update. * */ @@ -46,9 +51,10 @@ #include <cstring> // String operations #include <regex> // For grasp filtering #include <cstdlib> +#include <limits.h> -const char VERSION[] = "0.1"; ///< Change version number here for the entire code +const char VERSION[] = "0.2"; ///< Change version number here for the entire code #define STRLEN 100 ///< Max string length #define WIDTH 10 ///< Width between columns @@ -57,6 +63,7 @@ char vault[STRLEN]; ///< Storing custom info using namespace std; +namespace fs = std::filesystem; // Declaring functions void handle_pwd(const vector<string>& args); @@ -64,11 +71,11 @@ bool is_builtin_command(const vector<string>& args); void handle_noemi(const vector<string>& args); void handle_storage(const vector<string>& args); void handle_now(const vector<string>& args); +void handle_cd(const vector<string>& args); // To be implemented commands: //void handle_list(const vector<string>& args); -//void handle_cd(const vector<string>& args); //void handle_read(const vector<string>& args, string filename); //void handle_grasp(const vector<string>& args); //void handle_tap(const vector<string>& args); @@ -92,7 +99,15 @@ int main() { string command; // Storing command while (true) { - cout << "noshx> "; + + char buffer[PATH_MAX]; + + string cwd = getcwd(buffer, sizeof(buffer)); + + cout << "\033[1;33mnoshx: \033[0m" // Yellow "noshx:" + << "\033[1;96m" << cwd << "\033[0m" // Bold magenta cwd + << ">\033[0m "; // Normal > + getline(cin, command); // On 'exit' command @@ -158,14 +173,14 @@ void handle_help(const vector<string>& args){ << "about --version: Displays shell version only\n" << "exec: Execute an executable file\n" << "exit: Exit the shell\n" - << "grasp -d: Search through directories for other directories\n" - << "grasp -f: Search through directories for files\n" - << "list: List the files or other directories whithin a directory\n" + //<< "grasp -d: Search through directories for other directories\n" + //<< "grasp -f: Search through directories for files\n" + //<< "list: List the files or other directories whithin a directory\n" << "mkdir: Create a new directory whithin current directory\n" << "noemi: Draw a heart shape made of # characters\n" << "noemi -l: Draw a heart shape made of # characters with more delay\n" << "now: Gets and prints out the curent date and time\n" - << "rn: rename a file or a directory\n" + //<< "rn: rename a file or a directory\n" << "store: Save a string of maximum 100 characters during runtime\n" << "store -g: Show the string saved with store\n\n"; } @@ -308,7 +323,7 @@ bool is_builtin_command(const vector<string>& args){ } if (args[0] == "cd"){ - //handle_cd(args); + handle_cd(args); return true; } @@ -465,45 +480,51 @@ void handle_list(const vector<string>& args){ * @param args for arguments. */ -/* -void handle_cd(const vector<string>& args) { + + void handle_cd(const vector<string>& args) { if (args.size() > 2) { cout << "cd: too many arguments\n"; - return; // Exit early + return; } if (args.size() == 1) { // No argument, go to home directory - const char* home_dir = getenv("USERPROFILE"); // Get home directory on Windows + const char* home_dir = getenv("HOME"); // HOME is the Unix/Linux variable if (home_dir) { fs::path home_path = home_dir; - fs::current_path(home_path); // Change to home directory - cout << "Changed directory to: " << home_path << endl; + try { + fs::current_path(home_path); // Change to home directory + } catch (const fs::filesystem_error& e) { + cerr << "cd: " << e.what() << endl; + } } else { - cout << "cd: Home directory not found.\n"; + cerr << "cd: HOME environment variable not set.\n"; } } else if (args[1] == "..") { // cd .., go up one directory level - fs::path current_path = fs::current_path(); - fs::path parent_path = current_path.parent_path(); // Get the parent directory - fs::current_path(parent_path); // Change the current path - cout << "Changed directory to: " << parent_path << endl; + try { + fs::path parent_path = fs::current_path().parent_path(); + fs::current_path(parent_path); + } catch (const fs::filesystem_error& e) { + cerr << "cd: " << e.what() << endl; + } } else { // Try to change to the directory specified by the user fs::path new_path = args[1]; - if (fs::exists(new_path) && fs::is_directory(new_path)) { - fs::current_path(new_path); - cout << "Changed directory to: " << new_path << endl; - } else { - cout << "cd: " << new_path << ": No such directory\n"; + try { + if (fs::exists(new_path) && fs::is_directory(new_path)) { + fs::current_path(new_path); + } else { + cerr << "cd: " << new_path << ": No such directory\n"; + } + } catch (const fs::filesystem_error& e) { + cerr << "cd: " << e.what() << endl; } } - - } -*/ + /** * Handling the 'read' command for reading text from text based files.