Skip to content

Commit 9bd3fd2

Browse files
authored
Merge pull request #89 from hhzl/node-cli-1
Node command line testing
2 parents 39a447e + 71a00c7 commit 9bd3fd2

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed

scripts/demo.js

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
// ----------------------------------------------
2+
// LearnWords2 API usage
3+
//
4+
// creation:
5+
// LW = new BoxOfQuestions(new LWdb('learnWords'));
6+
//
7+
// methods and properties used
8+
//
9+
// LW.db.loadWords(wordlist)
10+
// LW.question()
11+
// LW.answer()
12+
// LW.moveQuestionForward()
13+
// LW.moveQuestionBackwards()
14+
//
15+
//
16+
//
17+
// Asks one word and then terminates
18+
// ----------------------------------------------
19+
20+
"use strict";
21+
var repl = require("repl");
22+
23+
24+
25+
var LWdb = require('../src/LWdb');
26+
var BoxOfQuestions = require('../src/BoxOfQuestions');
27+
28+
29+
var wordlist = require('../data/wordlist-en-ge.js');
30+
31+
32+
33+
34+
35+
36+
// ----------------------------------------------------------
37+
// Set up the LW (learnwords) object
38+
// ----------------------------------------------------------
39+
40+
var LW = new BoxOfQuestions(new LWdb('learnWords'));
41+
42+
if (LW.db.numberOfWords() == 0) {LW.db.loadWords(wordlist)};
43+
44+
45+
46+
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.');
71+
console.log('');
72+
};
73+
74+
printLWHelp();
75+
76+
77+
78+
79+
// start Read-Eval-Print-Loop server
80+
81+
var replServer = repl.start({prompt: "--> "});
82+
83+
replServer.context.LW = LW;
84+
85+
86+
87+
replServer.defineCommand('lw2help', {
88+
help: 'LearnWords2 commands',
89+
action: function(name) {
90+
printLWHelp();
91+
this.displayPrompt();
92+
}
93+
});
94+
95+
replServer.defineCommand('qw',{help: '(LW.question()).word', action: function() {
96+
console.log((LW.question()).word);
97+
this.displayPrompt();
98+
}
99+
});
100+
101+
102+
replServer.defineCommand('qo',{help: 'LW.question()', action: function() {
103+
console.log(LW.question());
104+
this.displayPrompt();
105+
}
106+
});
107+
108+
109+
110+
replServer.defineCommand('a', function() {
111+
console.log(LW.answer());
112+
this.displayPrompt();
113+
});
114+
115+
116+
replServer.defineCommand('ok', function() {
117+
LW.moveQuestionForward();
118+
this.displayPrompt();
119+
});
120+
121+
122+
replServer.defineCommand('nok', function() {
123+
LW.moveQuestionBackwards();
124+
this.displayPrompt();
125+
});

0 commit comments

Comments
 (0)