Skip to content

Commit

Permalink
factored out name updating
Browse files Browse the repository at this point in the history
  • Loading branch information
James Halliday committed Dec 26, 2013
1 parent 18daeba commit b86b7da
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 21 deletions.
44 changes: 24 additions & 20 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ var through2 = require('through2');
var duplexer = require('duplexer');
var parser = require('tap-parser');
var sprintf = require('sprintf');
var strip = require('strip-ansi');

module.exports = function (opts) {
if (!opts) opts = {};
Expand All @@ -13,24 +14,19 @@ module.exports = function (opts) {
var prevWidth = 0;
return function (str) {
if (opts.width) str = str.slice(0, opts.width);
var n = prevWidth - str.length;
var len = strip(str).length;
var n = prevWidth - len;
if (n < 0) n = 0;
var s = str + Array(n + 1).join(' ');
prevWidth = str.length;
prevWidth = len;
return '\r' + s;
};
})();

tap.on('comment', function (comment) {
if (test && test.ok) {
var y = test.offset + 1;
out.push(
'\r\x1b[' + y + 'A'
+ '\x1b[1m\x1b[32m'
+ '✓ ' + test.name
+ '\x1b[0m'
+ '\x1b[' + y + 'B'
);
var s = updateName(test.offset + 1, '✓ ' + test.name, 32);
out.push('\r' + s);
}

test = {
Expand All @@ -44,28 +40,27 @@ module.exports = function (opts) {

tap.on('assert', function (res) {
var ok = res.ok ? 'ok' : 'not ok';
var fmt = '%4d. %s %s';
var str = replaceLine(sprintf(fmt, res.number, ok, res.name));
var c = res.ok ? 32 : 31;
var fmt = ' %s \x1b[1m\x1b[' + c + 'm%d\x1b[0m %s';
var str = replaceLine(sprintf(fmt, ok, res.number, res.name));

if (!res.ok) {
var y = (++ test.offset) + 1;
str += '\n';
if (test.ok) {
str += '\x1b[' + y + 'A'
+ '\x1b[1m\x1b[31m'
+ '⨯ ' + test.name
+ '\x1b[0m'
+ '\x1b[' + y + 'B'
;
str += updateName(y, '⨯ ' + test.name, 31)
}
test.ok = false;
}
out.push(str);
test.assertions.push(res);
});

tap.on('result', function (res) {
//
tap.on('results', function (res) {
if (/^fail\s/.test(test.name)) {

console.log(test.name);
}
});

return duplexer(tap, out);
Expand All @@ -74,3 +69,12 @@ module.exports = function (opts) {
out.push('\r');
}
};

function updateName (y, str, c) {
return '\x1b[' + y + 'A'
+ '\x1b[1m\x1b[' + c + 'm'
+ str
+ '\x1b[0m'
+ '\x1b[' + y + 'B'
;
}
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"dependencies": {
"through2": "~0.2.3",
"tap-parser": "~0.3.0",
"duplexer": "~0.1.1"
"duplexer": "~0.1.1",
"strip-ansi": "~0.1.1"
},
"devDependencies": {
"tape": "~2.3.2"
Expand Down

0 comments on commit b86b7da

Please sign in to comment.