r3bl-ts-utils
Version:
The `r3bl-ts-utils` package is a set of useful TypeScript functions and classes that can be used in Node.js and browser environments. They are inspired by Kotlin stdlib, and Rust to write code as expressions rather than statements, colorized text, powerfu
62 lines • 3.07 kB
JavaScript
/*
* Copyright (c) 2022 R3BL LLC. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.sleep = exports.printHeader = void 0;
const kotlin_lang_utils_1 = require("../lang-utils/kotlin-lang-utils");
const color_console_1 = require("./color-console");
const internal_constants_1 = require("./internal-constants");
const printHeader = (message, postFix = internal_constants_1.defaultPostFix) => {
const isTooWideChar = message.length > internal_constants_1.maxWidth ? "\n" : "";
const spans = (0, kotlin_lang_utils_1._also)(new Array(3), (spans) => {
const headerLine = getHeaderLine(message, internal_constants_1.defaultRepeatChar);
const headerLineUnderscores = headerLine.replace(internal_constants_1.regExForDefaultRepeatChar, internal_constants_1.spaceChar);
let headerLeft, headerRight;
if (isTooWideChar) {
headerLeft = color_console_1.Formatter.headerUnderlineFn(headerLineUnderscores);
headerRight = color_console_1.Formatter.headerUnderlineFn(headerLineUnderscores);
}
else {
headerLeft = color_console_1.Formatter.headerUnderlineFn(getHeaderLine(message, internal_constants_1.defaultShortLeftRepeatChar));
headerRight = color_console_1.Formatter.headerFn(getHeaderLine(message, internal_constants_1.defaultShortRightRepeatChar));
}
spans[0] = headerLeft;
spans[1] = color_console_1.Formatter.headerMessageFn(`${internal_constants_1.spaceChar}${message}${internal_constants_1.spaceChar}`);
spans[2] = headerRight;
});
const output = spans.join(isTooWideChar ? "\n" : "");
console.log(output + postFix);
};
exports.printHeader = printHeader;
const getHeaderLine = (message, repeatChar) => (0, kotlin_lang_utils_1._let)(message.length, (count) => repeatChar.repeat(count + internal_constants_1.padding));
const sleep = (ms = 500) => {
const sprites = ["-", "\\", "-", "/"];
let count = 0;
const printDotsInterval = setInterval(() => {
const sprite = sprites[count++ % sprites.length]?.toString() ?? "";
color_console_1.ColorConsole.create(color_console_1.Styles.Primary)("Sleep " + sprite).consoleLogInPlace();
}, 100);
return new Promise((resolveFn) => {
setTimeout(() => {
clearInterval(printDotsInterval);
console.log();
resolveFn();
}, ms);
});
};
exports.sleep = sleep;
//# sourceMappingURL=helpers.js.map
;