progress.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /**
  2. * Module dependencies.
  3. */
  4. var Base = require('./base')
  5. , cursor = Base.cursor
  6. , color = Base.color;
  7. /**
  8. * Expose `Progress`.
  9. */
  10. exports = module.exports = Progress;
  11. /**
  12. * General progress bar color.
  13. */
  14. Base.colors.progress = 90;
  15. /**
  16. * Initialize a new `Progress` bar test reporter.
  17. *
  18. * @param {Runner} runner
  19. * @param {Object} options
  20. * @api public
  21. */
  22. function Progress(runner, options) {
  23. Base.call(this, runner);
  24. var self = this
  25. , options = options || {}
  26. , stats = this.stats
  27. , width = Base.window.width * .50 | 0
  28. , total = runner.total
  29. , complete = 0
  30. , max = Math.max;
  31. // default chars
  32. options.open = options.open || '[';
  33. options.complete = options.complete || '▬';
  34. options.incomplete = options.incomplete || Base.symbols.dot;
  35. options.close = options.close || ']';
  36. options.verbose = false;
  37. // tests started
  38. runner.on('start', function(){
  39. console.log();
  40. cursor.hide();
  41. });
  42. // tests complete
  43. runner.on('test end', function(){
  44. complete++;
  45. var incomplete = total - complete
  46. , percent = complete / total
  47. , n = width * percent | 0
  48. , i = width - n;
  49. cursor.CR();
  50. process.stdout.write('\u001b[J');
  51. process.stdout.write(color('progress', ' ' + options.open));
  52. process.stdout.write(Array(n).join(options.complete));
  53. process.stdout.write(Array(i).join(options.incomplete));
  54. process.stdout.write(color('progress', options.close));
  55. if (options.verbose) {
  56. process.stdout.write(color('progress', ' ' + complete + ' of ' + total));
  57. }
  58. });
  59. // tests are complete, output some stats
  60. // and the failures if any
  61. runner.on('end', function(){
  62. cursor.show();
  63. console.log();
  64. self.epilogue();
  65. });
  66. }
  67. /**
  68. * Inherit from `Base.prototype`.
  69. */
  70. Progress.prototype.__proto__ = Base.prototype;