| 1 | "use strict"; |
| 2 | |
| 3 | /** |
| 4 | * @typedef {import('../runner.js')} Runner |
| 5 | */ |
| 6 | |
| 7 | /** |
| 8 | * @module Progress |
| 9 | */ |
| 10 | /** |
| 11 | * Module dependencies. |
| 12 | */ |
| 13 | |
| 14 | var Base = require("./base"); |
| 15 | var constants = require("../runner").constants; |
| 16 | var EVENT_RUN_BEGIN = constants.EVENT_RUN_BEGIN; |
| 17 | var EVENT_TEST_END = constants.EVENT_TEST_END; |
| 18 | var EVENT_RUN_END = constants.EVENT_RUN_END; |
| 19 | var inherits = require("../utils").inherits; |
| 20 | var color = Base.color; |
| 21 | var cursor = Base.cursor; |
| 22 | |
| 23 | /** |
| 24 | * Expose `Progress`. |
| 25 | */ |
| 26 | |
| 27 | exports = module.exports = Progress; |
| 28 | |
| 29 | /** |
| 30 | * General progress bar color. |
| 31 | */ |
| 32 | |
| 33 | Base.colors.progress = 90; |
| 34 | |
| 35 | /** |
| 36 | * Constructs a new `Progress` reporter instance. |
| 37 | * |
| 38 | * @public |
| 39 | * @class |
| 40 | * @memberof Mocha.reporters |
| 41 | * @extends Mocha.reporters.Base |
| 42 | * @param {Runner} runner - Instance triggers reporter actions. |
| 43 | * @param {Object} [options] - runner options |
| 44 | */ |
| 45 | function Progress(runner, options) { |
| 46 | Base.call(this, runner, options); |
| 47 | |
| 48 | var self = this; |
| 49 | var width = (Base.window.width * 0.5) | 0; |
| 50 | var total = runner.total; |
| 51 | var complete = 0; |
| 52 | var lastN = -1; |
| 53 | |
| 54 | // default chars |
| 55 | options = options || {}; |
| 56 | var reporterOptions = options.reporterOptions || {}; |
| 57 | |
| 58 | options.open = reporterOptions.open || "["; |
| 59 | options.complete = reporterOptions.complete || "▬"; |
| 60 | options.incomplete = reporterOptions.incomplete || Base.symbols.dot; |
| 61 | options.close = reporterOptions.close || "]"; |
| 62 | options.verbose = reporterOptions.verbose || false; |
| 63 | |
| 64 | // tests started |
| 65 | runner.on(EVENT_RUN_BEGIN, function () { |
| 66 | process.stdout.write("\n"); |
| 67 | cursor.hide(); |
| 68 | }); |
| 69 | |
| 70 | // tests complete |
| 71 | runner.on(EVENT_TEST_END, function () { |
| 72 | complete++; |
| 73 | |
| 74 | var percent = complete / total; |
| 75 | var n = (width * percent) | 0; |
| 76 | var i = width - n; |
| 77 | |
| 78 | if (n === lastN && !options.verbose) { |
| 79 | // Don't re-render the line if it hasn't changed |
| 80 | return; |
| 81 | } |
| 82 | lastN = n; |
| 83 | |
| 84 | cursor.CR(); |
| 85 | process.stdout.write("\u001b[J"); |
| 86 | process.stdout.write(color("progress", " " + options.open)); |
| 87 | process.stdout.write(Array(n).join(options.complete)); |
| 88 | process.stdout.write(Array(i).join(options.incomplete)); |
| 89 | process.stdout.write(color("progress", options.close)); |
| 90 | if (options.verbose) { |
| 91 | process.stdout.write(color("progress", " " + complete + " of " + total)); |
| 92 | } |
| 93 | }); |
| 94 | |
| 95 | // tests are complete, output some stats |
| 96 | // and the failures if any |
| 97 | runner.once(EVENT_RUN_END, function () { |
| 98 | cursor.show(); |
| 99 | process.stdout.write("\n"); |
| 100 | self.epilogue(); |
| 101 | }); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Inherit from `Base.prototype`. |
| 106 | */ |
| 107 | inherits(Progress, Base); |
| 108 | |
| 109 | Progress.description = "a progress bar"; |