-
Notifications
You must be signed in to change notification settings - Fork 10
/
readline.js
146 lines (127 loc) · 3.92 KB
/
readline.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
var readline = require('readline');
var fs = require('fs');
module.exports = class Readline {
constructor(transcript, filename) {
var self = this;
this.readline = readline.createInterface({
input: process.stdin,
output: process.stdout
});
this.engine = null;
this.boundAnswer = answer;
this.transcript = transcript;
this.history = [];
this.state = new Play(this, filename);
Object.seal(this);
function answer(text) {
self.answer(text);
}
}
ask(cue) {
this.readline.question((cue || '') + '> ', this.boundAnswer);
}
answer(text) {
if (this.transcript) {
this.transcript.write('> ' + text + '\n');
}
this.state = this.state.answer(text);
}
close() {
if (this.transcript) {
this.transcript.write('\n');
}
this.readline.close();
}
}
class Play {
constructor(readline, filename) {
this.readline = readline;
this.filename = filename || 'kni.waypoint';
}
answer(text) {
var engine = this.readline.engine;
if (text === 'quit') {
console.log('');
engine.dialog.close();
} else if (text === 'bt' || text === 'trace') {
engine.log();
engine.ask();
} else if (text === 'capture' || text === 'cap') {
console.log(JSON.stringify(engine.waypoint));
console.log('');
engine.ask();
} else if (text === 'save') {
console.log('');
engine.dialog.ask('file name [' + this.filename + ']> ');
return new Save(this, engine.waypoint, this.filename);
} else if (text === 'load') {
console.log('');
engine.dialog.ask('file name [' + this.filename + ']> ');
return new Load(this, this.filename);
} else if (text === 'back') {
console.log('');
if (this.readline.transcript) {
this.readline.transcript.write('\n');
}
if (this.readline.history.length <= 1) {
console.log('Meanwhile, at the beginning of recorded history...');
}
engine.waypoint = this.readline.history.pop();
engine.resume(engine.waypoint);
} else if (text === 'replay') {
console.log('');
if (this.readline.transcript) {
this.readline.transcript.write('\n');
}
engine.resume(engine.waypoint);
} else {
this.readline.history.push(engine.waypoint);
engine.answer(text);
}
return this;
}
saved(filename) {
var engine = this.readline.engine;
this.filename = filename;
engine.ask();
return this;
}
loaded(waypoint) {
var engine = this.readline.engine;
engine.resume(waypoint);
return this;
}
}
class Save {
constructor(parent, waypoint, filename) {
this.parent = parent;
this.waypoint = waypoint;
this.filename = filename;
}
answer(filename) {
var waypoint = JSON.stringify(this.waypoint);
filename = filename || this.filename;
fs.writeFileSync(filename, waypoint, 'utf8');
console.log('');
console.log('Waypoint written to ' + filename);
console.log(waypoint);
console.log('');
return this.parent.saved(filename);
}
}
class Load {
constructor(parent, filename) {
this.parent = parent;
this.filename = filename;
}
answer(filename) {
filename = filename || this.filename;
var waypoint = fs.readFileSync(filename, 'utf8');
console.log('');
console.log('Loaded from ' + filename);
console.log(waypoint);
console.log('');
return this.parent.loaded(JSON.parse(waypoint));
}
}