landing.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. 'use strict';
  2. /**
  3. * @typedef {import('../runner.js')} Runner
  4. */
  5. /**
  6. * @module Landing
  7. */
  8. /**
  9. * Module dependencies.
  10. */
  11. var Base = require('./base');
  12. var inherits = require('../utils').inherits;
  13. var constants = require('../runner').constants;
  14. var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN;
  15. var EVENT_RUN_END = constants.EVENT_RUN_END;
  16. var EVENT_TEST_END = constants.EVENT_TEST_END;
  17. var STATE_FAILED = require('../runnable').constants.STATE_FAILED;
  18. var cursor = Base.cursor;
  19. var color = Base.color;
  20. /**
  21. * Expose `Landing`.
  22. */
  23. exports = module.exports = Landing;
  24. /**
  25. * Airplane color.
  26. */
  27. Base.colors.plane = 0;
  28. /**
  29. * Airplane crash color.
  30. */
  31. Base.colors['plane crash'] = 31;
  32. /**
  33. * Runway color.
  34. */
  35. Base.colors.runway = 90;
  36. /**
  37. * Constructs a new `Landing` reporter instance.
  38. *
  39. * @public
  40. * @class
  41. * @memberof Mocha.reporters
  42. * @extends Mocha.reporters.Base
  43. * @param {Runner} runner - Instance triggers reporter actions.
  44. * @param {Object} [options] - runner options
  45. */
  46. function Landing(runner, options) {
  47. Base.call(this, runner, options);
  48. var self = this;
  49. var width = (Base.window.width * 0.75) | 0;
  50. var stream = process.stdout;
  51. var plane = color('plane', '✈');
  52. var crashed = -1;
  53. var n = 0;
  54. var total = 0;
  55. function runway() {
  56. var buf = Array(width).join('-');
  57. return ' ' + color('runway', buf);
  58. }
  59. runner.on(EVENT_RUN_BEGIN, function () {
  60. stream.write('\n\n\n ');
  61. cursor.hide();
  62. });
  63. runner.on(EVENT_TEST_END, function (test) {
  64. // check if the plane crashed
  65. var col = crashed === -1 ? ((width * ++n) / ++total) | 0 : crashed;
  66. // show the crash
  67. if (test.state === STATE_FAILED) {
  68. plane = color('plane crash', '✈');
  69. crashed = col;
  70. }
  71. // render landing strip
  72. stream.write('\u001b[' + (width + 1) + 'D\u001b[2A');
  73. stream.write(runway());
  74. stream.write('\n ');
  75. stream.write(color('runway', Array(col).join('⋅')));
  76. stream.write(plane);
  77. stream.write(color('runway', Array(width - col).join('⋅') + '\n'));
  78. stream.write(runway());
  79. stream.write('\u001b[0m');
  80. });
  81. runner.once(EVENT_RUN_END, function () {
  82. cursor.show();
  83. process.stdout.write('\n');
  84. self.epilogue();
  85. });
  86. // if cursor is hidden when we ctrl-C, then it will remain hidden unless...
  87. process.once('SIGINT', function () {
  88. cursor.show();
  89. process.nextTick(function () {
  90. process.kill(process.pid, 'SIGINT');
  91. });
  92. });
  93. }
  94. /**
  95. * Inherit from `Base.prototype`.
  96. */
  97. inherits(Landing, Base);
  98. Landing.description = 'Unicode landing strip';