123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276 |
- 'use strict';
- /**
- * @module Nyan
- */
- /**
- * Module dependencies.
- */
- var Base = require('./base');
- var constants = require('../runner').constants;
- var inherits = require('../utils').inherits;
- var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
- var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
- var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
- var EVENT_RUN_END = constants.EVENT_RUN_END;
- var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
- /**
- * Expose `Dot`.
- */
- exports = module.exports = NyanCat;
- /**
- * Initialize a new `Dot` matrix test reporter.
- *
- * @param {Runner} runner
- * @public
- * @class Nyan
- * @memberof Mocha.reporters
- * @extends Mocha.reporters.Base
- */
- function NyanCat(runner) {
- Base.call(this, runner);
- var self = this;
- var width = (Base.window.width * 0.75) | 0;
- var nyanCatWidth = (this.nyanCatWidth = 11);
- this.colorIndex = 0;
- this.numberOfLines = 4;
- this.rainbowColors = self.generateColors();
- this.scoreboardWidth = 5;
- this.tick = 0;
- this.trajectories = [[], [], [], []];
- this.trajectoryWidthMax = width - nyanCatWidth;
- runner.on(EVENT_RUN_BEGIN, function() {
- Base.cursor.hide();
- self.draw();
- });
- runner.on(EVENT_TEST_PENDING, function() {
- self.draw();
- });
- runner.on(EVENT_TEST_PASS, function() {
- self.draw();
- });
- runner.on(EVENT_TEST_FAIL, function() {
- self.draw();
- });
- runner.once(EVENT_RUN_END, function() {
- Base.cursor.show();
- for (var i = 0; i < self.numberOfLines; i++) {
- write('\n');
- }
- self.epilogue();
- });
- }
- /**
- * Inherit from `Base.prototype`.
- */
- inherits(NyanCat, Base);
- /**
- * Draw the nyan cat
- *
- * @private
- */
- NyanCat.prototype.draw = function() {
- this.appendRainbow();
- this.drawScoreboard();
- this.drawRainbow();
- this.drawNyanCat();
- this.tick = !this.tick;
- };
- /**
- * Draw the "scoreboard" showing the number
- * of passes, failures and pending tests.
- *
- * @private
- */
- NyanCat.prototype.drawScoreboard = function() {
- var stats = this.stats;
- function draw(type, n) {
- write(' ');
- write(Base.color(type, n));
- write('\n');
- }
- draw('green', stats.passes);
- draw('fail', stats.failures);
- draw('pending', stats.pending);
- write('\n');
- this.cursorUp(this.numberOfLines);
- };
- /**
- * Append the rainbow.
- *
- * @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.
- *
- * @private
- */
- NyanCat.prototype.drawRainbow = function() {
- var self = this;
- this.trajectories.forEach(function(line) {
- write('\u001b[' + self.scoreboardWidth + 'C');
- write(line.join(''));
- write('\n');
- });
- this.cursorUp(this.numberOfLines);
- };
- /**
- * Draw the nyan cat
- *
- * @private
- */
- NyanCat.prototype.drawNyanCat = function() {
- var self = this;
- var startWidth = this.scoreboardWidth + this.trajectories[0].length;
- var dist = '\u001b[' + startWidth + 'C';
- var padding = '';
- write(dist);
- write('_,------,');
- write('\n');
- write(dist);
- padding = self.tick ? ' ' : ' ';
- write('_|' + padding + '/\\_/\\ ');
- write('\n');
- write(dist);
- padding = self.tick ? '_' : '__';
- var tail = self.tick ? '~' : '^';
- write(tail + '|' + padding + this.face() + ' ');
- write('\n');
- write(dist);
- padding = self.tick ? ' ' : ' ';
- write(padding + '"" "" ');
- write('\n');
- this.cursorUp(this.numberOfLines);
- };
- /**
- * Draw nyan cat face.
- *
- * @private
- * @return {string}
- */
- NyanCat.prototype.face = function() {
- var stats = this.stats;
- if (stats.failures) {
- return '( x .x)';
- } else if (stats.pending) {
- return '( o .o)';
- } else if (stats.passes) {
- return '( ^ .^)';
- }
- return '( - .-)';
- };
- /**
- * Move cursor up `n`.
- *
- * @private
- * @param {number} n
- */
- NyanCat.prototype.cursorUp = function(n) {
- write('\u001b[' + n + 'A');
- };
- /**
- * Move cursor down `n`.
- *
- * @private
- * @param {number} n
- */
- NyanCat.prototype.cursorDown = function(n) {
- write('\u001b[' + n + 'B');
- };
- /**
- * Generate rainbow colors.
- *
- * @private
- * @return {Array}
- */
- 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`.
- *
- * @private
- * @param {string} str
- * @return {string}
- */
- NyanCat.prototype.rainbowify = function(str) {
- if (!Base.useColors) {
- return str;
- }
- var color = this.rainbowColors[this.colorIndex % this.rainbowColors.length];
- this.colorIndex += 1;
- return '\u001b[38;5;' + color + 'm' + str + '\u001b[0m';
- };
- /**
- * Stdout helper.
- *
- * @param {string} string A message to write to stdout.
- */
- function write(string) {
- process.stdout.write(string);
- }
- NyanCat.description = '"nyan cat"';
|