forked from OSSPhilippines/covid19-tracker-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-util.js
More file actions
75 lines (60 loc) · 1.89 KB
/
server-util.js
File metadata and controls
75 lines (60 loc) · 1.89 KB
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
/** Modified script from blessed-contrib server-util */
const blessed = require('blessed'),
contrib = require('blessed-contrib'),
Stream = require('stream');
function OutputBuffer(options) {
this.isTTY = true;
this.columns = options.cols;
this.rows = options.rows;
this.write = function(s) {
s = s.replace('\x1b8', '');
options.stream.write(s)
};
this.on = function() {};
}
function InputBuffer() {
this.isTTY = true;
this.isRaw = true;
this.emit = function() {};
this.setRawMode = function() {};
this.resume = function() {};
this.pause = function() {};
this.on = function() {};
}
createScreen = (opt = {}) => {
const output = new OutputBuffer({stream: opt.stream, cols: 250, rows: 50});
const input = new InputBuffer(); //required to run under forever since it replaces stdin to non-tty
const program = blessed.program({output: output, input: input});
let screen = blessed.screen({program: program});
return screen
}
loadTemplate = (gridTemplateClass, gridStyleTemplateClass, json, callback) => {
blessed.Screen.global = null
blessed.Program.global = null
const
customStream = new Stream.Transform()
screen = createScreen({stream: customStream}),
grid = new contrib.grid({rows: 12, cols: 14, screen: screen});
let result = []
if(!gridTemplateClass) throw new Error('No template loaded')
// parse template
gridTemplateClass.load(grid, json, gridStyleTemplateClass)
customStream._transform = function (chunk,encoding,done) {
result.push(chunk.toString())
done()
}
customStream._final = () => {
callback(result.join(''))
}
customStream.on('error', (e) => {
// do nothing here
})
screen.render()
setTimeout(() => {
customStream.end()
}, 1000)
}
module.exports = {
createScreen,
loadTemplate,
}