14 #include <ConfigHolder.h>
18 namespace freeRecite
{
23 std::cerr
<< "Can't load the config file.\n";
26 if(!dictionary
.load()) {
27 std::cerr
<< "Can't load the dictionary.\n";
36 void CUI::run(int argc
, char *argv
[]) {
41 if(!strcmp(argv
[1],"--help")) {
44 }else if(!strcmp(argv
[1],"--version")) {
45 std::cout
<< "Free Recite version 1.0" << std::endl
;
47 }else if(!strcmp(argv
[1],"all")) {
50 }else if(!strcmp(argv
[1],"cls")) {
53 }else if(!strcmp(argv
[1],"done")) {
56 }else if(!strcmp(argv
[1],"ls")) {
59 }else if(!strcmp(argv
[1],"modify")) {
60 modify(std::string(""));
62 }else if(!strcmp(argv
[1],"status")) {
65 }else if(!strcmp(argv
[1],"stress")) {
71 if(!strcmp(argv
[1],"export")) { //frt export <taskID>
72 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
75 } else { //frt export <filename>
76 exportFromFile(argv
[2]);
79 }else if(!strcmp(argv
[1],"new")) {
82 }else if(!strcmp(argv
[1],"merge")) {
85 }else if(!strcmp(argv
[1],"modify")) {
86 modify(std::string(argv
[2]));
88 }else if(!strcmp(argv
[1],"recite")) { //frt recite <taskID>
89 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
92 }else { //frt recite <fileName>
93 recite(argv
[2],false);
96 }else if(!strcmp(argv
[1],"rrecite")) { //frt recite <taskID>
97 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
100 }else { //frt recite <fileName>
101 recite(argv
[2],true);
104 }else if(!strcmp(argv
[1],"remove")) {
105 if( ( taskID
= atoid(argv
[2]) ) != 0 ){
109 }else if(!strcmp(argv
[1],"test")) {
110 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
114 }else if(!strcmp(argv
[1],"rtest")) {
115 if( ( taskID
= atoid(argv
[2]) ) != 0 ) {
120 } //else if(argc == 3)
122 std::cout
<< "frt: \'" << argv
[argc
-1] << "\' is not a frt-command. "
123 << "See \'frt --help\'." << std::endl
;
126 time_t CUI::atoid(const char *argv
) {
127 std::istringstream
ssm(argv
);
130 return static_cast<time_t>(tid
);
132 return static_cast<time_t>(0);
135 void CUI::cleanStress() {
136 ::remove(configHolder
.keyFile().c_str());
140 for(int i
= 0; i
< 10; ++i
)
141 std::cerr
<< std::endl
;
144 void CUI::createNew(const char *fileName
) {
145 std::string taskName
;
146 std::ifstream
ifs(fileName
);
148 std::cout
<< "Can't find the file." << std::endl
;
151 std::set
<std::string
> wordSet
;
152 std::string word
, name
, cmd
;
153 std::cout
<< "Give a task name for it?" << std::endl
;
154 std::cout
<< "(Press Enter will give a default name): ";
155 std::getline(std::cin
,name
);
157 std::getline(ifs
,word
);
158 while(word
[word
.size()-1] == ' ')
159 word
.erase(word
.size()-1);
160 while(!dictionary
.lookUp(word
) && !word
.empty()) {
161 std::cout
<< '\"' << word
<< '\"'
162 << " can't be fond in your dictionary"<<std::endl
163 << "Remove(R),Modify(M) or Add to dictionary(A)? (R/m/a)"
165 std::getline(std::cin
,cmd
);
167 if(cmd
== "A" || cmd
== "a")
168 while(!modify(word
)) /* An empty sentance. */;
169 else if(cmd
== "M" || cmd
== "m") {
170 std::cout
<< "Input the new word: ";
171 std::getline(std::cin
,word
); //Get a new word from the user.
174 std::getline(ifs
,word
); //Read a new word from file.
177 wordSet
.insert(word
);
179 unsigned taskMaxWords
= 0;
180 if(wordSet
.size() > 30)
184 if(manager
.createTask(wordSet
,name
.c_str(),taskMaxWords
)) {
185 std::cout
<< "Creat a task SUCCESS!" << std::endl
;
188 std::cerr
<< "Can't creat a task :}" << std::endl
;
192 void CUI::exportFromFile(const char *fileName
) {
193 std::ifstream
ifs(fileName
);
195 std::set
<std::string
> wdSet
;
200 std::getline(ifs
, tmpWord
);
201 while(tmpWord
[tmpWord
.size()-1] == ' ')
202 tmpWord
.erase(tmpWord
.size()-1);
204 wdSet
.insert(tmpWord
);
207 std::set
<std::string
>::const_iterator itr
= wdSet
.begin();
208 while(itr
!= wdSet
.end()) {
209 if(dictionary
.lookUp(*itr
)) {
210 std::cout
<< std::setw(12) << std::setfill(' ') << std::left
211 << dictionary
.word() << ' ';
212 std::cout
<< '[' << dictionary
.phonetics() << ']';
213 std::cout
<< ' ' << dictionary
.translation() << std::endl
;
215 std::cout
<< *itr
<< " *** " << std::endl
;
221 void CUI::exportDone() {
222 exportFromFile(configHolder
.doneFile().c_str());
225 void CUI::exportTask(time_t taskID
) {
226 if(!manager
.hasTask(taskID
)) {
227 std::cout
<< "There's no this task!" << std::endl
;
230 std::string fileName
= Scanner::getTaskFileName(taskID
);
231 exportFromFile(fileName
.c_str());
234 void CUI::exportStress(){
235 exportFromFile(configHolder
.keyFile().c_str());
238 void CUI:: merge(const char *fileName
) {
239 if(!dictionary
.merge(fileName
))
240 std::cerr
<< "FAIL" << std::endl
;
242 std::cerr
<< "SUCCESS" << std::endl
;
246 void CUI::test(time_t taskID
, bool reverse
) {
248 if(!manager
.hasTask(taskID
)) {
249 std::cout
<< "There's no this task!" << std::endl
;
252 if(!tester
.load(taskID
)) {
253 std::cerr
<< "error when load words!" << std::endl
;
256 time_t startTime
= 0;
261 r_scanProcess(tester
);
266 startTime
= endTime
- startTime
;
267 std::cout
<< std::endl
<< "Used Time: "
268 << startTime
/60 << "minutes" << std::endl
;
269 int testResult
= manager
.test(taskID
,tester
.getScore());
270 if(testResult
== 0) {
271 std::cout
<<"Your score is " << tester
.getScore() << std::endl
;
272 std::cout
<<"You passed it!"<<std::endl
;
273 time_t nextTime
= manager
.getNextTime(taskID
);
274 struct tm
* timeinfo
;
276 timeinfo
= localtime(&nextTime
);
277 strftime(buffer
,30,"%Y.%m.%d %H:%M:%S",timeinfo
);
278 std::cout
<< "Then next reviewing time is: " << buffer
280 }else if(testResult
== -1){
281 std::cout
<<"Your score is " << tester
.getScore() << std::endl
282 <<"You haven't passed it :} "<<std::endl
;
283 }else { // testResult == 1
284 std::cout
<< "Congratulations! You have complish this task!"
289 void CUI::recite(const char *fileName
, bool reverse
) {
291 if(!reciter
.load(fileName
)) {
292 std::cerr
<< "Can't open this file!" << std::endl
;
295 time_t startTime
= 0;
299 r_scanProcess(reciter
);
301 scanProcess(reciter
);
303 startTime
= (endTime
- startTime
)/60;
304 time_t usedTime
= startTime
> 0 ? startTime
: 1;
305 std::cout
<< std::endl
<< "Used Time: "
306 << usedTime
<< " minutes" << std::endl
;
307 float r_num
= reciter
.capability() - reciter
.getScore();
308 std::cout
<< "There're " << reciter
.capability() << " words in this task. "
309 << reciter
.getScore() << " of them you have known before."
310 << std::endl
<< "Your Reciting Rate is: "
312 << " word(s)/min" << std::endl
;
315 void CUI::recite(time_t taskID
, bool reverse
) {
316 if(!manager
.hasTask(taskID
)) {
317 std::cerr
<< "There's no this task!" << std::endl
;
322 if(!reciter
.load(taskID
)) {
323 std::cerr
<< "error when load words!" << std::endl
;
326 time_t startTime
= 0;
330 r_scanProcess(reciter
);
332 scanProcess(reciter
);
334 startTime
= (endTime
- startTime
)/60;
335 time_t usedTime
= startTime
> 0 ? startTime
: 1;
336 std::cout
<< std::endl
<< "Used Time: "
337 << usedTime
<< " minutes" << std::endl
;
338 float r_num
= reciter
.capability() - reciter
.getScore();
339 std::cout
<< "There're " << reciter
.capability() << " words in this task. "
340 << reciter
.getScore() << " of them you have known before."
341 << std::endl
<< "Your Reciting Rate is: "
343 << " word(s)/min" << std::endl
;
346 void CUI::remove(time_t taskID
) {
347 if(!manager
.hasTask(taskID
)) {
348 std::cerr
<< "There's no this task!" << std::endl
;
351 manager
.removeTask(taskID
);
354 bool CUI::modify(const std::string
&word
) {
355 std::string newItem
,inputStr
;
358 std::getline(std::cin
,inputStr
);
359 newItem
.append("[W]"+inputStr
);
361 newItem
.append("[W]"+word
);
363 std::cout
<<"θ_ɑ_ʌ_ә_є_æ_ɔ_ʃ_ð_ŋ_ʒ"<<std::endl
;
364 std::cout
<<"0_1_2_3_4_5_6_7_8_9_="<<std::endl
;
366 std::getline(std::cin
,inputStr
);
367 newItem
.append("[T]"+inputStr
);
369 std::getline(std::cin
,inputStr
);
370 newItem
.append("[M]"+inputStr
);
372 if(!dictionary
.modify(newItem
)) {
373 std::cout
<<"ERROR!" << std::endl
;
376 std::cout
<<"SUCCESS!"<<std::endl
;
381 int i
= manager
.currNumWords();
383 std::cerr
<< "Error occurs when read tasks" << std::endl
;
388 time_t earlyTask
= manager
.getAllTasks().begin()->first
;
390 current
= (current
- earlyTask
) / (3600 * 24); //Days
393 std::cout
<< "Current Amount: " << i
<< std::endl
394 << "Reciting Rate : " << i
/current
<< " words/day" << std::endl
395 << "The foremost one is created before " << current
<< " days."
399 void CUI::showActive() {
400 if(!manager
.refresh()) {
401 std::cout
<< "The configure file is broken!" << std::endl
;
404 if(manager
.getActiveTaskNum() == 0) {
406 std::cout
<< "There's no new task should be review,type \"new\" to creat one."
409 if(manager
.getNextTime() != 0) {
410 time_t nextTime
= manager
.getNextTime();
411 struct tm
* timeinfo
;
413 timeinfo
= localtime(&nextTime
);
414 strftime(buffer
,30,"%Y.%m.%d %H:%M:%S",timeinfo
);
415 std::cout
<< "The nearest reviewing time is at " << buffer
417 << "Please use FreeRecite at that time!"
422 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< "N"
423 << std::setw(25) << std::setfill(' ') << std::left
<< "Name"
424 << std::setw(5) << std::setfill(' ') << std::left
<< "Step"
425 << std::setw(10) << std::setfill(' ') << std::left
<< " ID"
428 for(int i
= 0; i
< manager
.getActiveTaskNum(); ++i
) {
429 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< i
+1
430 << std::setw(25) << std::setfill(' ') << std::left
431 << manager
.getTaskName(manager
.getActiveTasks().at(i
)) << ' '
432 << std::setw(5) << std::setfill(' ') << std::left
433 << manager
.getTaskStep(manager
.getActiveTasks().at(i
))
434 << std::setw(10) << std::setfill(' ') << std::right
435 << manager
.getActiveTasks().at(i
)
441 void CUI::showAll() {
442 if(!manager
.refresh()) {
443 std::cout
<< "The configure file is broken!" << std::endl
;
446 if(manager
.getAllTasks().empty()) {
447 std::cout
<< "There's no new task should be review,type \"new\" to creat one."
451 std::cout
<< "There's " << manager
.getAllTasks().size()
452 << " tasks should review." << std::endl
;
453 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< "N"
454 << std::setw(20) << std::setfill(' ') << std::left
<< "Name"
455 << std::setw(5) << std::setfill(' ') << std::left
<< "Step"
456 << std::setw(12) << std::setfill(' ') << std::left
<< " ID"
457 << std::setw(12) << std::setfill(' ') << std::left
<< " Review Time"
460 std::map
<time_t,Task
>::const_iterator itr
= manager
.getAllTasks().begin();
462 while(++i
,itr
!= manager
.getAllTasks().end()) {
463 std::cout
<< std::setw(5) << std::setfill(' ') << std::left
<< i
464 << std::setw(20) << std::setfill(' ') << std::left
465 << manager
.getTaskName(itr
->first
) << ' '
466 << std::setw(5) << std::setfill(' ') << std::left
467 << manager
.getTaskStep(itr
->first
)
468 << std::setw(12) << std::setfill(' ') << std::right
469 << itr
->first
<< " ";
470 time_t nextTime
= manager
.getNextTime(itr
->first
);
471 struct tm
* timeinfo
;
473 timeinfo
= localtime(&nextTime
);
474 strftime(buffer
,30,"%Y.%m.%d %H:%M:%S",timeinfo
);
475 std::cout
<< std::setw(12) << std::setfill(' ') << std::left
476 << buffer
<< std::endl
;
482 void CUI::showResult(bool result
) {
484 std::cout
<<" ** * * "<<std::endl
;
485 std::cout
<<" * * * * "<<std::endl
;
486 std::cout
<<" * * ** "<<std::endl
;
487 std::cout
<<" * * * * "<<std::endl
;
488 std::cout
<<" ** * * "<<std::endl
;
491 std::cout
<<"##### #### #### "<<std::endl
;
492 std::cout
<<"# # # # # "<<std::endl
;
493 std::cout
<<"##### #### #### "<<std::endl
;
494 std::cout
<<"# # # # # "<<std::endl
;
495 std::cout
<<"##### # # # # "<<std::endl
;
499 void CUI::r_scanProcess(Scanner
&scanner
) {
501 std::string inputStr
;
502 while(scanner
.isValid()) {
504 if(dictionary
.lookUp(scanner
.getWord())) {
505 std::cout
<<" Amount: "<< scanner
.capability()
506 <<" R_Num: " << scanner
.size()
507 <<" R_Times: "<< scanner
.times() << std::endl
;
508 std::cout
<<"**********************************************" << std::endl
;
509 std::cout
<<"*[W]: "<< dictionary
.word();
510 std::getline(std::cin
,inputStr
);
511 if( !dictionary
.phonetics().empty() )
512 std::cout
<< "*[T]: /" << dictionary
.phonetics() << "/" << std::endl
;
513 std::cout
<< "*[M]: " << dictionary
.translation() << std::endl
;
514 std::cout
<< "**********************************************" << std::endl
;
515 std::cout
<< "Type \'/help\' to show the implicit command!" << std::endl
;
516 std::cout
<< "Do you remember it?(Y/n) ";
518 std::getline(std::cin
,inputStr
);
519 if(inputStr
== "/help"){
522 } else if(inputStr
== "/modify"){
523 modify(scanner
.getWord());
525 } else if(inputStr
== "/add"){
526 std::cout
<< "Input new word: ";
527 std::getline(std::cin
,inputStr
);
528 scanner
.add(inputStr
);
529 std::cout
<< "SUCCESS!" << std::endl
;
531 } else if(inputStr
== "/rm"){
532 inputStr
= scanner
.getWord();
533 scanner
.remove(inputStr
);
534 std::cout
<< "SUCCESS!" << std::endl
;
536 } else if(inputStr
== "/stop") {
539 if(inputStr
== "Y" || inputStr
== "y" || inputStr
.empty())
544 std::cout
<< std::endl
;
546 scanner
.test(result
);
549 } else { //If the dictionary can't look up the current word
550 std::cout
<< '\"' << scanner
.getWord() << '\"'
551 << " can't be found in your dictionary."
552 << "\n Modify Or Remove it from task(M/r) ";
554 std::getline(std::cin
,m_r
);
555 if(m_r
== "R" || m_r
== "r") {
556 inputStr
= scanner
.getWord();
557 scanner
.remove(inputStr
);
558 std::cout
<< "SUCCESS!" << std::endl
;
560 modify(scanner
.getWord());
566 void CUI::scanProcess(Scanner
&scanner
) {
568 std::string inputStr
;
569 while(scanner
.isValid()) {
571 if(dictionary
.lookUp(scanner
.getWord())){
572 std::cout
<<" Type \'/help\' to show the implicit command!"
574 std::cout
<<" Amount: "<< scanner
.capability()
575 <<" R_Num: " << scanner
.size()
576 <<" R_Times: "<< scanner
.times()<<std::endl
;
577 std::cout
<<"[M]: "<<dictionary
.translation()<<std::endl
;
578 std::cout
<<"**********************************************" << std::endl
;
579 std::cout
<<"*Input : ";
580 std::getline(std::cin
,inputStr
);
582 if(inputStr
== "/hint") {
583 std::cout
<< "*Hint : " << dictionary
.word().at(0);
584 for(unsigned i
= 1; i
< dictionary
.word().size(); ++i
)
586 if( !dictionary
.phonetics().empty() )
587 std::cout
<< " /" << dictionary
.phonetics() << "/";
588 std::cout
<< std::endl
<<"*Input : ";
589 std::getline(std::cin
,inputStr
);
592 if(inputStr
== "/help"){
595 } else if(inputStr
== "/modify"){
596 modify(scanner
.getWord());
598 } else if(inputStr
== "/add"){
599 std::cout
<< "Input new word: ";
600 std::getline(std::cin
,inputStr
);
601 scanner
.add(inputStr
);
602 std::cout
<< "SUCCESS!" << std::endl
;
604 } else if(inputStr
== "/rm"){
605 inputStr
= scanner
.getWord();
606 scanner
.remove(inputStr
);
607 std::cout
<< "SUCCESS!" << std::endl
;
609 } else if(inputStr
== "//stop") {
612 result
= ( inputStr
== scanner
.getWord() ? true : false );
613 std::cout
<< "*Answer: " << dictionary
.word();
614 if( !dictionary
.phonetics().empty() )
615 std::cout
<< " /" << dictionary
.phonetics() << "/";
616 std::cout
<< std::endl
;
617 std::cout
<<"**********************************************" << std::endl
;
619 scanner
.test(result
);
620 } else { //If the dictionary can't look up the current word
621 std::cout
<< '\"' << scanner
.getWord() << '\"'
622 << " can't be found in your dictionary."
623 << "\n Modify Or Remove it from task(M/r) ";
625 std::getline(std::cin
,m_r
);
626 if(m_r
== "R" || m_r
== "r") {
627 inputStr
= scanner
.getWord();
628 scanner
.remove(inputStr
);
629 std::cout
<< "SUCCESS!" << std::endl
;
631 modify(scanner
.getWord());
637 void CUI::scanProHelp() {
638 std::cout
<< std::endl
<< "usage: /command " << std::endl
639 << "/help Show this help information" << std::endl
640 << "/add Add new word to this task" << std::endl
641 << "/rm Remove the current word from this task" << std::endl
642 << "/modify Modify the current word in the dictionary"
644 << "/hint Get the hint of current word, but the translation can't use"
646 << "/stop Stop Free Recite at once" << std::endl
;
650 std::cout
<< "usage: frt [--version] [--help] COMMAND [ARGS]" << std::endl
651 << "The most commonly used git commands are:" << std::endl
652 << " all Show the detail of all the tasks"
654 << " cls Clean the strees' words in the system"
656 << " done Export the words which you have remembered"
658 << " export <taskID> Export a tasks' words to the screen"
660 << " export <filename> Export the words in the file as it is a task"
662 << " ls List the tasks that should be reviewed"
664 << " new <filename> Creat new tasks with the words in the file"
666 << " merge <filename> Merge the dictionary into file"
668 << " modify [word] Modify the word in the dictionary"
670 << " recite <taskID> Recite the task whose ID is taskID"
672 << " recite <filename> Recite the words in the specified file"
674 << " rrecite <taskID> Recite from the translationg to word"
676 << " rrecite <filename> Recite from the translationg to word"
678 << " remove <taskID> Remove a task which you don't want to recite"
680 << " stress Show some words which may be difficult for you"
682 << " status Show how many words you are reciting"
684 << " test <taskID> Test the task whose ID is taskID"
686 << " rtest <taskID> Test from translation to word"
688 << " --help Show this help information"
690 << " --version Show the current version"
694 } // namespace freeRecite End.