-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathterminal.cpp
More file actions
195 lines (163 loc) · 4.31 KB
/
terminal.cpp
File metadata and controls
195 lines (163 loc) · 4.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include "terminal.h"
#include "tree displayer.h"
#include "strings.h"
#include <filesystem>
#include <iostream>
#include <vector>
#include <utility>
#include <string_view>
#include <map>
#include <algorithm>
#include <locale>
#include <string>
std::map<std::string_view, Terminal::Command> Terminal::commandMap
{
{"quit", Command::Quit},
{"dir", Command::Dir},
{"cd", Command::Cd},
};
std::map<std::string_view, Terminal::CdCommand> Terminal::cdCommandMap
{
{"root", CdCommand::root},
{"parent", CdCommand::parent},
{"child", CdCommand::child},
{"sibling", CdCommand::sibling},
{"-", CdCommand::back},
{"+", CdCommand::forward},
};
void Terminal::run() const
{
while(m_shouldRun)
{
printPath();
executeCommand(getInput());
}
}
void Terminal::printErrorMessage()
{
std::cout << "There\'s been an error. Please try again\n\n";
}
void Terminal::printPath() const
{
TreeDisplayer::printCurrentPath(m_tree);
std::cout << "> ";
}
std::string Terminal::getInput() const
{
std::string input{};
std::getline(std::cin >> std::ws, input);
while (input[0] == ' ')
input.erase(input.begin());
return String::getLowercase(std::move(input));
}
void Terminal::executeCommand(std::string_view command) const
{
const auto wordList { parseCommand(command) };
std::string_view firstCommand { wordList[0] };
if (!commandMap.contains(firstCommand) || wordList.size() > 2)
{
printErrorMessage();
return;
}
switch (commandMap[firstCommand])
{
case Command::Quit:
m_shouldRun = false;
break;
case Command::Dir:
TreeDisplayer::displayCurrent(m_tree);
break;
case Command::Cd:
executeCdCommand(wordList);
break;
}
std::cout << '\n';
}
void Terminal::executeCdCommand(const std::vector<std::string_view>& wordList) const
{
if (wordList.size() < 2)
{
TreeDisplayer::printCurrentPath(m_tree);
std::cout << '\n';
return;
}
std::string_view cdCommand { wordList[1] };
if (!cdCommandMap.contains(cdCommand))
{
if (cdCommand[0] == '\"')
{
cdCommand.remove_prefix(1);
cdCommand.remove_suffix(1);
}
const fs::path path{ cdCommand };
if (fs::exists(path) && fs::is_directory(path))
m_tree.iterator().toPath(path);
else
printErrorMessage();
return;
}
switch (cdCommandMap[cdCommand])
{
case CdCommand::root:
m_tree.iterator().toRoot();
break;
case CdCommand::parent:
m_tree.iterator().toParent();
break;
case CdCommand::child:
m_tree.iterator().toChild();
break;
case CdCommand::sibling:
m_tree.iterator().toSibling();
break;
case CdCommand::back:
m_tree.iterator().back();
break;
case CdCommand::forward:
m_tree.iterator().forward();
break;
}
}
std::vector<std::string_view> Terminal::parseCommand(std::string_view command) const
{
const auto wordCounter { static_cast<size_t>( countWords(command) ) };
std::vector<std::string_view> words{};
words.reserve(wordCounter);
while (!command.empty())
{
while (command[0] == ' ')
command.remove_prefix(1);
std::string_view foundWord{ getFirstWord(command) };
words.push_back(foundWord);
command.remove_prefix(foundWord.size());
}
return words;
}
int Terminal::countWords(std::string_view command) const
{
bool isPath { false };
return 1 + std::count_if
(
command.begin(), command.end(), [&isPath](char myChar)
{
if (myChar == '\"')
{
isPath = !isPath;
return false;
}
return (!isPath) && (myChar == ' ');
}
);
}
std::string_view Terminal::getFirstWord(std::string_view command) const
{
bool isPath{ false };
for (size_t i{0}; i + 1 < command.size(); ++i)
{
if (command[i] == '\"')
isPath = !isPath;
if (command[i + 1] == ' ' && !isPath)
return command.substr(0, i + 1);
}
return command;
}