Skip to content

Commit 71a00c7

Browse files
committed
REPL, fixes #88
1 parent 4f92d81 commit 71a00c7

File tree

1 file changed

+68
-31
lines changed

1 file changed

+68
-31
lines changed

scripts/demo.js

Lines changed: 68 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
// ----------------------------------------------
1919

2020
"use strict";
21-
var readline = require('readline');
21+
var repl = require("repl");
22+
2223

2324

2425
var LWdb = require('../src/LWdb');
@@ -29,60 +30,96 @@ var wordlist = require('../data/wordlist-en-ge.js');
2930

3031

3132

32-
// set up the LW (learnwords) object
33+
34+
35+
36+
// ----------------------------------------------------------
37+
// Set up the LW (learnwords) object
38+
// ----------------------------------------------------------
3339

3440
var LW = new BoxOfQuestions(new LWdb('learnWords'));
3541

3642
if (LW.db.numberOfWords() == 0) {LW.db.loadWords(wordlist)};
3743

3844

39-
// clear console
40-
console.log('\x1Bc');
4145

4246

43-
// write title
44-
console.log('LearnWords2 demo');
47+
// ----------------------------------------------------------
48+
// Clear console and write title
49+
// ----------------------------------------------------------
50+
51+
console.log('\x1Bc');
52+
53+
function printLWHelp(){
54+
console.log('LearnWords2 Read-Eval-Print-Loop');
55+
console.log('Commands');
56+
console.log(' type .qw for (LW.question()).word');
57+
console.log(' type .qo for LW.question()');
58+
console.log(' type .a for LW.answer()');
59+
console.log(' type .ok for LW.moveQuestionForward()');
60+
console.log(' type .nok for LW.moveQuestionBackwards()');
61+
console.log('');
62+
console.log('JavaScript');
63+
console.log(' you may also directly evaluate JavaScript expressions such as');
64+
console.log(' LW.question() ');
65+
console.log(' LW.db.getWord(1) ');
66+
console.log('');
67+
console.log('Other');
68+
console.log(' type .help get general help.');
69+
console.log(' type .lw2help get this text.');
70+
console.log(' type .exit to terminate.');
4571
console.log('');
72+
};
4673

74+
printLWHelp();
4775

4876

49-
// get a question from the box
5077

51-
var aWord = LW.question();
52-
// gives for example
53-
// {_id: 1, "word": "apple", "translate": "der Apfel", "step": 0, "date": 0};
5478

79+
// start Read-Eval-Print-Loop server
5580

81+
var replServer = repl.start({prompt: "--> "});
5682

83+
replServer.context.LW = LW;
5784

58-
// if we got a question ask for the translation of the word
5985

60-
if (aWord) {
6186

62-
console.log('Type the answers:');
63-
console.log('');
87+
replServer.defineCommand('lw2help', {
88+
help: 'LearnWords2 commands',
89+
action: function(name) {
90+
printLWHelp();
91+
this.displayPrompt();
92+
}
93+
});
6494

95+
replServer.defineCommand('qw',{help: '(LW.question()).word', action: function() {
96+
console.log((LW.question()).word);
97+
this.displayPrompt();
98+
}
99+
});
65100

66-
var rl = readline.createInterface({
67-
input: process.stdin,
68-
output: process.stdout
101+
102+
replServer.defineCommand('qo',{help: 'LW.question()', action: function() {
103+
console.log(LW.question());
104+
this.displayPrompt();
105+
}
69106
});
70107

71108

72-
rl.question(aWord.word + " = ", function(answer) {
73-
answer = answer.toString().trim();
74109

75-
if (answer == LW.answer())
76-
{console.log('OK');
77-
LW.moveQuestionForward()}
78-
else {console.log('the correct answer is:', LW.answer());
79-
LW.moveQuestionBackwards()};
80-
rl.close();
81-
})
82-
} else {console.log('As of now there are no more words to repeat.')};
110+
replServer.defineCommand('a', function() {
111+
console.log(LW.answer());
112+
this.displayPrompt();
113+
});
114+
83115

116+
replServer.defineCommand('ok', function() {
117+
LW.moveQuestionForward();
118+
this.displayPrompt();
119+
});
84120

85-
// currently only one question is asked and then the program quits.
86-
// you have to restart the program to get the next question.
87-
//
88-
// the db entries are in the 'scratch' subdirectory
121+
122+
replServer.defineCommand('nok', function() {
123+
LW.moveQuestionBackwards();
124+
this.displayPrompt();
125+
});

0 commit comments

Comments
 (0)