-
Notifications
You must be signed in to change notification settings - Fork 1
/
ui.js
77 lines (61 loc) · 2.84 KB
/
ui.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
const readline = require('readline');
readline.emitKeypressEvents(process.stdin);
if (process.stdin.setRawMode){
process.stdin.setRawMode(true)
}
process.stdin.on('keypress', (str, key) => {
if (key.name === 'q' || ( key.ctrl && key.name == 'c')) {
UI.readline.cursorTo(process.stdout, 0, 0);
UI.readline.clearScreenDown(process.stdout);
process.exit();
} else {
if (key.name === 'p') {
UI.showPrompt();
return;
}
const cb = UI.bindings[key.name];
if (typeof cb === 'function') {
if (UI.paused) console.log('>>', key.name);
cb();
}
else {
if (UI.paused) {
console.log('Invalid command ! '+UI.promptMessage);
}
}
}
});
const UI = {
paused:true,
bindings:{},
readline:readline,
promptMessage:'[P] Pause mining | [B] Show the blockchain | [R] Resume mining | [Q] Quit',
bind:function(key, cb) {
UI.bindings[key] = cb;
},
showPrompt : function() {
UI.pause();
console.log(UI.promptMessage);
},
pause : function() {
UI.paused = true;
},
resume : function() {
UI.clear();
UI.paused=false;
},
clear : function() {
UI.readline.cursorTo(process.stdout, 0, 0);
UI.readline.clearScreenDown(process.stdout);
UI.readline.cursorTo(process.stdout, 0, 0);
UI.readline.clearLine(process.stdout, 0);
process.stdout.write('┌──────────────────────────────────────────────────────────────────────────────────┐\n');
process.stdout.write('│ ' + UI.promptMessage+' │\n');
process.stdout.write('└──────────────────────────────────────────────────────────────────────────────────┘\n');
process.stdout.write('┌──────────────────────────────────────────────────────────────────────────────────┐\n');
process.stdout.write('│ │\n');
process.stdout.write('└──────────────────────────────────────────────────────────────────────────────────┘\n');
}
}
UI.bind('r', UI.resume);
module.exports = UI;