-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Fix some reporter / logging issues. Fixes #392 #415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sindresorhus
merged 1 commit into
avajs:master
from
jamestalmage:fix-mini-reporter-logging
Jan 13, 2016
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
'use strict'; | ||
var logUpdate = require('log-update'); | ||
var colors = require('../colors'); | ||
var cliCursor = require('cli-cursor'); | ||
var lastLineTracker = require('last-line-stream/tracker'); | ||
var StringDecoder = require('string_decoder').StringDecoder; | ||
var plur = require('plur'); | ||
var colors = require('../colors'); | ||
var beautifyStack = require('../beautify-stack'); | ||
|
||
function MiniReporter() { | ||
|
@@ -14,7 +16,11 @@ function MiniReporter() { | |
this.skipCount = 0; | ||
this.rejectionCount = 0; | ||
this.exceptionCount = 0; | ||
this.finished = false; | ||
this.currentStatus = ''; | ||
this.statusLineCount = 0; | ||
this.lastLineTracker = lastLineTracker(); | ||
this.stream = process.stderr; | ||
this.stringDecoder = new StringDecoder(); | ||
} | ||
|
||
module.exports = MiniReporter; | ||
|
@@ -24,7 +30,7 @@ MiniReporter.prototype.start = function () { | |
}; | ||
|
||
MiniReporter.prototype.test = function (test) { | ||
var status = '\n'; | ||
var status = ''; | ||
var title; | ||
|
||
if (test.skip) { | ||
|
@@ -61,9 +67,7 @@ MiniReporter.prototype.unhandledError = function (err) { | |
}; | ||
|
||
MiniReporter.prototype.finish = function () { | ||
this.finished = true; | ||
|
||
var status = '\n'; | ||
var status = ''; | ||
|
||
if (this.passCount > 0) { | ||
status += ' ' + colors.pass(this.passCount, 'passed'); | ||
|
@@ -134,9 +138,76 @@ MiniReporter.prototype.finish = function () { | |
}; | ||
|
||
MiniReporter.prototype.write = function (str) { | ||
logUpdate.stderr(str); | ||
cliCursor.hide(); | ||
this.currentStatus = str + '\n'; | ||
this._update(); | ||
this.statusLineCount = this.currentStatus.split('\n').length; | ||
}; | ||
|
||
MiniReporter.prototype.stdout = MiniReporter.prototype.stderr = function (data) { | ||
this._update(data); | ||
}; | ||
|
||
MiniReporter.prototype._update = function (data) { | ||
var str = ''; | ||
var ct = this.statusLineCount; | ||
var columns = process.stdout.columns; | ||
var lastLine = this.lastLineTracker.lastLine(); | ||
|
||
// Terminals automatically wrap text. We only need the last log line as seen on the screen. | ||
lastLine = lastLine.substring(lastLine.length - (lastLine.length % columns)); | ||
|
||
// Don't delete the last log line if it's completely empty. | ||
if (lastLine.length) { | ||
ct++; | ||
} | ||
|
||
// Erase the existing status message, plus the last log line. | ||
str += eraseLines(ct); | ||
|
||
// Rewrite the last log line. | ||
str += lastLine; | ||
|
||
if (this.finished) { | ||
logUpdate.stderr.done(); | ||
if (str.length) { | ||
this.stream.write(str); | ||
} | ||
|
||
if (data) { | ||
// send new log data to the terminal, and update the last line status. | ||
this.lastLineTracker.update(this.stringDecoder.write(data)); | ||
this.stream.write(data); | ||
} | ||
|
||
var currentStatus = this.currentStatus; | ||
if (currentStatus.length) { | ||
lastLine = this.lastLineTracker.lastLine(); | ||
// We need a newline at the end of the last log line, before the status message. | ||
// However, if the last log line is the exact width of the terminal a newline is implied, | ||
// and adding a second will cause problems. | ||
if (lastLine.length % columns) { | ||
currentStatus = '\n' + currentStatus; | ||
} | ||
// rewrite the status message. | ||
this.stream.write(currentStatus); | ||
} | ||
}; | ||
|
||
// TODO(@jamestalamge): This should be fixed in log-update and ansi-escapes once we are confident it's a good solution. | ||
var CSI = '\u001b['; | ||
var ERASE_LINE = CSI + '2K'; | ||
var CURSOR_TO_COLUMN_0 = CSI + '0G'; | ||
var CURSOR_UP = CSI + '1A'; | ||
|
||
// Returns a string that will erase `count` lines from the end of the terminal. | ||
function eraseLines(count) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
var clear = ''; | ||
|
||
for (var i = 0; i < count; i++) { | ||
clear += ERASE_LINE + (i < count - 1 ? CURSOR_UP : ''); | ||
} | ||
if (count) { | ||
clear += CURSOR_TO_COLUMN_0; | ||
} | ||
|
||
return clear; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you comment the code in this function?