This repository was archived by the owner on Jul 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·195 lines (177 loc) · 4.93 KB
/
index.js
File metadata and controls
executable file
·195 lines (177 loc) · 4.93 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
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
var repeating = require('repeating');
var dateutil = require('dateutil');
var chalk = require('chalk');
var through = require('through');
var termwidth = require('window-size').width;
var newline = require('os').EOL;
var ansi = {
prefix: '\x1b[',
up: 'A',
clearLine: '0G'
};
var nanoPow = Math.pow(10,9);
function durationToSeconds(dur) {
return dur[0] + dur[1] / nanoPow;
}
var space = ' ';
var sAbbr = 's';
var places = 4;
var maxDisplaySecondsDigits = 4;
function formatDuration(dur) {
return durationToSeconds(dur).toFixed(places) + sAbbr;
}
var start = process.hrtime();
var elapsedLine = [0,0];
var elapsedTotal = [0,0];
var lap = start;
var last = start;
function tick(resetLine) {
var now = process.hrtime();
if (resetLine) {
lap = now;
elapsedLine = process.hrtime(last);
} else {
elapsedLine = process.hrtime(lap);
}
elapsedTotal = process.hrtime(start);
last = process.hrtime();
}
var nullString = '';
function padFor(s, max) {
var l = s.length;
return l < max ? repeating(space, max - l) : nullString;
}
var bar = space + chalk.reset.inverse(' ') + space;
var totalLabel = 'Total';
module.exports = function(opts) {
opts = opts || {};
var fmt = opts.format || 'H:i:s.u O';
var type = opts.type || 'elapsed-line';
var stampers = {
'elapsed-line': function() {
return formatDuration(elapsedLine);
},
'elapsed-total': function() {
return formatDuration(elapsedTotal);
},
'absolute': function() {
return dateutil.format(new Date(), fmt);
}
};
var maxDurLength, blank, maxLineLength;
if (type === 'absolute') {
maxDurLength = stampers.absolute().length;
} else {
maxDurLength = maxDisplaySecondsDigits + places + 2; // dot and 's'
}
maxDurLength = Math.max(maxDurLength, totalLabel.length);
blank = repeating(space, maxDurLength) + bar;
maxLineLength = termwidth - chalk.stripColor(blank).length;
function stampLine(stamp, line) {
var len = line ? chalk.stripColor(line).length : 0;
if (len > maxLineLength) {
return stamp + line.slice(0, maxLineLength) + newline +
stampLine(blank, line.slice(maxLineLength));
}
return stamp + line + newline;
}
var colorStamp;
var high = opts.high;
var medium = opts.medium;
if (medium && high) {
colorStamp = function(stamp) {
var seconds = durationToSeconds(elapsedLine);
if (seconds >= high) {
return chalk.reset.red(stamp);
}
if (seconds >= medium) {
return chalk.reset.yellow(stamp);
}
return chalk.reset.green(stamp);
};
} else if (medium) {
colorStamp = function(stamp) {
if (durationToSeconds(elapsedLine) >= medium) {
return chalk.reset.yellow(stamp);
}
return chalk.reset.green(stamp);
}
} else if (high) {
colorStamp = function(stamp) {
if (durationToSeconds(elapsedLine) >= high) {
return chalk.reset.red(stamp);
}
return chalk.reset.green(stamp);
}
} else {
colorStamp = function(stamp) {
return chalk.reset(stamp);
}
}
var createStamp = stampers[type];
function createFormattedStamp(text, value) {
var text = createStamp();
return padFor(text, maxDurLength) + colorStamp(text, value) + bar;
}
var lastLine = false;
var overwrite;
var autoUpdate;
function scheduleAutoUpdate(stream) {
autoUpdate = setTimeout(function() {
tick(false);
stream.queue(
overwrite + stampLine(createFormattedStamp(), lastLine)
);
scheduleAutoUpdate(stream);
}, opts.realTime);
}
function setLastLine(line) {
lastLine = line;
overwrite = ansi.prefix + (~~(lastLine.length / maxLineLength) + 1) +
ansi.up + ansi.prefix + ansi.clearLine;
}
var feed;
if (opts.realTime) {
feed = function(stream, line, last) {
feed = function(stream, line, last) {
tick(false);
stream.queue(
overwrite + stampLine(createFormattedStamp(), lastLine)
);
tick(true);
if (autoUpdate) clearTimeout(autoUpdate);
scheduleAutoUpdate(stream);
setLastLine(line);
if (!last) stream.queue(stampLine(blank, line));
};
stream.queue(stampLine(blank, line));
setLastLine(line);
scheduleAutoUpdate(stream);
}
} else {
feed = function(stream, line, last) {
feed = function(stream, line, last) {
tick(true);
if (!last)
stream.queue(stampLine(createFormattedStamp(), lastLine));
lastLine = line;
};
lastLine = line;
}
}
var onData;
if (opts.ignoreBlank) {
onData = function(line) { if (line) feed(this, line); }
} else {
onData = function(line) { feed(this, line); }
}
return through(onData, function end () {
feed(this, '', true);
this.queue(
stampLine(blank, '') +
padFor(totalLabel, maxDurLength) + totalLabel + bar +
formatDuration(elapsedTotal) + newline
);
if (autoUpdate) clearTimeout(autoUpdate);
});
};