Merge work in kermit
[FreeRecite.git] / src / ui / Cui.cpp
blob6826114ddc53010eacc756ad7b75176ce8025f82
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);
81 if(cmd == "exit")
82 return 'e';
83 else if(cmd == "new")
84 return 'n';
85 else if(cmd == "test")
86 return 't';
87 else if(cmd == "recite")
88 return 'r';
89 else if(cmd == "modify")
90 return 'm';
91 else if(cmd == "showall")
92 return 's';
93 else if(cmd == "help")
94 return 'h';
95 else
96 return '*';
99 void CUI::creatNew() {
100 std::string fileName,taskName;
101 std::cout<<"Input the filename of the words. File Name: ";
102 getLine(fileName);
103 std::ifstream ifs(fileName.c_str());
104 if(!ifs.is_open()) {
105 std::cout << "Can't find the file." << std::endl;
106 return;
108 if(!ifs.eof()) {
109 std::cout << "This is a null file." << std::endl;
110 return;
112 std::set<std::string> wordSet;
113 std::string word, name, cmd;
114 std::cout << "Give a task name for it?" << std::endl;
115 std::cout << "(Press Enter will give a default name): ";
116 getLine(name);
117 while(ifs.good()) {
118 std::getline(ifs,word);
119 if(!dictionary.lookUp(word) && !word.empty()) {
120 std::cout << '\"' << word << '\"'
121 << " can't be fond in your dictionary"<<std::endl
122 << "Remove(R) or Modify(M) it? (R/m)" << std::endl;
123 getLine(cmd);
125 if(cmd == "M" || cmd == "m")
126 while(!modify(word))
128 else
129 continue;
131 wordSet.insert(word);
133 if(manager.createTask(wordSet,name.c_str(),30)) {
134 std::cout << "Creat a task SUCCESS!" << std::endl;
135 manager.refresh();
136 }else
137 std::cerr << "Can't creat a task :}" << std::endl;
140 void CUI::showResult(bool result) {
141 if(result) {
142 std::cout<<" ** * * "<<std::endl;
143 std::cout<<" * * * * "<<std::endl;
144 std::cout<<" * * ** "<<std::endl;
145 std::cout<<" * * * * "<<std::endl;
146 std::cout<<" ** * * "<<std::endl;
148 else {
149 std::cout<<"##### #### #### "<<std::endl;
150 std::cout<<"# # # # # "<<std::endl;
151 std::cout<<"##### #### #### "<<std::endl;
152 std::cout<<"# # # # # "<<std::endl;
153 std::cout<<"##### # # # # "<<std::endl;
157 void CUI::test() {
158 showActive();
159 if((manager.getActiveTasks()).size() == 0){
160 std::cout << "There's no tasks to review!" << std::endl;
161 return;
163 unsigned i;
164 std::cout<< "Which do you want to test [ 0 by default]: ";
165 std::cin >> i;
166 if(i >= (manager.getActiveTasks()).size()) {
167 std::cerr<< "Error task!" << std::endl;
168 return;
171 Tester tester;
172 time_t taskID = (manager.getActiveTasks())[i];
173 std::string inputStr;
174 bool randomFlag = true;
175 std::cout << "Do you want a random order (Y/n)";
176 getLine(inputStr);
177 if(inputStr == "N" || inputStr == "n")
178 randomFlag = false;
180 if(!tester.loadWords(taskID,randomFlag)) {
181 std::cerr << "error when load words!" << std::endl;
182 return;
185 clear();
186 time_t startTime = 0;
187 time_t endTime = 0;
188 time(&startTime);
189 char c;
190 std::cin.get(c);
191 bool result;
193 while(tester.isValid()) {
194 if(dictionary.lookUp(tester.getWord())){
195 clear();
196 std::cout <<" Amount: "<< tester.capability()
197 <<" R_Num: " << tester.size()
198 <<" R_Times: "<< tester.times()<<std::endl;
199 std::cout<<"[M]: "<<dictionary.translation()<<std::endl;
200 std::cout <<"**********************************************" << std::endl;
201 std::cout<<"*Input : ";
202 getLine(inputStr);
203 if(inputStr == "\\modify")
204 modify(tester.getWord());
205 else if(inputStr == "\\stop")
206 return;
207 else if(inputStr == "\\exit")
208 exit(EXIT_FAILURE);
209 result = ( inputStr == tester.getWord() ? true : false );
210 std::cout << "*Answer: " << dictionary.word();
211 if( !dictionary.phonetics().empty() )
212 std::cout << " /" << dictionary.phonetics() << "/" << std::endl;
213 else
214 std::cout << std::endl;
215 std::cout <<"**********************************************" << std::endl;
216 showResult(result);
217 tester.test(result);
221 time(&endTime);
222 startTime = endTime - startTime;
223 std::cout << std::endl << "Used Time: "
224 << startTime/60 << "minutes" << std::endl;
226 if(manager.test(taskID,tester.getScore())) {
227 std::cout<<"Your score is " << tester.getScore() << std::endl;
228 std::cout<<"You passed it!"<<std::endl;
230 else {
231 std::cout<<"Your score is " << tester.getScore() << std::endl
232 <<"You haven't passed it :} "<<std::endl;
235 time_t nextTime = manager.getNextTime(taskID);
236 if(manager.getTaskStep(taskID) == 8)
237 std::cout << "Congratulations! You have complish this task!"
238 << std::endl;
239 else {
240 struct tm * timeinfo;
241 char buffer[30];
242 timeinfo = localtime(&nextTime);
243 strftime(buffer,30,"%Y.%m.%d %H:%M:%S",timeinfo);
244 std::cout << "Then next reviewing time is: " << buffer
245 << std::endl << std::endl;
249 void CUI::recite() {
250 showActive();
251 if((manager.getActiveTasks()).size() == 0){
252 std::cout << "There's no tasks to review!" << std::endl;
253 return;
255 unsigned i;
256 std::cout<< "Which do you want to test: ";
257 std::cin >> i;
258 if(i >= (manager.getActiveTasks()).size()) {
259 std::cerr<< "Error task!" << std::endl;
260 return;
263 Reciter reciter;
264 std::string inputStr;
265 time_t taskID = (manager.getActiveTasks())[i];
266 bool randomFlag = false;
267 std::cout << "Do you want a random order (y/N)";
268 getLine(inputStr);
269 if(inputStr == "Y" || inputStr == "y")
270 randomFlag = true;
271 if(!reciter.loadWords(taskID,randomFlag)) {
272 std::cerr << "error when load words!" << std::endl;
273 return;
275 clear();
276 time_t startTime = 0;
277 time_t endTime = 0;
278 time(&startTime);
279 char c;
280 std::cin.get(c);
282 while(reciter.isValid()) {
283 if(dictionary.lookUp(reciter.getWord())){
284 std::cout <<" Amount: "<< reciter.capability()
285 <<" R_Num: " << reciter.size()
286 <<" R_Times: "<< reciter.times() << std::endl;
287 std::cout <<"[M]" << dictionary.translation() << std::endl;
288 std::cout <<"Press Enter to show the answer!" << std::endl;
289 getLine(inputStr);
290 std::cout <<"****************** ANSWER ********************" << std::endl;
291 std::cout <<"* " << dictionary.word() << std::endl;
292 if( !dictionary.phonetics().empty() )
293 std::cout << "* [" << dictionary.phonetics()<<"]" << std::endl;
294 std::cout <<"* " << dictionary.translation() << std::endl;
295 std::cout <<"**********************************************"
296 << std::endl << std::endl << std::endl;
297 std::cout << std::endl <<"Press E to emphasize it ..." << std::endl;
298 getLine(inputStr);
299 if(inputStr == "\\modify"){
300 modify(reciter.getWord());
301 } else if(inputStr == "\\add"){
302 std::cout << "Input new word: ";
303 std::cin >> inputStr;
304 reciter.add(inputStr);
305 } else if(inputStr == "\\rm" || inputStr == "remove"){
306 inputStr = reciter.getWord();
307 reciter.remove(inputStr);
308 } else if(inputStr == "\\stop"){
309 return;
310 } else if(inputStr == "\\exit"){
311 exit(EXIT_FAILURE);
312 } else if(inputStr == "E" || inputStr == "e"){
313 reciter.test(false);
314 } else{
315 reciter.test(true);
317 clear();
321 time(&endTime);
322 startTime = endTime - startTime;
323 std::cout << std::endl << "Used Time: "
324 << startTime/60 << "minutes" << std::endl;
326 showActive();
329 bool CUI::modify(const char *word) {
330 std::string newItem,inputStr;
331 if(word == 0) {
332 std::cout<<"[W]: ";
333 getLine(inputStr);
334 newItem.append("[W]"+inputStr);
335 }else {
336 newItem.append("[W]");
337 newItem.append(word);
340 std::cout<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl;
341 std::cout<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl;
342 std::cout<<"[T]: ";
343 getLine(inputStr);
344 newItem.append("[T]"+inputStr);
345 std::cout<<"[M]: ";
346 getLine(inputStr);
347 newItem.append("[M]"+inputStr);
349 if(!dictionary.modify(newItem)) {
350 std::cout<<"ERROR!" << std::endl;
351 return false;
353 std::cout<<"SUCCESS!"<<std::endl;
354 return true;
357 bool CUI::modify(const std::string &word) {
358 std::string newItem,inputStr;
359 if(word.empty()) {
360 std::cout<<"[W]: ";
361 getLine(inputStr);
362 newItem.append("[W]"+inputStr);
363 }else
364 newItem.append("[W]"+word);
366 std::cout<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl;
367 std::cout<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl;
368 std::cout<<"[T]: ";
369 getLine(inputStr);
370 newItem.append("[T]"+inputStr);
371 std::cout<<"[M]: ";
372 getLine(inputStr);
373 newItem.append("[M]"+inputStr);
375 if(!dictionary.modify(newItem)) {
376 std::cout<<"ERROR!" << std::endl;
377 return false;
379 std::cout<<"SUCCESS!"<<std::endl;
380 return true;
383 void CUI::showActive() {
384 clear();
385 if(!manager.refresh()) {
386 std::cout << "The configure file is broken!" << std::endl;
387 exit(EXIT_FAILURE);
389 if(manager.getActiveTaskNum() == 0) {
391 std::cout << "There's no new task should be review,type \"new\" to creat one."
392 << std::endl;
394 if(manager.getNextTime() != 0) {
395 time_t nextTime = manager.getNextTime();
396 struct tm * timeinfo;
397 char buffer[30];
398 timeinfo = localtime(&nextTime);
399 strftime(buffer,30,"%Y.%m.%d %H:%M:%S",timeinfo);
400 std::cout << "The nearest reviewing time is at " << buffer
401 << std::endl
402 << "Please use FreeRecite at that time!"
403 << std::endl << std::endl;
406 else {
407 std::cout << "There's " << manager.getActiveTaskNum()
408 << " tasks should review." << std::endl;
409 std::cout << "They are:" << std::endl;
411 for(int i = 0; i < manager.getActiveTaskNum(); ++i) {
413 std::cout << i << ": "
414 << manager.getTaskName(manager.getActiveTasks()[i])
415 << std::endl;
418 std::cout << std::endl << std::endl << std::endl;
422 void CUI::clear() {
423 for(int i = 0; i < 10; ++i)
424 std::cerr << std::endl;
427 void CUI::getLine(std::string &input) {
428 input.clear();
429 char t;
430 while(std::cin.get(t),t != '\n')
431 input += t;
434 void CUI::help() {
435 std::cout << "Welcome to use Free Recite!" << std::endl
436 << "There's server command here:" << std::endl
437 << " exit: Exit Free Recite normally." << std::endl
438 << " new: Creat a new tasks."<<std::endl
439 << " test: Test the tasks." << std::endl
440 << " recite: Recite the task without any mark set."<<std::endl
441 << " help: Get help like this;p"<<std::endl;
444 } // namespace freeRecite End.