JezK
Edit File: progress-bar.js
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; function _classCallCheck2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); _classCallCheck2 = function _classCallCheck2() { return data; }; return data; } function _createClass2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); _createClass2 = function _createClass2() { return data; }; return data; } function _defineProperty2() { var data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function _defineProperty2() { return data; }; return data; } function _util() { var data = require("./util.js"); _util = function _util() { return data; }; return data; } var ProgressBar = /*#__PURE__*/function () { function ProgressBar(total) { var stdout = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : process.stderr; var callback = arguments.length > 2 ? arguments[2] : undefined; (0, _classCallCheck2()["default"])(this, ProgressBar); (0, _defineProperty2()["default"])(this, "stdout", void 0); (0, _defineProperty2()["default"])(this, "curr", void 0); (0, _defineProperty2()["default"])(this, "total", void 0); (0, _defineProperty2()["default"])(this, "width", void 0); (0, _defineProperty2()["default"])(this, "chars", void 0); (0, _defineProperty2()["default"])(this, "delay", void 0); (0, _defineProperty2()["default"])(this, "id", void 0); (0, _defineProperty2()["default"])(this, "_callback", void 0); this.stdout = stdout; this.total = total; this.chars = ProgressBar.bars[0]; this.delay = 60; this.curr = 0; this._callback = callback; (0, _util().clearLine)(stdout); } (0, _createClass2()["default"])(ProgressBar, [{ key: "tick", value: function tick() { var _this = this; if (this.curr >= this.total) { return; } this.curr++; // schedule render if (!this.id) { this.id = setTimeout(function () { return _this.render(); }, this.delay); } } }, { key: "cancelTick", value: function cancelTick() { if (this.id) { clearTimeout(this.id); this.id = null; } } }, { key: "stop", value: function stop() { // "stop" by setting current to end so `tick` becomes noop this.curr = this.total; this.cancelTick(); (0, _util().clearLine)(this.stdout); if (this._callback) { this._callback(this); } } }, { key: "render", value: function render() { // clear throttle this.cancelTick(); var ratio = this.curr / this.total; ratio = Math.min(Math.max(ratio, 0), 1); // progress without bar var bar = " ".concat(this.curr, "/").concat(this.total); // calculate size of actual bar // $FlowFixMe: investigate process.stderr.columns flow error var availableSpace = Math.max(0, this.stdout.columns - bar.length - 3); var width = Math.min(this.total, availableSpace); var completeLength = Math.round(width * ratio); var complete = this.chars[0].repeat(completeLength); var incomplete = this.chars[1].repeat(width - completeLength); bar = "[".concat(complete).concat(incomplete, "]").concat(bar); (0, _util().toStartOfLine)(this.stdout); this.stdout.write(bar); } }]); return ProgressBar; }(); exports["default"] = ProgressBar; (0, _defineProperty2()["default"])(ProgressBar, "bars", [['#', '-']]); //# sourceMappingURL=progress-bar.js.map