14 #include <ConfigHolder.h>
18 namespace freeRecite
{
23 std::cerr
<< "Can't load " << configHolder
.mgrFile() << ".\n";
24 std::cerr
<< "You should use command \'frt-init\' to initialize.\n";
27 if(!dictionary
.load()) {
28 std::cerr
<< "Can't load the dictionary.\n";
29 std::cerr
<< "You should use command \'frt-init\' to initialize.\n";
38 void CUI::run(int argc
, char *argv
[]) {
43 if(!strcmp(argv
[1],"--help")) {
46 }else if(!strcmp(argv
[1],"--version")) {
47 std::cout
<< "Free Recite version 1.0" << std::endl
;
49 }else if(!strcmp(argv
[1],"all")) {
52 }else if(!strcmp(argv
[1],"cls")) {
55 }else if(!strcmp(argv
[1],"done")) {
58 }else if(!strcmp(argv
[1],"ls")) {
61 }else if(!strcmp(argv
[1],"modify")) {
62 modify(std::string(""));
64 }else if(!strcmp(argv
[1],"stress")) {
70 if(!strcmp(argv
[1],"export")) { //frt export <taskID>
71 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
74 } else { //frt export <filename>
75 exportFromFile(argv
[2]);
78 }else if(!strcmp(argv
[1],"new")) {
81 }else if(!strcmp(argv
[1],"merge")) {
84 }else if(!strcmp(argv
[1],"modify")) {
85 modify(std::string(argv
[2]));
87 }else if(!strcmp(argv
[1],"recite")) { //frt recite <taskID>
88 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
91 }else { //frt recite <fileName>
92 recite(argv
[2],false);
95 }else if(!strcmp(argv
[1],"rrecite")) { //frt recite <taskID>
96 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
99 }else { //frt recite <fileName>
100 recite(argv
[2],true);
103 }else if(!strcmp(argv
[1],"remove")) {
104 if( ( taskID
= atoid(argv
[2]) ) != 0 ){
108 }else if(!strcmp(argv
[1],"test")) {
109 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
113 }else if(!strcmp(argv
[1],"rtest")) {
114 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
119 } //else if(argc == 3)
121 std::cout
<< "frt: \'" << argv
[argc
-1] << "\' is not a frt-command. "
122 << "See \'frt --help\'." << std::endl
;
125 time_t CUI::atoid(const char *argv
) {
126 std::istringstream
ssm(argv
);
129 return static_cast<time_t>(tid
);
131 return static_cast<time_t>(0);
134 void CUI::cleanStress() {
135 ::remove(configHolder
.keyFile().c_str());
139 for(int i
= 0; i
< 10; ++i
)
140 std::cerr
<< std::endl
;
143 void CUI::createNew(const char *fileName
) {
144 std::string taskName
;
145 std::ifstream
ifs(fileName
);
147 std::cout
<< "Can't find the file." << std::endl
;
150 std::set
<std::string
> wordSet
;
151 std::string word
, name
, cmd
;
152 std::cout
<< "Give a task name for it?" << std::endl
;
153 std::cout
<< "(Press Enter will give a default name): ";
154 std::getline(std::cin
,name
);
156 std::getline(ifs
,word
);
157 while(!dictionary
.lookUp(word
) && !word
.empty()) {
158 std::cout
<< '\"' << word
<< '\"'
159 << " can't be fond in your dictionary"<<std::endl
160 << "Remove(R),Modify(M) or Add to dictionary(A)? (R/m/a)"
162 std::getline(std::cin
,cmd
);
164 if(cmd
== "A" || cmd
== "a")
165 while(!modify(word
)) /* An empty sentance. */;
166 else if(cmd
== "M" || cmd
== "m") {
167 std::cout
<< "Input the new word: ";
168 std::getline(std::cin
,word
); //Get a new word from the user.
171 std::getline(ifs
,word
); //Read a new word from file.
174 wordSet
.insert(word
);
176 unsigned taskMaxWords
= 0;
177 if(wordSet
.size() > 30)
181 if(manager
.createTask(wordSet
,name
.c_str(),taskMaxWords
)) {
182 std::cout
<< "Creat a task SUCCESS!" << std::endl
;
185 std::cerr
<< "Can't creat a task :}" << std::endl
;
189 void CUI::exportFromFile(const char *fileName
) {
190 std::ifstream
ifs(fileName
);
192 std::set
<std::string
> wdSet
;
197 std::getline(ifs
, tmpWord
);
199 wdSet
.insert(tmpWord
);
202 std::set
<std::string
>::const_iterator itr
= wdSet
.begin();
203 while(itr
!= wdSet
.end()) {
204 if(dictionary
.lookUp(*itr
)) {
205 std::cout
<< std::setw(12) << std::setfill(' ') << std::left
206 << dictionary
.word() << ' ';
207 if(!dictionary
.phonetics().empty())
208 std::cout
<< '[' << dictionary
.phonetics() << ']';
209 std::cout
<< ' ' << dictionary
.translation() << std::endl
;
211 std::cout
<< *itr
<< " *** " << std::endl
;
217 void CUI::exportDone() {
218 exportFromFile(configHolder
.doneFile().c_str());
221 void CUI::exportTask(time_t taskID
) {
222 if(!manager
.hasTask(taskID
)) {
223 std::cout
<< "There's no this task!" << std::endl
;
226 std::string fileName
= Scanner::getTaskFileName(taskID
);
227 exportFromFile(fileName
.c_str());
230 void CUI::exportStress(){
231 exportFromFile(configHolder
.keyFile().c_str());
234 void CUI:: merge(const char *fileName
) {
235 if(!dictionary
.merge(fileName
))
236 std::cerr
<< "FAIL" << std::endl
;
238 std::cerr
<< "SUCCESS" << std::endl
;
242 void CUI::test(time_t taskID
, bool reverse
) {
244 if(!manager
.hasTask(taskID
)) {
245 std::cout
<< "There's no this task!" << std::endl
;
248 if(!tester
.load(taskID
)) {
249 std::cerr
<< "error when load words!" << std::endl
;
252 time_t startTime
= 0;
257 r_scanProcess(tester
);
262 startTime
= endTime
- startTime
;
263 std::cout
<< std::endl
<< "Used Time: "
264 << startTime
/60 << "minutes" << std::endl
;
265 int testResult
= manager
.test(taskID
,tester
.getScore());
266 if(testResult
== 0) {
267 std::cout
<<"Your score is " << tester
.getScore() << std::endl
;
268 std::cout
<<"You passed it!"<<std::endl
;
269 time_t nextTime
= manager
.getNextTime(taskID
);
270 struct tm
* timeinfo
;
272 timeinfo
= localtime(&nextTime
);
273 strftime(buffer
,30,"%Y.%m.%d %H:%M:%S",timeinfo
);
274 std::cout
<< "Then next reviewing time is: " << buffer
276 }else if(testResult
== -1){
277 std::cout
<<"Your score is " << tester
.getScore() << std::endl
278 <<"You haven't passed it :} "<<std::endl
;
279 }else { // testResult == 1
280 std::cout
<< "Congratulations! You have complish this task!"
285 void CUI::recite(const char *fileName
, bool reverse
) {
287 if(!reciter
.load(fileName
)) {
288 std::cerr
<< "Can't open this file!" << std::endl
;
291 time_t startTime
= 0;
295 r_scanProcess(reciter
);
297 scanProcess(reciter
);
299 startTime
= (endTime
- startTime
)/60;
300 time_t usedTime
= startTime
> 0 ? startTime
: 1;
301 std::cout
<< std::endl
<< "Used Time: "
302 << usedTime
<< " minutes" << std::endl
;
303 float r_num
= reciter
.capability() - reciter
.getScore();
304 std::cout
<< "There're " << reciter
.capability() << " words in this task. "
305 << reciter
.getScore() << " of them you have known before."
306 << std::endl
<< "Your Reciting Rate is: "
308 << " word(s)/min" << std::endl
;
311 void CUI::recite(time_t taskID
, bool reverse
) {
312 if(!manager
.hasTask(taskID
)) {
313 std::cerr
<< "There's no this task!" << std::endl
;
318 if(!reciter
.load(taskID
)) {
319 std::cerr
<< "error when load words!" << std::endl
;
322 time_t startTime
= 0;
326 r_scanProcess(reciter
);
328 scanProcess(reciter
);
330 startTime
= (endTime
- startTime
)/60;
331 time_t usedTime
= startTime
> 0 ? startTime
: 1;
332 std::cout
<< std::endl
<< "Used Time: "
333 << usedTime
<< " minutes" << std::endl
;
334 float r_num
= reciter
.capability() - reciter
.getScore();
335 std::cout
<< "There're " << reciter
.capability() << " words in this task. "
336 << reciter
.getScore() << " of them you have known before."
337 << std::endl
<< "Your Reciting Rate is: "
339 << " word(s)/min" << std::endl
;
342 void CUI::remove(time_t taskID
) {
343 if(!manager
.hasTask(taskID
)) {
344 std::cerr
<< "There's no this task!" << std::endl
;
347 manager
.removeTask(taskID
);
350 bool CUI::modify(const std::string
&word
) {
351 std::string newItem
,inputStr
;
354 std::getline(std::cin
,inputStr
);
355 newItem
.append("[W]"+inputStr
);
357 newItem
.append("[W]"+word
);
359 std::cout
<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl
;
360 std::cout
<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl
;
362 std::getline(std::cin
,inputStr
);
363 newItem
.append("[T]"+inputStr
);
365 std::getline(std::cin
,inputStr
);
366 newItem
.append("[M]"+inputStr
);
368 if(!dictionary
.modify(newItem
)) {
369 std::cout
<<"ERROR!" << std::endl
;
372 std::cout
<<"SUCCESS!"<<std::endl
;
376 void CUI::showActive() {
377 if(!manager
.refresh()) {
378 std::cout
<< "The configure file is broken!" << std::endl
;
381 if(manager
.getActiveTaskNum() == 0) {
383 std::cout
<< "There's no new task should be review,type \"new\" to creat one."
386 if(manager
.getNextTime() != 0) {
387 time_t nextTime
= manager
.getNextTime();
388 struct tm
* timeinfo
;
390 timeinfo
= localtime(&nextTime
);
391 strftime(buffer
,30,"%Y.%m.%d %H:%M:%S",timeinfo
);
392 std::cout
<< "The nearest reviewing time is at " << buffer
394 << "Please use FreeRecite at that time!"
399 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< "N"
400 << std::setw(25) << std::setfill(' ') << std::left
<< "Name"
401 << std::setw(5) << std::setfill(' ') << std::left
<< "Step"
402 << std::setw(10) << std::setfill(' ') << std::left
<< " ID"
405 for(int i
= 0; i
< manager
.getActiveTaskNum(); ++i
) {
406 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< i
+1
407 << std::setw(25) << std::setfill(' ') << std::left
408 << manager
.getTaskName(manager
.getActiveTasks().at(i
)) << ' '
409 << std::setw(5) << std::setfill(' ') << std::left
410 << manager
.getTaskStep(manager
.getActiveTasks().at(i
))
411 << std::setw(10) << std::setfill(' ') << std::right
412 << manager
.getActiveTasks().at(i
)
418 void CUI::showAll() {
419 if(!manager
.refresh()) {
420 std::cout
<< "The configure file is broken!" << std::endl
;
423 if(manager
.getAllTasks().empty()) {
424 std::cout
<< "There's no new task should be review,type \"new\" to creat one."
428 std::cout
<< "There's " << manager
.getAllTasks().size()
429 << " tasks should review." << std::endl
;
430 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< "N"
431 << std::setw(20) << std::setfill(' ') << std::left
<< "Name"
432 << std::setw(5) << std::setfill(' ') << std::left
<< "Step"
433 << std::setw(12) << std::setfill(' ') << std::left
<< " ID"
434 << std::setw(12) << std::setfill(' ') << std::left
<< " Review Time"
437 std::map
<time_t,Task
>::const_iterator itr
= manager
.getAllTasks().begin();
439 while(++i
,itr
!= manager
.getAllTasks().end()) {
440 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< i
441 << std::setw(20) << std::setfill(' ') << std::left
442 << manager
.getTaskName(itr
->first
) << ' '
443 << std::setw(5) << std::setfill(' ') << std::left
444 << manager
.getTaskStep(itr
->first
)
445 << std::setw(12) << std::setfill(' ') << std::right
446 << itr
->first
<< " ";
447 time_t nextTime
= manager
.getNextTime(itr
->first
);
448 struct tm
* timeinfo
;
450 timeinfo
= localtime(&nextTime
);
451 strftime(buffer
,30,"%Y.%m.%d %H:%M:%S",timeinfo
);
452 std::cout
<< std::setw(12) << std::setfill(' ') << std::left
453 << buffer
<< std::endl
;
459 void CUI::showResult(bool result
) {
461 std::cout
<<" ** * * "<<std::endl
;
462 std::cout
<<" * * * * "<<std::endl
;
463 std::cout
<<" * * ** "<<std::endl
;
464 std::cout
<<" * * * * "<<std::endl
;
465 std::cout
<<" ** * * "<<std::endl
;
468 std::cout
<<"##### #### #### "<<std::endl
;
469 std::cout
<<"# # # # # "<<std::endl
;
470 std::cout
<<"##### #### #### "<<std::endl
;
471 std::cout
<<"# # # # # "<<std::endl
;
472 std::cout
<<"##### # # # # "<<std::endl
;
476 void CUI::r_scanProcess(Scanner
&scanner
) {
478 std::string inputStr
;
479 while(scanner
.isValid()) {
480 if(dictionary
.lookUp(scanner
.getWord())) {
481 std::cout
<<" Amount: "<< scanner
.capability()
482 <<" R_Num: " << scanner
.size()
483 <<" R_Times: "<< scanner
.times() << std::endl
;
484 std::cout
<<"**********************************************" << std::endl
;
485 std::cout
<<"*[W]: "<< dictionary
.word();
486 std::getline(std::cin
,inputStr
);
487 if( !dictionary
.phonetics().empty() )
488 std::cout
<< "*[T]: /" << dictionary
.phonetics() << "/" << std::endl
;
489 std::cout
<< "*[M]: " << dictionary
.translation() << std::endl
;
490 std::cout
<< "**********************************************" << std::endl
;
491 std::cout
<< "Type \'\\help\' to show the implicit command!" << std::endl
;
492 std::cout
<< "Do you remember it?(Y/n) ";
494 std::getline(std::cin
,inputStr
);
495 if(inputStr
== "\\help"){
498 } else if(inputStr
== "\\modify"){
499 modify(scanner
.getWord());
501 } else if(inputStr
== "\\add"){
502 std::cout
<< "Input new word: ";
503 std::getline(std::cin
,inputStr
);
504 scanner
.add(inputStr
);
505 std::cout
<< "SUCCESS!" << std::endl
;
507 } else if(inputStr
== "\\rm"){
508 inputStr
= scanner
.getWord();
509 scanner
.remove(inputStr
);
510 std::cout
<< "SUCCESS!" << std::endl
;
512 } else if(inputStr
== "\\stop") {
515 if(inputStr
== "N" || inputStr
== "n")
520 std::cout
<< std::endl
;
523 scanner
.test(result
);
524 } else { //If the dictionary can't look up the current word
525 std::cout
<< '\"' << scanner
.getWord() << '\"'
526 << " can't be found in your dictionary."
527 << "\n Modify Or Remove it from task(M/r) ";
529 std::getline(std::cin
,m_r
);
530 if(m_r
== "R" || m_r
== "r") {
531 inputStr
= scanner
.getWord();
532 scanner
.remove(inputStr
);
533 std::cout
<< "SUCCESS!" << std::endl
;
535 modify(scanner
.getWord());
541 void CUI::scanProcess(Scanner
&scanner
) {
543 std::string inputStr
;
544 while(scanner
.isValid()) {
545 if(dictionary
.lookUp(scanner
.getWord())){
546 std::cout
<<" Type \'\\help\' to show the implicit command!"
548 std::cout
<<" Amount: "<< scanner
.capability()
549 <<" R_Num: " << scanner
.size()
550 <<" R_Times: "<< scanner
.times()<<std::endl
;
551 std::cout
<<"[M]: "<<dictionary
.translation()<<std::endl
;
552 std::cout
<<"**********************************************" << std::endl
;
553 std::cout
<<"*Input : ";
554 std::getline(std::cin
,inputStr
);
556 if(inputStr
== "\\hint") {
557 std::cout
<< "*Hint : " << dictionary
.word().at(0);
558 for(unsigned i
= 1; i
< dictionary
.word().size(); ++i
)
560 if( !dictionary
.phonetics().empty() )
561 std::cout
<< " /" << dictionary
.phonetics() << "/";
562 std::cout
<< std::endl
<<"*Input : ";
563 std::getline(std::cin
,inputStr
);
566 if(inputStr
== "\\help"){
569 } else if(inputStr
== "\\modify"){
570 modify(scanner
.getWord());
572 } else if(inputStr
== "\\add"){
573 std::cout
<< "Input new word: ";
574 std::getline(std::cin
,inputStr
);
575 scanner
.add(inputStr
);
576 std::cout
<< "SUCCESS!" << std::endl
;
578 } else if(inputStr
== "\\rm"){
579 inputStr
= scanner
.getWord();
580 scanner
.remove(inputStr
);
581 std::cout
<< "SUCCESS!" << std::endl
;
583 } else if(inputStr
== "\\stop") {
586 result
= ( inputStr
== scanner
.getWord() ? true : false );
587 std::cout
<< "*Answer: " << dictionary
.word();
588 if( !dictionary
.phonetics().empty() )
589 std::cout
<< " /" << dictionary
.phonetics() << "/";
590 std::cout
<< std::endl
;
591 std::cout
<<"**********************************************" << std::endl
;
594 scanner
.test(result
);
595 } else { //If the dictionary can't look up the current word
596 std::cout
<< '\"' << scanner
.getWord() << '\"'
597 << " can't be found in your dictionary."
598 << "\n Modify Or Remove it from task(M/r) ";
600 std::getline(std::cin
,m_r
);
601 if(m_r
== "R" || m_r
== "r") {
602 inputStr
= scanner
.getWord();
603 scanner
.remove(inputStr
);
604 std::cout
<< "SUCCESS!" << std::endl
;
606 modify(scanner
.getWord());
612 void CUI::scanProHelp() {
613 std::cout
<< std::endl
<< "usage: \\command " << std::endl
614 << "\\help Show this help information" << std::endl
615 << "\\add Add new word to this task" << std::endl
616 << "\\rm Remove the current word from this task" << std::endl
617 << "\\modify Modify the current word in the dictionary" << std::endl
618 << "\\hint Get the hint of current word, but the translation can't use"
620 << "\\stop Stop Free Recite at once" << std::endl
;
624 std::cout
<< "usage: frt [--version] [--help] COMMAND [ARGS]" << std::endl
625 << "The most commonly used git commands are:" << std::endl
626 << " all Show the detail of all the tasks"
628 << " cls Clean the strees' words in the system"
630 << " done Export the words which you have remembered"
632 << " export <taskID> Export a tasks' words to the screen"
634 << " export <filename> Export the words in the file as it is a task"
636 << " ls List the tasks that should be reviewed"
638 << " new <filename> Creat new tasks with the words in the file"
640 << " merge <filename> Merge the dictionary into file"
642 << " modify [word] Modify the word in the dictionary"
644 << " recite <taskID> Recite the task whose ID is taskID"
646 << " recite <filename> Recite the words in the specified file"
648 << " rrecite <taskID> Recite from the translationg to word"
650 << " rrecite <filename> Recite from the translationg to word"
652 << " remove <taskID> Remove a task which you don't want to recite"
654 << " stress Show some words which may be difficult for you"
656 << " test <taskID> Test the task whose ID is taskID"
658 << " rtest <taskID> Test from translation to word"
660 << " --help Show this help information"
662 << " --version Show the current version"
666 } // namespace freeRecite End.