-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisplay.js
63 lines (55 loc) · 1.69 KB
/
display.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
/*
* DISPLAY
*
* We display timeseries horizontally using box chars
*
// Example mood ts for the last 14 days
const moodTS = [6, 7, 8, 2, 5, 5, 7, 8, 6, 5, 9, 9, 9, 8];
*/
import chalk from 'chalk';
// values from 1-9
const BOXEN = ['_', '▁', '▂', '▃', '▄', '▅', '▆', '▇', '█'];
const displayValue = (value) => {
if (value === null) {
return { character: 'x', color: 'grey' };
}
const percentage = (value / 9) * 100;
const charIndex = Math.round((percentage / 100) * (BOXEN.length - 1));
const character = BOXEN[charIndex];
// color-coding
let color = 'green'; // Default to green
if (percentage < 40) {
color = 'red'; // Less than 40% is red
} else if (percentage < 65) {
color = 'yellow'; // Less than 65% is yellow
}
return { character, color };
};
// Function to display the horizontal time series
const displayTS = (ts) => {
// for each data point
ts.forEach((value, index) => {
// select box
const { character, color } = displayValue(value);
process.stdout.write(chalk[color](character));
});
if (ts[ts.length - 1]) {
let compareEntry;
for (let ix = ts.length - 2; ix >= 0; ix--) {
if (ts[ix]) {
compareEntry = ts[ix];
break;
}
}
if (compareEntry) {
const diff = ts[ts.length - 1] - compareEntry;
let color = (diff == 0) ? 'white' : ((diff < 0) ? 'yellow' : 'green');
process.stdout.write(` ${chalk[color](diff)}`);
}
}
console.log(); // Move to the next line after displaying the time series
}
export {
displayValue,
displayTS,
};