Skip to content

Commit 6344472

Browse files
committed
feat: blessed to stream
1 parent 57a3416 commit 6344472

File tree

5 files changed

+174
-4
lines changed

5 files changed

+174
-4
lines changed

app.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,25 @@ app.get('/', async (req, res, next) => {
2323
}
2424
return next();
2525
});
26+
// historical chart by country
27+
app.get('/test/blessed', async (req, res, next) => {
28+
const blessedStream = require('./test-blessed')
29+
30+
let t = await new Promise((resolve, reject) => {
31+
blessedStream((data) => {
32+
resolve(data)
33+
})
34+
})
2635

36+
37+
38+
setTimeout(function() {
39+
res.send(t+'\r\n'+'\u2800'+'\033[?25h')
40+
},100)
41+
return null;
42+
43+
return next();
44+
});
2745
// global historical chart
2846
app.get(['/history/all/:chartType(cases|deaths)?', '/history/'], async (req, res, next) => {
2947
const userAgent = req.headers['user-agent'],
@@ -154,6 +172,9 @@ app.get('/history/charts/:country', async (req, res, next) => {
154172
return next();
155173
});
156174

175+
176+
177+
157178
app.get('*', (req, res) => res.send(`
158179
Welcome to COVID-19 Tracker CLI v${pkg.version} by Waren Gonzaga\n
159180
Please visit: https://warengonza.ga/covid19-tracker-cli

cli.js

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ const axios = require('axios'),
44
ora = require('ora'),
55
spinner = ora({ text: 'Loading...'}),
66
covid19 = require('./lib/cli'),
7+
covid19GFX = require ('./lib/cli/gfx'),
8+
http = require('http'),
79
apiBaseURL = "https://corona.lmao.ninja",
810
argv = require('yargs')
911
.usage('Usage: $0 <country> [options]')
@@ -49,12 +51,46 @@ const getCountry = async (u, country) => {
4951
spinner.stop();
5052
return console.log(result);
5153
}
54+
55+
56+
const getCountryGFX = async (u, country) => {
57+
let result;
58+
let resHead = {}
59+
const api = await axios.get(`${apiBaseURL}/countries/${country}`),
60+
history = await axios.get(`${apiBaseURL}/v2/historical/${api.data.country}?lastdays=all`),
61+
s = api.data,
62+
h = history.data,
63+
chartType = 'cases';
64+
65+
66+
var http = require('http');
67+
http.createServer(function (req, res) {
68+
resHead = res
69+
return covid19GFX.historyCountryTracker(
70+
req,res,
71+
s.country, s.cases, s.todayCases,
72+
s.deaths, s.todayDeaths, s.recovered,
73+
s.active, s.critical, s.casesPerOneMillion,
74+
s.updated, h, chartType, s.countryInfo)
75+
}).listen(1000);
76+
77+
result = await axios.get('http://localhost:1000')
78+
clear()
79+
spinner.stop();
80+
console.log(result.data)
81+
return 'dfdfd'
82+
83+
84+
85+
86+
}
87+
5288

5389
(async () => {
5490
clear();
55-
country = argv._[0];
5691
spinner.start();
92+
country = argv._[0];
5793
const all = await axios.get(`${apiBaseURL}/all`);
58-
!country && getGlobal(all.data);
59-
country && getCountry(all.data, country);
94+
getCountryGFX(all.data, country);
95+
6096
})()

lib/cli/gfx/presenter.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const present = (req, res, body, cba) => {
4242
}, '' ? 5000:0)
4343
}
4444

45-
catch (e) {
45+
catch (e) { console.log('-------',e)
4646
return cba(e)
4747
}
4848
})

lib/cli/gfx/server-util.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/** Modified script from blessed-contrib server-util */
2+
const blessed = require('blessed');
3+
4+
function OutputBuffer(options) {
5+
this.isTTY = true;
6+
this.columns = options.cols;
7+
this.rows = options.rows;
8+
this.write = function(s) {
9+
s = s.replace('\x1b8', ''); //not clear from where in blessed this code comes from. It forces the terminal to clear and loose existing content.
10+
options.stream.write(s)
11+
};
12+
13+
this.on = function() {};
14+
}
15+
16+
function InputBuffer() {
17+
this.isTTY = true;
18+
this.isRaw = true;
19+
20+
this.emit = function() {};
21+
22+
this.setRawMode = function() {};
23+
this.resume = function() {};
24+
this.pause = function() {};
25+
26+
this.on = function() {};
27+
}
28+
29+
function serverError(err) {
30+
throw new Error(err)
31+
}
32+
33+
34+
function createScreen(opt = {}) {
35+
let cols = opt.cols || 250;
36+
let rows = opt.rows || 50;
37+
38+
if (cols<=35 || cols>=500 || rows<=5 || rows>=300) {
39+
serverError('cols must be bigger than 35 and rows must be bigger than 5');
40+
return null;
41+
}
42+
43+
const output = new OutputBuffer({stream: opt.stream, cols: cols, rows: rows});
44+
const input = new InputBuffer(); //required to run under forever since it replaces stdin to non-tty
45+
const program = blessed.program({output: output, input: input});
46+
47+
let screen = blessed.screen({program: program});
48+
return screen
49+
}
50+
51+
52+
exports.createScreen = createScreen;

test-blessed.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
3+
const blessed = require('blessed'),
4+
contrib = require('blessed-contrib'),
5+
Stream = require('stream'),
6+
GFXUtil = require('./lib/cli/gfx/server-util');
7+
8+
9+
const showGraph = (callback) => {
10+
11+
blessed.Screen.global = null
12+
blessed.Program.global = null
13+
const
14+
readableStream = new Stream.Transform()
15+
screen = GFXUtil.createScreen({stream: readableStream}),
16+
grid = new contrib.grid({rows: 10, cols: 10, screen: screen})
17+
18+
let result = []
19+
20+
var donut = grid.set(0, 0, 2, 2, contrib.donut,
21+
{
22+
label: 'Percent Donut',
23+
radius: 10,
24+
arcWidth: 4,
25+
yPadding: 4,
26+
data: [{label: 'Storage', percent: 87}]
27+
})
28+
29+
30+
var map = grid.set(0, 2, 4, 4, contrib.map,
31+
{
32+
label: 'World Map'
33+
})
34+
35+
map.addMarker({"lon" : "-79.0000", "lat" : "37.5000", color: 'yellow', char: 'vgdfggdfX' })
36+
37+
/*readableStream._write = (chunk, encoding, next) => {
38+
res.send(chunk.toString())
39+
}*/
40+
41+
readableStream._transform = function (chunk,encoding,done)
42+
{
43+
result.push(chunk.toString())
44+
done()
45+
}
46+
47+
readableStream._final = () => {
48+
callback(result.join(''))
49+
}
50+
51+
screen.render()
52+
53+
setTimeout(() => {
54+
readableStream.end()
55+
}, 100)
56+
}
57+
58+
module.exports = showGraph
59+
60+
61+

0 commit comments

Comments
 (0)