nyan.js 4.8 KB

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