nyan.js 4.7 KB

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