-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
236 lines (198 loc) · 5.55 KB
/
main.cpp
File metadata and controls
236 lines (198 loc) · 5.55 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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
#include <algorithm>
#include <chrono>
#include <fstream>
#include <iostream>
#include <random>
#include <string>
#include <vector>
// a struct to contain the questions, answers, and a boolean flag
struct Item {
std::string question;
std::string answer;
bool show_answer = false;
};
/**
* Clears the screen and prints the splash text
* @param None
*
* @return - None
*/
void clear_screen() {
std::cout << std::string(100, '\n');
std::string intro = R"(
________ _________ ___ ___ ________ ___ ___ ________ ___ ___
|\ ____\|\___ ___\\ \|\ \|\ ___ \ |\ \ / /| |\ ____\|\ \ |\ \
\ \ \___|\|___ \ \_\ \ \\\ \ \ \_|\ \ \ \ \/ / / \ \ \___|\ \ \ \ \ \
\ \_____ \ \ \ \ \ \ \\\ \ \ \ \\ \ \ \ / / \ \ \ \ \ \ \ \ \
\|____|\ \ \ \ \ \ \ \\\ \ \ \_\\ \ \/ / / \ \ \____\ \ \____\ \ \
____\_\ \ \ \__\ \ \_______\ \_______\__/ / / \ \_______\ \_______\ \__\
|\_________\ \|__| \|_______|\|_______|\___/ / \|_______|\|_______|\|__|
\|_________| \|___|/
)";
std::cout << intro << std::endl;
}
/**
* Trims whitespace and returns the new string
* @param str - the string to be trimmed
*
* @return - the trimmed string
*/
std::string trim(std::string str) {
while (!str.empty() && std::isspace(str.back())) {
str.pop_back();
}
std::size_t pos = 0;
while (pos < str.size() && std::isspace(str[pos])) {
++pos;
}
return str.substr(pos);
}
/**
* Gets the file from the user input
* @param None
*
* @return - an input file stream
*/
std::ifstream get_file() {
std::ifstream file;
clear_screen();
while (true) {
std::cout << "Enter the .study file name: ";
std::string file_name;
std::getline(std::cin, file_name);
if (file_name == "q") {
exit(EXIT_SUCCESS);
}
file_name = trim(file_name);
file_name += ".study";
file.open(file_name);
if (file.is_open()) {
break;
} else {
std::cout << "File not found. Try again." << std::endl;
}
}
return file;
}
/**
* Parses the .study file and creates a vector of Items
* @param file - a reference to a input file stream
*
* @return - a vector of Items
*/
std::vector<Item> read_study_file(std::ifstream& file) {
std::vector<Item> data;
std::string temp;
while (std::getline(file, temp, '|')) {
Item new_item;
new_item.question = trim(temp);
std::getline(file, temp, '\n');
new_item.answer = trim(temp);
data.push_back(new_item);
}
file.close();
return data;
}
/**
* Outputs the item at the current index
* @param questions - a constant reference to a vector of Items
* @param current - a constant reference to an int for the index in questions
* to output
*
* @return - None
*/
void show_item(const std::vector<Item>& questions, const int& current) {
clear_screen();
if (questions[current].show_answer == false) {
std::cout << questions[current].question << std::endl;
} else {
std::cout << questions[current].answer << std::endl;
}
}
/**
* Outputs the keybindings
* @param None
*
* @return - None
*/
void cout_help() {
clear_screen();
std::string keybindings = R"(
Keybindings:
- Main Controls
- a or A - go back one card
- w or W - flip the current card
- s or S - flip the current card (also)
- d or D - go forward one card
- Misc.
- ? - help
- q or Q - quit Study CLI
)";
std::cout << keybindings << std::endl;
}
int main() {
std::ifstream file = get_file();
std::vector<Item> questions = read_study_file(file);
// create a time based seed for shuffling questions
unsigned long long seed =
std::chrono::system_clock::now().time_since_epoch().count();
// shuffle the questions based on the seed
std::shuffle(std::begin(questions), std::end(questions),
std::default_random_engine(seed));
const int last_index = questions.size() - 1;
int current = 0;
show_item(questions, current);
while (true) {
std::cout << ">> ";
std::string input;
std::getline(std::cin, input);
input = trim(input);
if (!input.empty()) {
switch (input[0]) {
case 'A':
case 'a':
// handles underflow of current
current--;
current < 0 ? current = last_index : false;
show_item(questions, current);
break;
case 'S':
case 's':
// flips the boolean for the current question
questions[current].show_answer == false
? questions[current].show_answer = true
: questions[current].show_answer = false;
show_item(questions, current);
break;
case 'W':
case 'w':
// flips the boolean for the current question
questions[current].show_answer == false
? questions[current].show_answer = true
: questions[current].show_answer = false;
show_item(questions, current);
break;
case 'D':
case 'd':
// handles overflow of current
current++;
current > last_index ? current = 0 : false;
show_item(questions, current);
break;
case 'Q':
case 'q':
return 0;
case '?':
cout_help();
break;
default:
std::cout << "Try again. You can type '?' for help." << std::endl;
break;
}
} else {
std::cout << "Try again. You can type '?' for help." << std::endl;
}
input.clear();
}
return 0;
}