forked from dtex/barcli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
225 lines (170 loc) · 5.46 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
// var chalk = require("chalk");
const ctx = require("chalk");
const chalk = new ctx.constructor({level: 3});
var barclis = [];
var currentPosition = 0;
var colors = ["indianred", "palegreen", "gold", "skyblue", "orchid", "lightcyan", "white"];
var maxLabelLength = 0;
var maxValueLength = 0;
var windowWidth = process.stdout.columns || 80;
// fmap() and constrain() are lifted from Rick Waldron's awesome
// Johnny-Five library https://github.com/rwaldron/johnnny-five
var fmap = function(value, fromLow, fromHigh, toLow, toHigh) {
return (value - fromLow) * (toHigh - toLow) /
(fromHigh - fromLow) + toLow;
};
var constrain = function(value, lower, upper) {
return Math.min(upper, Math.max(lower, value));
};
function Barcli(opts) {
if (!(this instanceof Barcli)) {
return new Barcli(opts);
}
if (typeof opts === "undefined") {
opts = {};
}
if (!opts.range && opts.inputRange) {
opts.range = opts.inputRange;
}
this.index = barclis.length;
this.autoRange = opts.autoRange || false;
this.inputRange = opts.range || [null, null];
this.color = opts.color || colors[this.index % colors.length];
this.label = opts.label || "Input " + String(this.index + 1);
this.percent = opts.percent || false;
this.constrain = !!opts.constrain || false;
this.precision = opts.precision || 0;
this.inline = opts.inline || false;
this.width = opts.width || windowWidth;
if (!opts.range || opts.range[0] === null || opts.range[1] === null) {
this.autoRange = true;
}
// So we can avoid wrapping
if (this.precision > maxValueLength) {
maxValueLength = this.precision;
}
// So we can left align all the graphs
if (this.label.length > maxLabelLength) {
maxLabelLength = this.label.length;
}
if (barclis.length === 0 && !this.inline){
clearScreen();
}
barclis.push(this);
resize(this.width);
}
Barcli.halt = function() {
barclis.forEach(function(barcli) {
barcli.finish();
});
}
Barcli.prototype.finish = function() {
process.stdout.write("\n");
};
Barcli.prototype.update = function(data) {
var prepend = append = bar = postbar = "";
this.data = data;
if (Array.isArray(data)) {
data = data[0];
}
var type = typeof data;
if (String(data).length > maxValueLength) {
maxValueLength = String(data).length;
resize(this.width);
}
var raw = data;
if (type === "number") {
if (this.autoRange) {
if (isNaN(this.inputRange[0]) || data < this.inputRange[0]) {
this.inputRange[0] = data;
}
if (isNaN(this.inputRange[1]) || data > this.inputRange[1]) {
this.inputRange[1] = data;
}
}
// Map and constrain the input values
data = fmap(data, this.inputRange[0], this.inputRange[1], 0, this.width);
data = constrain(data, 0, this.width);
// Make our "bar"
for (i = 0; i <= data; i++) {
bar = bar + "\u2588";
}
// Make the space after our bar
for (i = data; i < this.width; i++) {
postbar += " ";
}
}
if (type === "string") {
bar = data;
for (i = bar.length; i <= this.width; i++) {
postbar += " ";
}
}
// Hide the cursor
process.stdout.write("\033[?25l");
// Put cursor on the correct line and clear right
if (this.inline) {
process.stdout.write("\r");
} else {
process.stdout.write("\033["+String(this.index + 1)+";0H\033[K");
}
// Output the label
process.stdout.write(chalk.keyword([this.color])(this.label+": "));
process.stdout.write(chalk.keyword('white')("|"));
// Ouput the bar
if (type === "number") {
process.stdout.write(chalk.keyword([this.color])(bar));
} else {
process.stdout.write(chalk.keyword([this.color])(bar));
}
process.stdout.write(chalk.keyword([this.color])(postbar));
process.stdout.write(chalk.keyword('white')("| "));
if (type === "number") {
// Output the raw data value in red if outside the range
var color = (raw >= this.inputRange[0] && raw <= this.inputRange[1]) ? "white" : "red";
if (raw > this.inputRange[1]) {
prepend = "> ";
}
if (raw < this.inputRange[0]) {
prepend = "< ";
}
if (this.constrain) {
raw = constrain(raw, this.inputRange[0], this.inputRange[1]);
}
if (this.percent) {
raw = fmap(raw, this.inputRange[0], this.inputRange[1], 0, 100);
append = "%";
}
if (raw === null) {
process.stdout.write(chalk.red(prepend + "null" + append));
} else {
process.stdout.write(chalk.keyword([color])(prepend + String(raw.toFixed(this.precision)) + append));
}
}
// Move the cursor to its previous position and make it visible again
// process.stdout.write("\033["+position.row+";"+position.column+"H\033[?25h");
if (!this.inline) process.stdout.write("\033[?25h");
};
Barcli.prototype.set = Barcli.prototype.update;
var clearScreen = function() {
// Clear the console and hide the cursor
process.stdout.write("\033[2J");
// Redraw on resize event
process.stdout.on('resize', function() {
resize();
});
}
var resize = function() {
windowWidth = process.stdout.columns || 80;
// Update our labels so the formatting is consistent
barclis.forEach(function(barcli, index) {
// Make sure the labels are laft padded with spaces
while (barcli.label.length < maxLabelLength) {
barcli.label = " " + barcli.label;
}
barcli.width = windowWidth - maxLabelLength - maxValueLength - 10;
barcli.update(barcli.data);
barcli.update(barcli.data);
});
};
module.exports = Barcli;