landing.js 1.7 KB

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