Dieses Repository beinhaltet HTML- und Javascript Code zur einer NotizenWebApp auf Basis von Web Storage. Zudem sind Mocha/Chai Tests im Browser enthalten. https://meinenotizen.netlify.app/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

nyan.js 5.2KB

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