Add initial efforts
[minishell-2.git] / Sources / input.cpp
blob30b5fa002b89bf7800f09187a0e4a65a351a27f1
1 #include "Headers/opsys.h"
3 bool is_verbose = false;
4 bool cwd_changed = false;
6 CLIInputs::CLIInputs(int argc, char **argv)
8 if(argc > 1)
10 for(short unsigned int i = 1; i < argc; i++)
12 string s_argv = argv[i];
13 if (!s_argv.compare("-h") || !s_argv.compare("--help"))
15 printf("%-20s\tObtain help.\n", "-h, --help");
16 printf("%-20s\tShows additional execution data.\n", "-v, --verbose");
17 exit(0);
19 if (!s_argv.compare("-v") || !s_argv.compare("--verbose"))
21 printf("Executing in verbose mode.\n");
22 is_verbose = false;
28 void Prompt::get_last_dir()
30 string last_dir = OS.cwd;
31 string home_dir = getenv("HOME");
32 last_dir = std::regex_replace(last_dir, std::regex(home_dir), "~"); // Change HOME to ~ on the prompt line.
33 this->last_dir = last_dir;
34 cout << "PROMPT " << last_dir << '\n';
37 Prompt::Prompt()
39 get_last_dir();
42 void Prompt::print()
44 if (cwd_changed)
46 get_last_dir();
47 cwd_changed = false;
49 cout << BLUE_ANSI << OS.username << "@" << OS.hostname << ":" << RED_ANSI << last_dir << BLUE_ANSI << "$ " << RESET_ANSI;
52 void Prompt::parse(string input)
54 if(!tokens.empty())
55 tokens.clear();
56 istringstream iss (input);
57 copy(istream_iterator<string>(iss), istream_iterator<string>(), back_inserter(tokens));
58 for(unsigned short int i = 0; i < tokens.size(); i++)
59 cout << tokens[i] << ' ';
60 cout << '\n';
63 short int Prompt::run()
65 if(!tokens[0].compare("cd"))
67 OS.change_dir(tokens);
68 return cwd_changed;
69 } else if (!tokens[0].compare("pwd"))
71 cout << OS.cwd << '\n';
72 return 1;
73 } else if (!tokens[0].compare("help"))
75 cout << "Insert help text here.\n";
76 return 1;
77 } else
79 cout << "Wait.\n";
80 return 1;