nyan.js 5.5 KB

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