forked from ionic-team/ionic-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmultibar.js
More file actions
103 lines (88 loc) · 2.14 KB
/
multibar.js
File metadata and controls
103 lines (88 loc) · 2.14 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
var ProgressBar = require('progress');
function Multibar(stream) {
this.stream = stream || process.stderr;
this.cursor = 0;
this.bars = [];
this.terminates = 0;
}
Multibar.prototype = {
newBar: function(schema, options) {
options.stream = this.stream;
var bar = new ProgressBar(schema, options);
this.bars.push(bar);
var index = this.bars.length - 1;
// alloc line
this.move(index);
this.stream.write('\n');
this.cursor ++;
// replace original
var self = this;
bar.otick = bar.tick;
bar.oterminate = bar.terminate;
bar.tick = function(value, options) {
self.tick(index, value, options);
}
bar.terminate = function() {
self.terminates++;
if (self.terminates == self.bars.length) {
self.terminate();
}
}
return bar;
},
terminate: function() {
this.move(this.bars.length);
this.stream.clearLine();
this.stream.cursorTo(0);
},
move: function(index) {
if (!this.stream.isTTY) return;
this.stream.moveCursor(0, index - this.cursor);
this.cursor = index;
},
tick: function(index, value, options) {
var bar = this.bars[index];
if (bar) {
this.move(index);
bar.otick(value, options);
}
}
}
// var mbars = new Multibar();
// var bars = [];
// function addBar() {
// bars.push(mbars.newBar(' :title [:bar] :percent', {
// complete: '='
// , incomplete: ' '
// , width: 30
// , total: 100
// }));
// return bars[bars.length - 1];
// }
// function forward() {
// for (var i = 0; i < bars.length; i++) {
// bars[i].tick(1, { title: 'forward: ' });
// }
// if (bars[0].curr > 60) {
// addBar().tick(bars[0].curr);
// backward();
// } else {
// setTimeout(forward, 20);
// }
// }
// function backward() {
// for (var i = 0; i < bars.length; i++) {
// bars[i].tick(-1, { title: 'backward: ' });
// }
// if (bars[0].curr == 0) {
// mbars.terminate();
// console.log("End all");
// } else {
// setTimeout(backward, 20);
// }
// }
// for(var i = 0; i < 5; i++) {
// addBar();
// }
// forward();
module.exports = new Multibar();