-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
331 lines (281 loc) · 8.99 KB
/
index.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
const blessed = require('blessed');
const contrib = require('blessed-contrib');
const moment = require('moment');
const colors = require('colors/safe');
const approx = require('approximate-number');
const figures = require('figures');
const getRanking = require('./utils/coinRanking');
const getCoinHistory = require('./utils/coinHistory');
// Global Vars
const limit = 10;
var totalCoins = 0;
var currentCoinId = -1;
var currentCoinName = "";
var offset = 0;
var currentPage = 0;
var periodIndex = 0;
var currencyIndex = 0;
var currencySymbol = "";
var realTimeRefresh = 60000; // 1 min
const period = ['Real-Time', '24h', '7d', '30d', '1y'];
const currency = ['USD', 'EUR'];
module.exports = (args) => {
// Create screen
var screen = createScreen();
// Create grid layout
var grid = createGridLayout(screen);
// Create price log
var log = createLog(grid, []);
// Create coin graph
var graph = createGraph(grid);
// Create ranking table
var table = createTable(grid, screen);
// Set keys
setKeys(grid, screen);
// Render the screen.
screen.render();
}
function createScreen(){
var screen = blessed.screen({
smartCSR: true,
useBCE: true,
cursor: {
artificial: true,
blink: true,
shape: 'underline'
},
debug: true,
dockBorders: true
});
screen.title = 'Crypto Stats';
return screen;
};
function createGridLayout(screen){
var grid = new contrib.grid({rows: 7, cols: 5, screen: screen})
return grid;
}
async function createGraph(grid){
var minPrice = 0;
var historyPrice = [];
var historyDate = [];
var graph = 0;
var realTimeDataLimit = 50;
if(currentCoinId > 0){
var periodAux = period[periodIndex];
if(periodIndex == 0){
periodAux = period[1];
}
const coinHistory = await getCoinHistory(currentCoinId, periodAux, currency[currencyIndex]);
var coinHistoryData = [];
if(periodIndex == 0){
coinHistoryData = coinHistory.data.history.slice(0,realTimeDataLimit);
}else{
coinHistoryData = coinHistory.data.history;
}
createLog(grid, coinHistoryData);
historyPrice = new Array(coinHistoryData.length);
historyDate = new Array(coinHistoryData.length);
for (var i = 0; i < historyPrice.length; i++) {
historyPrice[i] = parseFloat(coinHistoryData[i].price);
if(periodIndex == 0 || periodIndex == 1){
historyDate[i] = moment(new Date(coinHistoryData[i].timestamp)).format("h:mm a");
}else if(periodIndex == 2 || periodIndex == 3){
historyDate[i] = moment(new Date(coinHistoryData[i].timestamp)).format("D MMM");
}else{
historyDate[i] = moment(new Date(coinHistoryData[i].timestamp)).format("MMM Y");
}
}
var minPrice = Math.min.apply(Math, historyPrice);
graph = grid.set(0, 0, 4, 4, contrib.line,
{ style:
{ line: "green"
, text: "white"
, baseline: "green"}
, xLabelPadding: 10
, xPadding: 1
, minY: minPrice
, abbreviate: true
, showLegend: false
, wholeNumbersOnly: false
, label: ' Coin Price History (' + period[periodIndex] + ') (' + currency[currencyIndex] + ') - ' + colors.green(currentCoinName) + colors.yellow(' [p] Period [c] Currency [esc] Exit ')
});
var data = {
title: 'Price (' + period[periodIndex] + ')',
x: historyDate,
y: historyPrice
};
graph.setData([data]);
}else{
var graph = grid.set(0, 0, 4, 4, contrib.line,
{ label: ' Coin Price History (' + period[periodIndex] + ') (' + currency[currencyIndex] + ')' + colors.yellow(' [c] Currency [esc] Exit ')
});
}
return graph;
}
function createLog(grid, coinHistoryData){
var title = ""
if(periodIndex == 0){
title = ' Real-time prices (' + currency[currencyIndex] + ') ';
}else{
title = ' Last graph prices (' + currency[currencyIndex] + ') ';
}
var log = grid.set(0, 4, 4, 1, contrib.log,
{ fg: "white"
, selectedFg: "green"
, label: title});
if(coinHistoryData.length > 0){
var num = 30;
for (var i = coinHistoryData.length - num; i < coinHistoryData.length; i++) {
if(coinHistoryData[i].price > coinHistoryData[i - 1].price){
var arrow = " " + colors.blue(figures.arrowUp) + " ";
}else{
var arrow = " " + colors.red(figures.arrowDown) + " ";
}
if(periodIndex == 0){
log.log(moment(coinHistoryData[i].timestamp).format("h:mm") + arrow + currencySymbol + " " + coinHistoryData[i].price);
}else{
log.log(moment(coinHistoryData[i].timestamp).format('D/M/Y h:mm') + arrow + currencySymbol + " " + approx(coinHistoryData[i].price));
}
}
}
return log;
}
async function createTable(grid, screen){
const ranking = await getRanking(limit, offset, currency[currencyIndex])
var coinList = new Array(ranking.data.coins.length);
totalCoins = ranking.data.stats.total;
var coinNameMaxLength = 20;
var table = grid.set(4, 0, 3, 5, contrib.table,
{ keys: true
, fg: 'white'
, selectedFg: 'black'
, selectedBg: 'green'
, interactive: true
, align: 'left'
, label: ' Ranking by Market Cap (Page ' + (currentPage + 1) + ' of ' + Math.round(totalCoins/limit) + ') (' + currency[currencyIndex] + ') ' + colors.yellow('[' + figures.arrowUp + '] Up [' + figures.arrowDown + '] Down [enter] Graph [+] Next page [-] Previous page ')
, width: '100%'
, height: '100%'
, columnSpacing: 2
, columnWidth: [5, coinNameMaxLength, 10, 20, 15, 15, 20] });
table.focus();
currencySymbol = ranking.data.base.sign;
for (var i = 0; i < coinList.length; i++) {
var coin = ranking.data.coins[i];
var coinInfo = [];
if(coin.rank == null){
coinInfo.push("n/a")
}else{
coinInfo.push(coin.rank);
}
if(coin.name == null){
coinInfo.push("n/a")
}else{
if(coin.name.length > coinNameMaxLength){
coinInfo.push(coin.name.slice(0, coinNameMaxLength - 3) + "...");
}else{
coinInfo.push(coin.name);
}
}
if(coin.symbol == null){
coinInfo.push("n/a")
}else{
coinInfo.push(coin.symbol);
}
if(coin.price == null){
coinInfo.push("n/a")
}else{
coinInfo.push(currencySymbol + " " + coin.price);
}
if(coin.marketCap == null){
coinInfo.push("n/a")
}else{
coinInfo.push(currencySymbol + " " + approx(coin.marketCap));
}
if(coin.change == null){
coinInfo.push("n/a")
}else{
if(coin.change > 0){
coinInfo.push(colors.blue("+" + coin.change + "%"));
}else{
coinInfo.push(colors.red(coin.change + "%"));
}
}
if(coin.allTimeHigh == null){
coinInfo.push("n/a")
}else{
coinInfo.push(currencySymbol + " " + coin.allTimeHigh.price);
}
coinList[i] = coinInfo;
}
table.rows.on('select', (item, index) => {
selectedCoinId = ranking.data.coins[index].id;
if(currentCoinId != selectedCoinId){
currentCoinId = selectedCoinId;
currentCoinName = ranking.data.coins[index].name;
createGraph(grid);
screen.render();
if(periodIndex == 0){
setInterval(function() {
createGraph(grid);
screen.render();
}, realTimeRefresh);
}
}
});
table.setData(
{ headers: [colors.green('Rank'), colors.green('Name'), colors.green('Symbol'), colors.green('Price'), colors.green('Market Cap'), colors.green('24h Change'), colors.green('All Time High')]
, data: coinList
});
return table;
}
function setKeys(grid, screen){
// Exit CLI
screen.key(['escape', 'C-c'], function(ch, key) {
return process.exit(0);
});
// Change period
screen.key(['p'], function(ch, key) {
if(periodIndex == (period.length - 1)){
periodIndex = 0;
}else{
periodIndex++;
}
createGraph(grid);
screen.render();
if(periodIndex == 0){
setInterval(function() {
createGraph(grid);
screen.render();
}, realTimeRefresh);
}
});
// Change currency
screen.key(['c'], function(ch, key) {
if(currencyIndex == (currency.length - 1)){
currencyIndex = 0;
}else{
currencyIndex++;
}
createTable(grid, screen);
createGraph(grid);
screen.render();
});
// Next page
screen.key(['+'], function(ch, key) {
if(currentPage < (totalCoins/limit) - 1){
offset += limit;
currentPage++;
createTable(grid, screen);
screen.render();
}
});
// Previous page
screen.key(['-'], function(ch, key) {
if(currentPage > 0){
offset -= limit;
currentPage--;
createTable(grid, screen);
screen.render();
}
});
};