1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 |
|
7 | var _ansiEscapes = require('ansi-escapes');
|
8 |
|
9 | var _ansiEscapes2 = _interopRequireDefault(_ansiEscapes);
|
10 |
|
11 | var _wrapAnsi = require('wrap-ansi');
|
12 |
|
13 | var _wrapAnsi2 = _interopRequireDefault(_wrapAnsi);
|
14 |
|
15 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
16 |
|
17 |
|
18 |
|
19 | class LogUpdate {
|
20 | constructor() {
|
21 | this.prevLineCount = 0;
|
22 | this.listening = false;
|
23 | this.extraLines = '';
|
24 | this._onData = this._onData.bind(this);
|
25 | this._streams = [process.stdout, process.stderr];
|
26 | }
|
27 |
|
28 | render(lines) {
|
29 | this.listen();
|
30 |
|
31 | const wrappedLines = (0, _wrapAnsi2.default)(lines, this.columns, {
|
32 | trim: false,
|
33 | hard: true,
|
34 | wordWrap: false
|
35 | });
|
36 |
|
37 | const data = _ansiEscapes2.default.eraseLines(this.prevLineCount) + wrappedLines + '\n' + this.extraLines;
|
38 |
|
39 | this.write(data);
|
40 |
|
41 | this.prevLineCount = data.split('\n').length;
|
42 | }
|
43 |
|
44 | get columns() {
|
45 | return (process.stderr.columns || 80) - 2;
|
46 | }
|
47 |
|
48 | write(data) {
|
49 | if (process.stderr.__write) {
|
50 | process.stderr.__write(data, 'utf-8');
|
51 | } else {
|
52 | process.stderr.write(data, 'utf-8');
|
53 | }
|
54 | }
|
55 |
|
56 | clear() {
|
57 | this.done();
|
58 | this.write(_ansiEscapes2.default.eraseLines(this.prevLineCount));
|
59 | }
|
60 |
|
61 | done() {
|
62 | this.stopListen();
|
63 |
|
64 | this.prevLineCount = 0;
|
65 | this.extraLines = '';
|
66 | }
|
67 |
|
68 | _onData(data) {
|
69 | const str = String(data);
|
70 | const lines = str.split('\n').length - 1;
|
71 | if (lines > 0) {
|
72 | this.prevLineCount += lines;
|
73 | this.extraLines += data;
|
74 | }
|
75 | }
|
76 |
|
77 | listen() {
|
78 | if (this.listening) {
|
79 | return;
|
80 | }
|
81 |
|
82 | const t = this;
|
83 |
|
84 | for (const stream of this._streams) {
|
85 | if (!stream.__write) {
|
86 | stream.__write = stream.write;
|
87 | stream.write = function write(data, ...args) {
|
88 | t._onData(data);
|
89 | this.__write(data, ...args);
|
90 | };
|
91 | }
|
92 | }
|
93 |
|
94 | this.listening = true;
|
95 | }
|
96 |
|
97 | stopListen() {
|
98 | for (const stream of this._streams) {
|
99 | if (stream.__write) {
|
100 | stream.write = stream.__write;
|
101 | delete stream.__write;
|
102 | }
|
103 | }
|
104 |
|
105 | this.listening = false;
|
106 | }
|
107 | }
|
108 | exports.default = LogUpdate; |
\ | No newline at end of file |