-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-helper.js
More file actions
84 lines (68 loc) · 1.75 KB
/
progress-helper.js
File metadata and controls
84 lines (68 loc) · 1.75 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
let _total = 0;
let _current = 0;
let isShowing = false;
let _clearTwoLines = false;
/* Overriding console.log */
const _oldConsoleLog = console.log;
const _newConsoleLog = (...rest) => {
if (isShowing) clearProgress();
_internalConsoleLog(...rest);
if (isShowing) printProgress();
};
const _internalConsoleLog = (...rest) => {
process.stdout.write = _oldStdOutWrite;
_oldConsoleLog(...rest);
process.stdout.write = _newStdOutWrite;
};
/* Overriding process.stdout.write */
const _oldStdOutWrite = process.stdout.write.bind(process.stdout);
const _newStdOutWrite = (rest) => {
// _internalConsoleLog('rest:', JSON.stringify(rest));
if (isShowing) clearProgress();
_oldStdOutWrite(rest);
if (rest.slice(-1) !== '\n') {
_clearTwoLines = true;
_oldStdOutWrite('\n');
}
if (isShowing) printProgress();
};
console.log = _newConsoleLog;
process.stdout.write = _newStdOutWrite
function printProgress() {
let columns = process.stdout.columns;
let message = ' ' + _current + ' / ' + _total + ' ';
columns = columns - message.length;
let multiplyer = _total / columns;
let current = _current / multiplyer;
_oldStdOutWrite('\r');
for (let i = 0; i < columns; i++) {
if (i < current) {
_oldStdOutWrite('█')
} else {
_oldStdOutWrite('░')
}
}
_oldStdOutWrite(message);
if (_current === _total) {
isShowing = false;
_oldStdOutWrite('\n');
}
}
function clearProgress() {
_oldStdOutWrite('\r');
if (_clearTwoLines) {
_oldStdOutWrite('\x1B[1A');
_clearTwoLines = false;
}
_oldStdOutWrite('\x1B[0J');
}
function setTotal(total) {
_total = total;
isShowing = true;
}
function setCurrent(current) {
_current = current + 1;
printProgress();
}
module.exports.setTotal = setTotal;
module.exports.setCurrent = setCurrent;