forked from mochajs/mocha
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
261 additions
and
1 deletion.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -0,0 +1,259 @@ | ||
|
||
/** | ||
* Module dependencies. | ||
*/ | ||
|
||
var Base = require('./base') | ||
, color = Base.color; | ||
|
||
/** | ||
* Expose `Dot`. | ||
*/ | ||
|
||
exports = module.exports = NyanCat; | ||
|
||
/** | ||
* Initialize a new `Dot` matrix test reporter. | ||
* | ||
* @param {Runner} runner | ||
* @api public | ||
*/ | ||
|
||
function NyanCat(runner) { | ||
Base.call(this, runner); | ||
|
||
var self = this | ||
, stats = this.stats | ||
, width = Base.window.width * .75 | 0 | ||
, rainbowColors = this.rainbowColors = self.generateColors() | ||
, colorIndex = this.colorIndex = 0 | ||
, numerOfLines = this.numberOfLines = 4 | ||
, trajectories = this.trajectories = [[], [], [], []] | ||
, nyanCatWidth = this.nyanCatWidth = 11 | ||
, trajectoryWidthMax = this.trajectoryWidthMax = (width - nyanCatWidth) | ||
, scoreboardWidth = this.scoreboardWidth = 5 | ||
, tick = this.tick = 0 | ||
, n = 0; | ||
|
||
runner.on('start', function(){ | ||
Base.cursor.hide(); | ||
self.draw('start'); | ||
}); | ||
|
||
runner.on('pending', function(test){ | ||
self.draw('pending'); | ||
}); | ||
|
||
runner.on('pass', function(test){ | ||
self.draw('pass'); | ||
}); | ||
|
||
runner.on('fail', function(test, err){ | ||
self.draw('fail'); | ||
}); | ||
|
||
runner.on('end', function(){ | ||
Base.cursor.show(); | ||
for (var i = 0; i < self.numberOfLines; i++) write('\n'); | ||
self.epilogue(); | ||
}); | ||
} | ||
|
||
/** | ||
* Draw the nyan cat with runner `status`. | ||
* | ||
* @param {String} status | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.draw = function(status){ | ||
this.appendRainbow(); | ||
this.drawScoreboard(); | ||
this.drawRainbow(); | ||
this.drawNyanCat(status); | ||
this.tick = !this.tick; | ||
}; | ||
|
||
/** | ||
* Draw the "scoreboard" showing the number | ||
* of passes, failures and pending tests. | ||
* | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.drawScoreboard = function(){ | ||
var stats = this.stats; | ||
var colors = Base.colors; | ||
|
||
function draw(color, n) { | ||
write(' '); | ||
write('\033[' + color + 'm' + n + '\033[0m'); | ||
write('\n'); | ||
} | ||
|
||
draw(colors.green, stats.passes); | ||
draw(colors.fail, stats.failures); | ||
draw(colors.pending, stats.pending); | ||
write('\n'); | ||
|
||
this.cursorUp(this.numberOfLines); | ||
}; | ||
|
||
/** | ||
* Append the rainbow. | ||
* | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.appendRainbow = function(){ | ||
var segment = this.tick ? '_' : '-'; | ||
var rainbowified = this.rainbowify(segment); | ||
|
||
for (var index = 0; index < this.numberOfLines; index++) { | ||
var trajectory = this.trajectories[index]; | ||
if (trajectory.length >= this.trajectoryWidthMax) trajectory.shift(); | ||
trajectory.push(rainbowified); | ||
} | ||
}; | ||
|
||
/** | ||
* Draw the rainbow. | ||
* | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.drawRainbow = function(){ | ||
var self = this; | ||
|
||
this.trajectories.forEach(function(line, index) { | ||
write('\033[' + self.scoreboardWidth + 'C'); | ||
write(line.join('')); | ||
write('\n'); | ||
}); | ||
|
||
this.cursorUp(this.numberOfLines); | ||
}; | ||
|
||
/** | ||
* Draw the nyan cat with `status`. | ||
* | ||
* @param {String} status | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.drawNyanCat = function(status) { | ||
var self = this; | ||
var startWidth = this.scoreboardWidth + this.trajectories[0].length; | ||
|
||
[0, 1, 2, 3].forEach(function(index) { | ||
write('\033[' + startWidth + 'C'); | ||
|
||
switch (index) { | ||
case 0: | ||
write('_,------,'); | ||
write('\n'); | ||
break; | ||
case 1: | ||
var padding = self.tick ? ' ' : ' '; | ||
write('_|' + padding + '/\\_/\\'); | ||
write('\n'); | ||
break; | ||
case 2: | ||
var padding = self.tick ? '_' : '__'; | ||
var tail = self.tick ? '~' : '^'; | ||
var face; | ||
switch (status) { | ||
case 'pass': | ||
face = '( ^ .^)'; | ||
break; | ||
case 'fail': | ||
face = '( o .o)'; | ||
break; | ||
default: | ||
face = '( - .-)'; | ||
} | ||
write(tail + '|' + padding + face); | ||
write('\n'); | ||
break; | ||
case 3: | ||
var padding = self.tick ? ' ' : ' '; | ||
write(padding + '"" ""'); | ||
write('\n'); | ||
break; | ||
} | ||
}); | ||
|
||
this.cursorUp(this.numberOfLines); | ||
}; | ||
|
||
/** | ||
* Move cursor up `n`. | ||
* | ||
* @param {Number} n | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.cursorUp = function(n) { | ||
write('\033[' + n + 'A'); | ||
}; | ||
|
||
/** | ||
* Move cursor down `n`. | ||
* | ||
* @param {Number} n | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.cursorDown = function(n) { | ||
write('\033[' + n + 'B'); | ||
}; | ||
|
||
/** | ||
* Generate rainbow colors. | ||
* | ||
* @return {Array} | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.generateColors = function(){ | ||
var colors = []; | ||
|
||
for (var i = 0; i < (6 * 7); i++) { | ||
var pi3 = Math.floor(Math.PI / 3); | ||
var n = (i * (1.0 / 6)); | ||
var r = Math.floor(3 * Math.sin(n) + 3); | ||
var g = Math.floor(3 * Math.sin(n + 2 * pi3) + 3); | ||
var b = Math.floor(3 * Math.sin(n + 4 * pi3) + 3); | ||
colors.push(36 * r + 6 * g + b + 16); | ||
} | ||
|
||
return colors; | ||
}; | ||
|
||
/** | ||
* Apply rainbow to the given `str`. | ||
* | ||
* @param {String} str | ||
* @return {String} | ||
* @api private | ||
*/ | ||
|
||
NyanCat.prototype.rainbowify = function(str){ | ||
var color = this.rainbowColors[this.colorIndex % this.rainbowColors.length]; | ||
this.colorIndex += 1; | ||
return '\033[38;5;' + color + 'm' + str + '\033[0m'; | ||
}; | ||
|
||
/** | ||
* Stdout helper. | ||
*/ | ||
|
||
function write(string) { | ||
process.stdout.write(string); | ||
} | ||
|
||
/** | ||
* Inherit from `Base.prototype`. | ||
*/ | ||
|
||
NyanCat.prototype.__proto__ = Base.prototype; |
This file contains 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 |
---|---|---|
|
@@ -4223,4 +4223,4 @@ window.mocha = require('mocha'); | |
return runner.run(fn); | ||
}; | ||
})(); | ||
})(); | ||
})(); |