From b51d9f47a5e4a55ab7c6073974f384e34ef91d10 Mon Sep 17 00:00:00 2001 From: hhzl Date: Wed, 7 Dec 2016 07:00:58 +0100 Subject: [PATCH 1/6] cli test start --- scripts/demo.js | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 scripts/demo.js diff --git a/scripts/demo.js b/scripts/demo.js new file mode 100644 index 0000000..3597b97 --- /dev/null +++ b/scripts/demo.js @@ -0,0 +1,23 @@ +"use strict"; + +var aWord = {_id: 1, "word": "apple", "translate": "der Apfel"}; + +console.log('\x1Bc'); + +console.log('LearnWords2 demo'); +console.log(''); +console.log('Type the answers:'); +console.log(''); + +// process.stdout.write('\x1B[2J'); + +console.log(aWord.word); + +process.stdin.once('data', function (data) { + var answer = data.toString().trim(); + + if (answer == aWord.translate) {console.log('OK')} + else {console.log('not OK')}; + + process.exit(); +}); From db948ce91dd18b4fc563e51236b57d229a728587 Mon Sep 17 00:00:00 2001 From: hhzl Date: Wed, 7 Dec 2016 07:17:39 +0100 Subject: [PATCH 2/6] require stmts added --- scripts/demo.js | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/scripts/demo.js b/scripts/demo.js index 3597b97..a588caf 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -1,6 +1,16 @@ "use strict"; +var LWdb = require('../src/LWdb'); +var BoxOfQuestions = require('../src/BoxOfQuestions'); +var wordlist = require('../data/wordlist-en-ge.js'); -var aWord = {_id: 1, "word": "apple", "translate": "der Apfel"}; + +var LW = new BoxOfQuestions(new LWdb('learnWords')); +LW.db.loadWords(wordlist); + + +// var aWord = {_id: 1, "word": "apple", "translate": "der Apfel"}; + +var aWord = LW.question(); console.log('\x1Bc'); @@ -9,7 +19,6 @@ console.log(''); console.log('Type the answers:'); console.log(''); -// process.stdout.write('\x1B[2J'); console.log(aWord.word); From 2488ec9fbbe77f77fd0a7de6d2aefdcdc1dc3486 Mon Sep 17 00:00:00 2001 From: hhzl Date: Wed, 7 Dec 2016 09:34:46 +0100 Subject: [PATCH 3/6] use readline --- scripts/demo.js | 54 +++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 11 deletions(-) diff --git a/scripts/demo.js b/scripts/demo.js index a588caf..4a18a80 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -3,14 +3,30 @@ var LWdb = require('../src/LWdb'); var BoxOfQuestions = require('../src/BoxOfQuestions'); var wordlist = require('../data/wordlist-en-ge.js'); +var readline = require('readline'); -var LW = new BoxOfQuestions(new LWdb('learnWords')); -LW.db.loadWords(wordlist); - +// ---------------------------------------------- +// LearnWords2 API usage +// +// creation: +// LW = new BoxOfQuestions(new LWdb('learnWords')); +// +// methods and properties used +// +// LW.db.loadWords(wordlist) +// LW.question() +// LW.answer() +// LW.moveQuestionForward() +// LW.moveQuestionBackwards() +// +// ---------------------------------------------- -// var aWord = {_id: 1, "word": "apple", "translate": "der Apfel"}; -var aWord = LW.question(); + +var LW = new BoxOfQuestions(new LWdb('learnWords')); + +if (LW.db.numberOfWords() == 0) {LW.db.loadWords(wordlist)}; + console.log('\x1Bc'); @@ -20,13 +36,29 @@ console.log('Type the answers:'); console.log(''); -console.log(aWord.word); +// var aWord = {_id: 1, "word": "apple", "translate": "der Apfel"}; + +var aWord = LW.question(); +console.log(aWord); + + +var rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); -process.stdin.once('data', function (data) { - var answer = data.toString().trim(); - if (answer == aWord.translate) {console.log('OK')} - else {console.log('not OK')}; - process.exit(); +rl.question(aWord.word + " = ", function(answer) { + answer = answer.toString().trim(); + + if (answer == LW.answer()) + {console.log('OK'); + LW.moveQuestionForward()} + else {console.log('the correct answer is:', LW.answer()); + LW.moveQuestionBackwards()}; +rl.close(); + }); + + From 1f64799d53893dd0003a8e65135f9a1cc075f0a7 Mon Sep 17 00:00:00 2001 From: hhzl Date: Wed, 7 Dec 2016 11:21:53 +0100 Subject: [PATCH 4/6] question() fixed --- scripts/demo.js | 1 - 1 file changed, 1 deletion(-) diff --git a/scripts/demo.js b/scripts/demo.js index 4a18a80..ce6e276 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -39,7 +39,6 @@ console.log(''); // var aWord = {_id: 1, "word": "apple", "translate": "der Apfel"}; var aWord = LW.question(); -console.log(aWord); var rl = readline.createInterface({ From 4f92d81e2257e49a32b97ef3ec91ba2611eedea4 Mon Sep 17 00:00:00 2001 From: hhzl Date: Thu, 8 Dec 2016 14:49:42 +0100 Subject: [PATCH 5/6] comment update --- scripts/demo.js | 51 ++++++++++++++++++++++++++++++++++++------------- 1 file changed, 38 insertions(+), 13 deletions(-) diff --git a/scripts/demo.js b/scripts/demo.js index ce6e276..6e4b101 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -1,10 +1,3 @@ -"use strict"; -var LWdb = require('../src/LWdb'); -var BoxOfQuestions = require('../src/BoxOfQuestions'); -var wordlist = require('../data/wordlist-en-ge.js'); - -var readline = require('readline'); - // ---------------------------------------------- // LearnWords2 API usage // @@ -19,26 +12,55 @@ var readline = require('readline'); // LW.moveQuestionForward() // LW.moveQuestionBackwards() // +// +// +// Asks one word and then terminates // ---------------------------------------------- +"use strict"; +var readline = require('readline'); + + +var LWdb = require('../src/LWdb'); +var BoxOfQuestions = require('../src/BoxOfQuestions'); + + +var wordlist = require('../data/wordlist-en-ge.js'); + + +// set up the LW (learnwords) object var LW = new BoxOfQuestions(new LWdb('learnWords')); if (LW.db.numberOfWords() == 0) {LW.db.loadWords(wordlist)}; +// clear console console.log('\x1Bc'); + +// write title console.log('LearnWords2 demo'); console.log(''); -console.log('Type the answers:'); -console.log(''); -// var aWord = {_id: 1, "word": "apple", "translate": "der Apfel"}; + +// get a question from the box var aWord = LW.question(); +// gives for example +// {_id: 1, "word": "apple", "translate": "der Apfel", "step": 0, "date": 0}; + + + + +// if we got a question ask for the translation of the word + +if (aWord) { + +console.log('Type the answers:'); +console.log(''); var rl = readline.createInterface({ @@ -47,7 +69,6 @@ var rl = readline.createInterface({ }); - rl.question(aWord.word + " = ", function(answer) { answer = answer.toString().trim(); @@ -57,7 +78,11 @@ rl.question(aWord.word + " = ", function(answer) { else {console.log('the correct answer is:', LW.answer()); LW.moveQuestionBackwards()}; rl.close(); - -}); +}) +} else {console.log('As of now there are no more words to repeat.')}; +// currently only one question is asked and then the program quits. +// you have to restart the program to get the next question. +// +// the db entries are in the 'scratch' subdirectory From 71a00c71751601abf07413890b3dd444774c0e14 Mon Sep 17 00:00:00 2001 From: hhzl Date: Fri, 9 Dec 2016 11:29:45 +0100 Subject: [PATCH 6/6] REPL, fixes #88 --- scripts/demo.js | 99 +++++++++++++++++++++++++++++++++---------------- 1 file changed, 68 insertions(+), 31 deletions(-) diff --git a/scripts/demo.js b/scripts/demo.js index 6e4b101..dfe7810 100644 --- a/scripts/demo.js +++ b/scripts/demo.js @@ -18,7 +18,8 @@ // ---------------------------------------------- "use strict"; -var readline = require('readline'); +var repl = require("repl"); + var LWdb = require('../src/LWdb'); @@ -29,60 +30,96 @@ var wordlist = require('../data/wordlist-en-ge.js'); -// set up the LW (learnwords) object + + + +// ---------------------------------------------------------- +// Set up the LW (learnwords) object +// ---------------------------------------------------------- var LW = new BoxOfQuestions(new LWdb('learnWords')); if (LW.db.numberOfWords() == 0) {LW.db.loadWords(wordlist)}; -// clear console -console.log('\x1Bc'); -// write title -console.log('LearnWords2 demo'); +// ---------------------------------------------------------- +// Clear console and write title +// ---------------------------------------------------------- + +console.log('\x1Bc'); + +function printLWHelp(){ +console.log('LearnWords2 Read-Eval-Print-Loop'); +console.log('Commands'); +console.log(' type .qw for (LW.question()).word'); +console.log(' type .qo for LW.question()'); +console.log(' type .a for LW.answer()'); +console.log(' type .ok for LW.moveQuestionForward()'); +console.log(' type .nok for LW.moveQuestionBackwards()'); +console.log(''); +console.log('JavaScript'); +console.log(' you may also directly evaluate JavaScript expressions such as'); +console.log(' LW.question() '); +console.log(' LW.db.getWord(1) '); +console.log(''); +console.log('Other'); +console.log(' type .help get general help.'); +console.log(' type .lw2help get this text.'); +console.log(' type .exit to terminate.'); console.log(''); +}; +printLWHelp(); -// get a question from the box -var aWord = LW.question(); -// gives for example -// {_id: 1, "word": "apple", "translate": "der Apfel", "step": 0, "date": 0}; +// start Read-Eval-Print-Loop server +var replServer = repl.start({prompt: "--> "}); +replServer.context.LW = LW; -// if we got a question ask for the translation of the word -if (aWord) { -console.log('Type the answers:'); -console.log(''); +replServer.defineCommand('lw2help', { + help: 'LearnWords2 commands', + action: function(name) { + printLWHelp(); + this.displayPrompt(); + } +}); +replServer.defineCommand('qw',{help: '(LW.question()).word', action: function() { + console.log((LW.question()).word); + this.displayPrompt(); + } +}); -var rl = readline.createInterface({ - input: process.stdin, - output: process.stdout + +replServer.defineCommand('qo',{help: 'LW.question()', action: function() { + console.log(LW.question()); + this.displayPrompt(); + } }); -rl.question(aWord.word + " = ", function(answer) { - answer = answer.toString().trim(); - if (answer == LW.answer()) - {console.log('OK'); - LW.moveQuestionForward()} - else {console.log('the correct answer is:', LW.answer()); - LW.moveQuestionBackwards()}; -rl.close(); -}) -} else {console.log('As of now there are no more words to repeat.')}; +replServer.defineCommand('a', function() { + console.log(LW.answer()); + this.displayPrompt(); +}); + +replServer.defineCommand('ok', function() { + LW.moveQuestionForward(); + this.displayPrompt(); +}); -// currently only one question is asked and then the program quits. -// you have to restart the program to get the next question. -// -// the db entries are in the 'scratch' subdirectory + +replServer.defineCommand('nok', function() { + LW.moveQuestionBackwards(); + this.displayPrompt(); +});