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