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