landing.js 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. 'use strict';
  2. /**
  3. * @module Landing
  4. */
  5. /**
  6. * Module dependencies.
  7. */
  8. var Base = require('./base');
  9. var inherits = require('../utils').inherits;
  10. var cursor = Base.cursor;
  11. var color = Base.color;
  12. /**
  13. * Expose `Landing`.
  14. */
  15. exports = module.exports = Landing;
  16. /**
  17. * Airplane color.
  18. */
  19. Base.colors.plane = 0;
  20. /**
  21. * Airplane crash color.
  22. */
  23. Base.colors['plane crash'] = 31;
  24. /**
  25. * Runway color.
  26. */
  27. Base.colors.runway = 90;
  28. /**
  29. * Initialize a new `Landing` reporter.
  30. *
  31. * @public
  32. * @class
  33. * @memberof Mocha.reporters
  34. * @extends Mocha.reporters.Base
  35. * @api public
  36. * @param {Runner} runner
  37. */
  38. function Landing(runner) {
  39. Base.call(this, runner);
  40. var self = this;
  41. var width = (Base.window.width * 0.75) | 0;
  42. var total = runner.total;
  43. var stream = process.stdout;
  44. var plane = color('plane', '✈');
  45. var crashed = -1;
  46. var n = 0;
  47. function runway() {
  48. var buf = Array(width).join('-');
  49. return ' ' + color('runway', buf);
  50. }
  51. runner.on('start', function() {
  52. stream.write('\n\n\n ');
  53. cursor.hide();
  54. });
  55. runner.on('test end', function(test) {
  56. // check if the plane crashed
  57. var col = crashed === -1 ? (width * ++n / total) | 0 : crashed;
  58. // show the crash
  59. if (test.state === 'failed') {
  60. plane = color('plane crash', '✈');
  61. crashed = col;
  62. }
  63. // render landing strip
  64. stream.write('\u001b[' + (width + 1) + 'D\u001b[2A');
  65. stream.write(runway());
  66. stream.write('\n ');
  67. stream.write(color('runway', Array(col).join('⋅')));
  68. stream.write(plane);
  69. stream.write(color('runway', Array(width - col).join('⋅') + '\n'));
  70. stream.write(runway());
  71. stream.write('\u001b[0m');
  72. });
  73. runner.once('end', function() {
  74. cursor.show();
  75. console.log();
  76. self.epilogue();
  77. });
  78. }
  79. /**
  80. * Inherit from `Base.prototype`.
  81. */
  82. inherits(Landing, Base);