Skip to content

Commit 22ed827

Browse files
committed
Document code, make variable names more descriptive.
1 parent 7b03f19 commit 22ed827

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

lib/reporters/mini.js

+21-11
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ function MiniReporter() {
1515
this.skipCount = 0;
1616
this.rejectionCount = 0;
1717
this.exceptionCount = 0;
18-
this.lastWritten = '';
19-
this.prevLineCount = 0;
18+
this.currentStatus = '';
19+
this.statusLineCount = 0;
2020
this.stream = lastLineStream(process.stderr);
2121
}
2222

@@ -136,9 +136,9 @@ MiniReporter.prototype.finish = function () {
136136

137137
MiniReporter.prototype.write = function (str) {
138138
cliCursor.hide();
139-
this.lastWritten = str + '\n';
139+
this.currentStatus = str + '\n';
140140
this._update();
141-
this.prevLineCount = this.lastWritten.split('\n').length;
141+
this.statusLineCount = this.currentStatus.split('\n').length;
142142
};
143143

144144
MiniReporter.prototype.stdout = MiniReporter.prototype.stderr = function (data) {
@@ -147,11 +147,15 @@ MiniReporter.prototype.stdout = MiniReporter.prototype.stderr = function (data)
147147

148148
MiniReporter.prototype._update = function (data) {
149149
var str = '';
150-
var ct = this.prevLineCount;
150+
var ct = this.statusLineCount;
151151
var columns = process.stdout.columns;
152152
var lastLine = this.stream.lastLine;
153+
154+
// Terminals will automatically wrap to a new line if text exceed the column width.
155+
// We want what is (visually) the last line of output. Not everything after the last `\n` character.
153156
lastLine = lastLine.substring(lastLine.length - (lastLine.length % columns));
154157

158+
// Unless the last line of logOutput is completely empty, we need to delete it and rewrite it.
155159
if (lastLine.length) {
156160
ct++;
157161
}
@@ -161,37 +165,43 @@ MiniReporter.prototype._update = function (data) {
161165
str += lastLine;
162166

163167
if (str.length) {
168+
// `push`ing to the stream is the equivalent to a `write`, but `lastLine` is not updated.
164169
this.stream.push(str);
165170
}
166171

167172
if (data) {
168173
this.stream.write(data);
169174
}
170175

171-
var lastWritten = this.lastWritten;
172-
if (lastWritten.length) {
176+
var currentStatus = this.currentStatus;
177+
if (currentStatus.length) {
173178
lastLine = this.stream.lastLine;
179+
// We need a newline between the last line of the log output and the status message.
180+
// However, if the last line of the log output is the exact width of the terminal,
181+
// a newline is already implied, and adding a second will cause problems.
174182
if (lastLine.length % columns) {
175-
lastWritten = '\n' + lastWritten;
183+
currentStatus = '\n' + currentStatus;
176184
}
177-
this.stream.push(lastWritten);
185+
// Writes the status message, without affecting the `lastLine` status.
186+
this.stream.push(currentStatus);
178187
}
179188
};
180189

181190
// TODO(@jamestalamge): This should be fixed in log-update and ansi-escapes once we are confident it's a good solution.
182191
var CSI = '\u001b[';
183192
var ERASE_LINE = CSI + '2K';
184-
var CURSOR_LEFT = CSI + '0G';
193+
var CURSOR_TO_COLUMN_0 = CSI + '0G';
185194
var CURSOR_UP = CSI + '1A';
186195

196+
// Erases `count` lines from the end of the terminal.
187197
function eraseLines(count) {
188198
var clear = '';
189199

190200
for (var i = 0; i < count; i++) {
191201
clear += ERASE_LINE + (i < count - 1 ? CURSOR_UP : '');
192202
}
193203
if (count) {
194-
clear += CURSOR_LEFT;
204+
clear += CURSOR_TO_COLUMN_0;
195205
}
196206

197207
return clear;

0 commit comments

Comments
 (0)