landing.js 2.1 KB

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