nyan.js 4.7 KB

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