New Recite Function
[FreeRecite.git] / src / ui / Cui.cpp
blob8445ae4bf7472c351e698c66a87aadf94a83ecb2
1 #include <set>
2 #include <fstream>
3 #include <cstdlib>
4 #include <iostream>
5 #include <string>
6 #include <Manager.h>
7 #include <Dict.h>
8 #include <Task.h>
9 #include <Reciter.h>
10 #include <Tester.h>
11 #include <ConfigHolder.h>
12 #include "Cui.h"
14 namespace freeRecite {
16 CUI::CUI()
18 if(!manager.load()) {
19 std::cerr << "Can't load " << configHolder.mgrFile() << std::endl
20 << "The program exit!\n";
21 exit(EXIT_FAILURE);
23 if(!dictionary.load()) {
24 std::cerr << "Can't load " << configHolder.dictFile() << std::endl
25 << "The program exit!\n";
26 exit(EXIT_FAILURE);
28 showActive();
31 CUI::~CUI()
36 void CUI::run() {
37 std::cout << "Welcome to Free Recite!"<< std::endl;
38 std::cout << "Type \"help\" for help;p" << std::endl;
39 while(true) {
40 switch(getCmd()) {
41 case 'e':{ //eixt
42 exit(EXIT_SUCCESS);
44 case 'n':{ //creat new task
45 creatNew();
46 break;
48 case 't':{ //test
49 test();
50 break;
52 case 'r': { //recite for the new works.
53 recite();
54 break;
56 case 'm':{
57 modify();
58 break;
60 case 's':{
61 showActive();
62 break;
64 case'h':{
65 help();
66 break;
68 default:{
69 std::cerr<< "Bad Command!"<<std::endl;
70 break;
72 } //End of swithch().
73 } //End of while().
74 } //End of run().
76 char CUI::getCmd() {
77 std::string cmd;
78 std::cout << "~_~Free Recite:> ";
79 std::cout.flush();
80 getLine(cmd);
82 if(cmd == "exit")
83 return 'e';
84 else if(cmd == "new")
85 return 'n';
86 else if(cmd == "test")
87 return 't';
88 else if(cmd == "recite")
89 return 'r';
90 else if(cmd == "modify")
91 return 'm';
92 else if(cmd == "showall")
93 return 's';
94 else if(cmd == "help")
95 return 'h';
96 else
97 return '*';
100 void CUI::creatNew() {
101 std::string fileName,taskName;
102 std::cout<<"Input the filename of the words. File Name: ";
103 getLine(fileName);
104 std::ifstream ifs(fileName.c_str());
105 if(!ifs.is_open()) {
106 std::cout << "Can't find the file.";
107 return;
109 std::set<std::string> wordSet;
110 std::string word, cmd;
111 while(ifs.good()) {
112 std::getline(ifs,word);
113 if(!dictionary.lookUp(word) && !word.empty()) {
114 std::cout << '\"' << word << '\"'
115 << " can't be fond in your dictionary"<<std::endl
116 << "Remove(R) or Modify(M) it? (R/m)" << std::endl;
117 getLine(cmd);
119 if(cmd == "M" || cmd == "m")
120 while(!modify(word))
122 else
123 continue;
125 wordSet.insert(word);
127 std::cout << "Give a task name for it?" << std::endl;
128 std::cout << "(Press Enter will give a default name): ";
129 cmd.clear();
130 getLine(cmd);
131 if(manager.creatTask(wordSet,cmd.c_str())) {
132 std::cout << "Creat a task SUCCESS!" << std::endl;
133 manager.refresh();
134 }else
135 std::cerr << "Can't creat a task :}" << std::endl;
138 void CUI::showAnswer(Scanner *scanner) const {
139 std::cout<<std::endl
140 <<"********************ANSWER********************"
141 <<std::endl
142 <<"*"<<std::endl
143 <<"* " <<dictionary.word()<< std::endl
144 <<"*" << std::endl
145 <<"* ["<<dictionary.phonetics() << "]"<<std::endl
146 <<"* " <<dictionary.translation() << std::endl
147 <<"*" << std::endl
148 <<"* Amount: "<< scanner->capability()
149 <<" Unrecited Num: "
150 << (scanner->size()-1) << std::endl
151 <<"**********************************************"
152 <<std::endl
153 <<std::endl;
156 void CUI::showResult(bool result) {
157 if(result) {
158 std::cout<<" ** * * "<<std::endl;
159 std::cout<<" * * * * "<<std::endl;
160 std::cout<<" * * ** "<<std::endl;
161 std::cout<<" * * * * "<<std::endl;
162 std::cout<<" ** * * "<<std::endl;
164 else {
165 std::cout<<"##### #### #### "<<std::endl;
166 std::cout<<"# # # # # "<<std::endl;
167 std::cout<<"##### #### #### "<<std::endl;
168 std::cout<<"# # # # # "<<std::endl;
169 std::cout<<"##### # # # # "<<std::endl;
173 void CUI::test() {
174 showActive();
175 if((manager.getActiveTasks()).size() == 0){
176 std::cout << "There's no tasks to review!" << std::endl;
177 return;
179 unsigned i;
180 std::cout<< "Which do you want to test [ 0 by default]: ";
181 std::cin >> i;
182 if(i >= (manager.getActiveTasks()).size()) {
183 std::cerr<< "Error task!" << std::endl;
184 return;
187 Tester tester;
188 time_t taskID = (manager.getActiveTasks())[i];
190 if(!tester.loadWords(taskID)) {
191 std::cerr << "error when load words!" << std::endl;
192 return;
195 clear();
196 time_t startTime = 0;
197 time_t endTime = 0;
198 time(&startTime);
199 char c;
200 std::cin.get(c);
201 std::string inputStr;
202 bool result;
204 while(tester.isValid()) {
205 if(dictionary.lookUp(tester.getWord())){
206 clear();
207 std::cout<<"[M]: "<<dictionary.translation()<<std::endl;
208 std::cout<<"Input: ";
209 getLine(inputStr);
210 if(inputStr == "\\modify")
211 modify(tester.getWord());
212 else if(inputStr == "\\stop")
213 return;
214 else if(inputStr == "\\exit")
215 exit(EXIT_FAILURE);
216 result = ( inputStr == tester.getWord() ? true : false );
217 showAnswer(&tester);
218 showResult(result);
219 tester.test(result);
223 time(&endTime);
224 startTime = endTime - startTime;
225 std::cout << std::endl << "Used Time: "
226 << startTime/60 << "minutes" << std::endl;
228 if(manager.test(taskID,tester.getScore())) {
229 std::cout<<"Your score is " << tester.getScore() << std::endl;
230 std::cout<<"You passed it!"<<std::endl;
232 else {
233 std::cout<<"Your score is " << tester.getScore() << std::endl
234 <<"You haven't passed it :} "<<std::endl;
237 time_t nextTime = manager.getNextTime(taskID);
238 if(manager.getTaskStep(taskID) == 8)
239 std::cout << "Congratulations! You have complish this task!"
240 << std::endl;
241 else {
242 struct tm * timeinfo;
243 char buffer[30];
244 timeinfo = localtime(&nextTime);
245 strftime(buffer,30,"%Y.%m.%d %H:%M:%S",timeinfo);
246 std::cout << "Then next reviewing time is: " << buffer
247 << std::endl << std::endl;
249 showActive();
252 void CUI::recite() {
253 showActive();
254 if((manager.getActiveTasks()).size() == 0){
255 std::cout << "There's no tasks to review!" << std::endl;
256 return;
258 unsigned i;
259 std::cout<< "Which do you want to test [ 0 by default]: ";
260 std::cin >> i;
261 if(i >= (manager.getActiveTasks()).size()) {
262 std::cerr<< "Error task!" << std::endl;
263 return;
266 Reciter reciter;
267 time_t taskID = (manager.getActiveTasks())[i];
269 if(!reciter.loadWords(taskID)) {
270 std::cerr << "error when load words!" << std::endl;
271 return;
274 clear();
275 time_t startTime = 0;
276 time_t endTime = 0;
277 time(&startTime);
278 char c;
279 std::cin.get(c);
280 std::string inputStr;
282 while(reciter.isValid()) {
283 if(dictionary.lookUp(reciter.getWord())){
284 showAnswer(&reciter);
285 std::cout<< "Press E to emphasize,Enter to continue......"
286 << std::endl;
287 getLine(inputStr);
288 if(inputStr == "\\modify")
289 modify(reciter.getWord());
290 else if(inputStr == "\\stop")
291 return;
292 else if(inputStr == "\\exit")
293 exit(EXIT_FAILURE);
294 else if(inputStr == "E" || inputStr == "e")
295 reciter.test(false);
296 else
297 reciter.test(true);
298 clear();
302 time(&endTime);
303 startTime = endTime - startTime;
304 std::cout << std::endl << "Used Time: "
305 << startTime/60 << "minutes" << std::endl;
307 showActive();
310 bool CUI::modify(const char *word) {
311 std::string newItem,inputStr;
312 if(word == 0) {
313 std::cout<<"[W]: ";
314 getLine(inputStr);
315 newItem.append("[W]"+inputStr);
316 }else {
317 newItem.append("[W]");
318 newItem.append(word);
321 std::cout<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl;
322 std::cout<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl;
323 std::cout<<"[T]: ";
324 getLine(inputStr);
325 newItem.append("[T]"+inputStr);
326 std::cout<<"[M]: ";
327 getLine(inputStr);
328 newItem.append("[M]"+inputStr);
330 if(!dictionary.modify(newItem)) {
331 std::cout<<"ERROR!" << std::endl;
332 return false;
334 std::cout<<"SUCCESS!"<<std::endl;
335 return true;
338 bool CUI::modify(const std::string &word) {
339 std::string newItem,inputStr;
340 if(word.empty()) {
341 std::cout<<"[W]: ";
342 getLine(inputStr);
343 newItem.append("[W]"+inputStr);
344 }else
345 newItem.append("[W]"+word);
347 std::cout<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl;
348 std::cout<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl;
349 std::cout<<"[T]: ";
350 getLine(inputStr);
351 newItem.append("[T]"+inputStr);
352 std::cout<<"[M]: ";
353 getLine(inputStr);
354 newItem.append("[M]"+inputStr);
356 if(!dictionary.modify(newItem)) {
357 std::cout<<"ERROR!" << std::endl;
358 return false;
360 std::cout<<"SUCCESS!"<<std::endl;
361 return true;
364 void CUI::showActive() {
365 clear();
366 if(!manager.refresh()) {
367 std::cout << "The configure file is broken!" << std::endl;
368 exit(EXIT_FAILURE);
370 if(manager.getActiveTaskNum() == 0) {
372 std::cout << "There's no new task should be review,type \"new\" to creat one."
373 << std::endl;
375 if(manager.getNextTime() != 0) {
376 time_t nextTime = manager.getNextTime();
377 struct tm * timeinfo;
378 char buffer[30];
379 timeinfo = localtime(&nextTime);
380 strftime(buffer,30,"%Y.%m.%d %H:%M:%S",timeinfo);
381 std::cout << "The nearest reviewing time is at " << buffer
382 << std::endl
383 << "Please use FreeRecite at that time!"
384 << std::endl << std::endl;
387 else {
388 std::cout << "There's " << manager.getActiveTaskNum()
389 << " tasks should review." << std::endl;
390 std::cout << "They are:" << std::endl;
392 for(int i = 0; i < manager.getActiveTaskNum(); ++i) {
394 std::cout << i << ": "
395 << manager.getTaskName(manager.getActiveTasks()[i])
396 << std::endl;
399 std::cout << std::endl << std::endl << std::endl;
403 void CUI::clear() {
404 for(int i = 0; i < 10; ++i)
405 std::cerr << std::endl;
408 void CUI::getLine(std::string &input) {
410 input.clear();
411 char t;
412 while(std::cin.get(t),t != '\n')
413 input += t;
416 void CUI::help() {
417 std::cout << "Welcome to use Free Recite!" << std::endl
418 << "There's server command here:" << std::endl
419 << " exit: Exit Free Recite normally." << std::endl
420 << " new: Creat a new tasks."<<std::endl
421 << " test: Test the tasks." << std::endl
422 << " recite: Recite the task without any mark set."<<std::endl
423 << " help: Get help like this;p"<<std::endl;
426 } // namespace freeRecite End.