source: Dev/trunk/node_modules/mocha/lib/reporters/nyan.js

Last change on this file was 484, checked in by hendrikvanantwerpen, 11 years ago

Commit node_modules, to make checkouts and builds more deterministic.

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