UNPKG

baluni-api

Version:
273 lines (272 loc) 8.59 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.loadPrettyConsole = exports.PrettyConsole = void 0; const fs = require('fs'); class PrettyConsole { constructor() { this.closeByNewLine = true; this.useIcons = true; this.logsTitle = 'LOGS'; this.warningsTitle = 'WARNINGS'; this.errorsTitle = 'ERRORS'; this.informationsTitle = 'INFORMATIONS'; this.successesTitle = 'SUCCESS'; this.debugsTitle = 'DEBUG'; this.assertsTitle = 'ASSERT'; } getColor(foregroundColor = '', backgroundColor = '') { let fgc = '\x1b[37m'; switch (foregroundColor.trim().toLowerCase()) { case 'black': fgc = '\x1b[30m'; break; case 'red': fgc = '\x1b[31m'; break; case 'green': fgc = '\x1b[32m'; break; case 'yellow': fgc = '\x1b[33m'; break; case 'blue': fgc = '\x1b[34m'; break; case 'magenta': fgc = '\x1b[35m'; break; case 'cyan': fgc = '\x1b[36m'; break; case 'white': fgc = '\x1b[37m'; break; } let bgc = ''; switch (backgroundColor.trim().toLowerCase()) { case 'black': bgc = '\x1b[40m'; break; case 'red': bgc = '\x1b[44m'; break; case 'green': bgc = '\x1b[44m'; break; case 'yellow': bgc = '\x1b[43m'; break; case 'blue': bgc = '\x1b[44m'; break; case 'magenta': bgc = '\x1b[45m'; break; case 'cyan': bgc = '\x1b[46m'; break; case 'white': bgc = '\x1b[47m'; break; } return `${fgc}${bgc}`; } getColorReset() { return '\x1b[0m'; } print(foregroundColor = 'white', backgroundColor = 'black', ...strings) { const c = this.getColor(foregroundColor, backgroundColor); // turns objects into printable strings console.log(c, strings.join(''), this.getColorReset()); if (this.closeByNewLine) { console.log(''); } } clear() { console.clear(); } log(...strings) { const fg = 'white'; const bg = ''; const icon = '\u25ce'; const groupTile = ` ${this.logsTitle}`; if (strings.length > 1) { const c = this.getColor(fg, bg); console.group(c, (this.useIcons ? icon : '') + groupTile); const nl = this.closeByNewLine; this.closeByNewLine = false; strings.forEach((item) => { this.print(fg, bg, item, this.getColorReset()); }); this.closeByNewLine = nl; console.groupEnd(); if (nl) { console.log(); } } else { this.print(fg, bg, strings.map((item) => { return `${this.useIcons ? `${icon} ` : ''}${item}`; })); } } warn(...strings) { const fg = 'yellow'; const bg = ''; const icon = '\u26a0'; const groupTile = ` ${this.warningsTitle}`; if (strings.length > 1) { const c = this.getColor(fg, bg); console.group(c, (this.useIcons ? icon : '') + groupTile); const nl = this.closeByNewLine; this.closeByNewLine = false; strings.forEach((item) => { this.print(fg, bg, item, this.getColorReset()); }); this.closeByNewLine = nl; console.groupEnd(); if (nl) { console.log(); } } else { this.print(fg, bg, strings.map((item) => { return `${this.useIcons ? `${icon} ` : ''}${item}`; })); } } error(...strings) { const fg = 'red'; const bg = ''; const icon = '\u26D4'; const groupTile = ` ${this.errorsTitle}`; if (strings.length > 1) { const c = this.getColor(fg, bg); console.group(c, (this.useIcons ? icon : '') + groupTile); const nl = this.closeByNewLine; this.closeByNewLine = false; strings.forEach((item) => { this.print(fg, bg, item); }); this.closeByNewLine = nl; console.groupEnd(); if (nl) { console.log(); } } else { this.print(fg, bg, strings.map((item) => { return `${this.useIcons ? `${icon} ` : ''}${item}`; })); } } info(...strings) { const fg = 'blue'; const bg = ''; const icon = '\u2139'; const groupTile = ` ${this.informationsTitle}`; if (strings.length > 1) { const c = this.getColor(fg, bg); console.group(c, (this.useIcons ? icon : '') + groupTile); const nl = this.closeByNewLine; this.closeByNewLine = false; strings.forEach((item) => { this.print(fg, bg, item); }); this.closeByNewLine = nl; console.groupEnd(); if (nl) { console.log(); } } else { this.print(fg, bg, strings.map((item) => { return `${this.useIcons ? `${icon} ` : ''}${item}`; })); } } success(...strings) { const fg = 'green'; const bg = ''; const icon = '\u2713'; const groupTile = ` ${this.successesTitle}`; if (strings.length > 1) { const c = this.getColor(fg, bg); console.group(c, (this.useIcons ? icon : '') + groupTile); const nl = this.closeByNewLine; this.closeByNewLine = false; strings.forEach((item) => { this.print(fg, bg, item); }); this.closeByNewLine = nl; console.groupEnd(); if (nl) { console.log(); } } else { this.print(fg, bg, strings.map((item) => { return `${this.useIcons ? `${icon} ` : ''}${item}`; })); } } debug(...strings) { const fg = 'magenta'; const bg = ''; const icon = '\u1367'; const groupTile = ` ${this.debugsTitle}`; if (strings.length > 1) { const c = this.getColor(fg, bg); console.group(c, (this.useIcons ? icon : '') + groupTile); const nl = this.closeByNewLine; this.closeByNewLine = false; strings.forEach((item) => { this.print(fg, bg, item); }); this.closeByNewLine = nl; console.groupEnd(); if (nl) { console.log(); } } else { this.print(fg, bg, strings.map((item) => { return `${this.useIcons ? `${icon} ` : ''}${item}`; })); } } assert(...strings) { const fg = 'cyan'; const bg = ''; const icon = '\u0021'; const groupTile = ` ${this.assertsTitle}`; if (strings.length > 1) { const c = this.getColor(fg, bg); console.group(c, (this.useIcons ? icon : '') + groupTile); const nl = this.closeByNewLine; this.closeByNewLine = false; strings.forEach((item) => { this.print(fg, bg, item); }); this.closeByNewLine = nl; console.groupEnd(); if (nl) { console.log(); } } else { this.print(fg, bg, strings.map((item) => { return `${this.useIcons ? `${icon} ` : ''}${item}`; })); } } } exports.PrettyConsole = PrettyConsole; function loadPrettyConsole() { const prettyConsole = new PrettyConsole(); prettyConsole.clear(); prettyConsole.closeByNewLine = true; prettyConsole.useIcons = true; return prettyConsole; } exports.loadPrettyConsole = loadPrettyConsole;