UNPKG

cli-block

Version:
75 lines 3.69 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.blockStepLoader = void 0; const tools_1 = require("@sil/tools"); const types_1 = require("../types"); const settings_1 = require("../settings"); const util_1 = require("../util"); const blocks_line_1 = require("./blocks.line"); const blockStepLoader = (args = {}, settings = {}) => __awaiter(void 0, void 0, void 0, function* () { if (settings.logger === types_1.LoggerType.CONSOLE) { (0, util_1.logger)("Loader does not Work with Console.log"); return; } let config = Object.assign({ message: "[percentage] [loader]", // increment: 1, width: "100%", start: 0, step: 0, end: 100, clear: true, charFilled: "▒", charUnfilled: "░", newLine: false }, args); const loaded = Math.round((100 / config.end) * config.step); const loadBar = () => { const calculatedWidth = () => { const contentWidth = (0, settings_1.getContentWidth)() - 7; if (typeof config.width === "number") { if (config.width > 9) { // Assuming the width is bigger than 9, so it can be the number of total characters return config.width; } else { // Otherwise, the number is smaller and will be used as a percentage of the contentWidth return contentWidth * config.width; } } // If it's a string and thus a percentage. if (typeof config.width === "string" && config.width.includes("%")) { const percentageWidth = parseInt(config.width.replace("%", "")); return (contentWidth / 100) * percentageWidth; } else { return contentWidth / 2; } }; const width = calculatedWidth(); const filledPercentage = Math.round(loaded); const filled = Math.round((width / 100) * filledPercentage); const unfilled = Math.round(width - filled); const msg = `${(0, tools_1.repeat)(filled, config.charFilled, true)}${(0, tools_1.repeat)(unfilled, config.charUnfilled, true)}`; return msg; }; const loaderAction = () => { (0, util_1.clear)(); const loader = loadBar(); const percentage = `${loaded}%`; let message = config.message .replace("[step]", `${config.step + 1}`) .replace("[loader]", loader) .replace("[percentage]", (0, util_1.spacedText)(4, percentage)); (0, blocks_line_1.blockLine)(message, Object.assign(Object.assign({}, settings), { newLine: false })); if (config.newLine && config.step === config.end) { (0, util_1.newLine)(); } if (config.clear && config.step === config.end) { (0, util_1.clear)(); } }; loaderAction(); }); exports.blockStepLoader = blockStepLoader; //# sourceMappingURL=blocks.step-loader.js.map