nyan.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. 'use strict';
  2. /**
  3. * @module Nyan
  4. */
  5. /**
  6. * Module dependencies.
  7. */
  8. var Base = require('./base');
  9. var constants = require('../runner').constants;
  10. var inherits = require('../utils').inherits;
  11. var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
  12. var EVENT_TEST_PENDING = constants.EVENT_TEST_PENDING;
  13. var EVENT_TEST_PASS = constants.EVENT_TEST_PASS;
  14. var EVENT_RUN_END = constants.EVENT_RUN_END;
  15. var EVENT_TEST_FAIL = constants.EVENT_TEST_FAIL;
  16. /**
  17. * Expose `Dot`.
  18. */
  19. exports = module.exports = NyanCat;
  20. /**
  21. * Initialize a new `Dot` matrix test reporter.
  22. *
  23. * @param {Runner} runner
  24. * @public
  25. * @class Nyan
  26. * @memberof Mocha.reporters
  27. * @extends Mocha.reporters.Base
  28. */
  29. function NyanCat(runner) {
  30. Base.call(this, runner);
  31. var self = this;
  32. var width = (Base.window.width * 0.75) | 0;
  33. var nyanCatWidth = (this.nyanCatWidth = 11);
  34. this.colorIndex = 0;
  35. this.numberOfLines = 4;
  36. this.rainbowColors = self.generateColors();
  37. this.scoreboardWidth = 5;
  38. this.tick = 0;
  39. this.trajectories = [[], [], [], []];
  40. this.trajectoryWidthMax = width - nyanCatWidth;
  41. runner.on(EVENT_RUN_BEGIN, function() {
  42. Base.cursor.hide();
  43. self.draw();
  44. });
  45. runner.on(EVENT_TEST_PENDING, function() {
  46. self.draw();
  47. });
  48. runner.on(EVENT_TEST_PASS, function() {
  49. self.draw();
  50. });
  51. runner.on(EVENT_TEST_FAIL, function() {
  52. self.draw();
  53. });
  54. runner.once(EVENT_RUN_END, function() {
  55. Base.cursor.show();
  56. for (var i = 0; i < self.numberOfLines; i++) {
  57. write('\n');
  58. }
  59. self.epilogue();
  60. });
  61. }
  62. /**
  63. * Inherit from `Base.prototype`.
  64. */
  65. inherits(NyanCat, Base);
  66. /**
  67. * Draw the nyan cat
  68. *
  69. * @private
  70. */
  71. NyanCat.prototype.draw = function() {
  72. this.appendRainbow();
  73. this.drawScoreboard();
  74. this.drawRainbow();
  75. this.drawNyanCat();
  76. this.tick = !this.tick;
  77. };
  78. /**
  79. * Draw the "scoreboard" showing the number
  80. * of passes, failures and pending tests.
  81. *
  82. * @private
  83. */
  84. NyanCat.prototype.drawScoreboard = function() {
  85. var stats = this.stats;
  86. function draw(type, n) {
  87. write(' ');
  88. write(Base.color(type, n));
  89. write('\n');
  90. }
  91. draw('green', stats.passes);
  92. draw('fail', stats.failures);
  93. draw('pending', stats.pending);
  94. write('\n');
  95. this.cursorUp(this.numberOfLines);
  96. };
  97. /**
  98. * Append the rainbow.
  99. *
  100. * @private
  101. */
  102. NyanCat.prototype.appendRainbow = function() {
  103. var segment = this.tick ? '_' : '-';
  104. var rainbowified = this.rainbowify(segment);
  105. for (var index = 0; index < this.numberOfLines; index++) {
  106. var trajectory = this.trajectories[index];
  107. if (trajectory.length >= this.trajectoryWidthMax) {
  108. trajectory.shift();
  109. }
  110. trajectory.push(rainbowified);
  111. }
  112. };
  113. /**
  114. * Draw the rainbow.
  115. *
  116. * @private
  117. */
  118. NyanCat.prototype.drawRainbow = function() {
  119. var self = this;
  120. this.trajectories.forEach(function(line) {
  121. write('\u001b[' + self.scoreboardWidth + 'C');
  122. write(line.join(''));
  123. write('\n');
  124. });
  125. this.cursorUp(this.numberOfLines);
  126. };
  127. /**
  128. * Draw the nyan cat
  129. *
  130. * @private
  131. */
  132. NyanCat.prototype.drawNyanCat = function() {
  133. var self = this;
  134. var startWidth = this.scoreboardWidth + this.trajectories[0].length;
  135. var dist = '\u001b[' + startWidth + 'C';
  136. var padding = '';
  137. write(dist);
  138. write('_,------,');
  139. write('\n');
  140. write(dist);
  141. padding = self.tick ? ' ' : ' ';
  142. write('_|' + padding + '/\\_/\\ ');
  143. write('\n');
  144. write(dist);
  145. padding = self.tick ? '_' : '__';
  146. var tail = self.tick ? '~' : '^';
  147. write(tail + '|' + padding + this.face() + ' ');
  148. write('\n');
  149. write(dist);
  150. padding = self.tick ? ' ' : ' ';
  151. write(padding + '"" "" ');
  152. write('\n');
  153. this.cursorUp(this.numberOfLines);
  154. };
  155. /**
  156. * Draw nyan cat face.
  157. *
  158. * @private
  159. * @return {string}
  160. */
  161. NyanCat.prototype.face = function() {
  162. var stats = this.stats;
  163. if (stats.failures) {
  164. return '( x .x)';
  165. } else if (stats.pending) {
  166. return '( o .o)';
  167. } else if (stats.passes) {
  168. return '( ^ .^)';
  169. }
  170. return '( - .-)';
  171. };
  172. /**
  173. * Move cursor up `n`.
  174. *
  175. * @private
  176. * @param {number} n
  177. */
  178. NyanCat.prototype.cursorUp = function(n) {
  179. write('\u001b[' + n + 'A');
  180. };
  181. /**
  182. * Move cursor down `n`.
  183. *
  184. * @private
  185. * @param {number} n
  186. */
  187. NyanCat.prototype.cursorDown = function(n) {
  188. write('\u001b[' + n + 'B');
  189. };
  190. /**
  191. * Generate rainbow colors.
  192. *
  193. * @private
  194. * @return {Array}
  195. */
  196. NyanCat.prototype.generateColors = function() {
  197. var colors = [];
  198. for (var i = 0; i < 6 * 7; i++) {
  199. var pi3 = Math.floor(Math.PI / 3);
  200. var n = i * (1.0 / 6);
  201. var r = Math.floor(3 * Math.sin(n) + 3);
  202. var g = Math.floor(3 * Math.sin(n + 2 * pi3) + 3);
  203. var b = Math.floor(3 * Math.sin(n + 4 * pi3) + 3);
  204. colors.push(36 * r + 6 * g + b + 16);
  205. }
  206. return colors;
  207. };
  208. /**
  209. * Apply rainbow to the given `str`.
  210. *
  211. * @private
  212. * @param {string} str
  213. * @return {string}
  214. */
  215. NyanCat.prototype.rainbowify = function(str) {
  216. if (!Base.useColors) {
  217. return str;
  218. }
  219. var color = this.rainbowColors[this.colorIndex % this.rainbowColors.length];
  220. this.colorIndex += 1;
  221. return '\u001b[38;5;' + color + 'm' + str + '\u001b[0m';
  222. };
  223. /**
  224. * Stdout helper.
  225. *
  226. * @param {string} string A message to write to stdout.
  227. */
  228. function write(string) {
  229. process.stdout.write(string);
  230. }
  231. NyanCat.description = '"nyan cat"';