-
Notifications
You must be signed in to change notification settings - Fork 0
/
repl.js
50 lines (47 loc) · 1.2 KB
/
repl.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
const repl = require("@alien.sh/repl");
const hasEqualParens = str =>
(str.match(/\(/g) || []).length == (str.match(/\)/g) || []).length;
class AlienRepl extends repl {
constructor({ core, ...rest }) {
super(rest);
this.core = core;
this.keyEaters["\r"] = [
function(key) {
if (this.isBusy) return;
if (this.currentInput && hasEqualParens(this.currentInput)) {
this.stdout.write("\n");
this.processInput();
} else {
this.insertAtCursor(key);
}
}
];
}
preprint() {
for (const hook of this.core.onBeforePrint) {
hook.call(this);
}
}
async processInput() {
let src = this.currentInput;
for (const hook of this.core.onBeforeProcess) {
src = hook.call(this, src);
}
this.y = 0;
this.x = 0;
this.currentInput = "";
this.currentOutput = "";
this.stdout.write("\n");
this.isBusy = true;
this.stdin.pause();
this.stdin.setRawMode(false);
await this.core.handle(this.core, src);
this.stdin.setRawMode(true);
this.stdin.resume();
this.isBusy = false;
this.stdout.write("\n");
this.preprint();
this.print();
}
}
module.exports = AlienRepl;