-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathprogress-view.js
85 lines (65 loc) · 2.12 KB
/
progress-view.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
"use strict";
var CompactGauge = require("./compact-gauge");
module.exports = function (BaseView) {
var ProgressView = function ProgressView(options) {
BaseView.call(this, options);
this.logProvider = options.logProvider;
this.includeRegex = new RegExp(this.layoutConfig.include);
this.node = new CompactGauge({
label: " " + this.layoutConfig.title + " ",
border: "line",
style: {
border: {
fg: this.layoutConfig.borderColor
}
},
stroke: this.layoutConfig.progressColor,
fill: this.layoutConfig.textColor
});
this.recalculatePosition();
options.parent.append(this.node);
var content = options.logProvider.getLog(this.layoutConfig.streams);
if (content.length > 0) {
this._update(content);
}
this._boundUpdate = this._update.bind(this);
this.layoutConfig.streams.forEach(function (eventName) {
this.logProvider.on(eventName, this._boundUpdate);
}.bind(this));
};
ProgressView.prototype = Object.create(BaseView.prototype);
ProgressView.prototype.getDefaultLayoutConfig = function () {
return {
borderColor: "blue",
progressColor: "green",
textColor: "white",
title: "progress",
streams: ["stderr", "stdout"]
};
};
ProgressView.prototype._update = function (data) {
var lines = data.replace(/\n$/, "").split("\n");
var lastLine = null;
for (var i = lines.length - 1; i >= 0; i--) {
if (this.includeRegex.test(lines[i])) {
lastLine = lines[i];
break;
}
}
if (lastLine !== null) {
var match = lastLine.match(this.includeRegex);
var percent = Math.round(Math.max(0, Math.min(100, parseInt(match[1], 10))));
this.node.setPercent(percent);
this.parent.screen.render();
}
};
ProgressView.prototype.destroy = function () {
BaseView.prototype.destroy.call(this);
this.layoutConfig.streams.forEach(function (eventName) {
this.logProvider.removeListener(eventName, this._boundUpdate);
}.bind(this));
this._boundUpdate = null;
this.logProvider = null;
};
return ProgressView;
};