@gamastudio/colorslog
Version:
A utility for logging colored messages to the console with different system message types.
251 lines (248 loc) • 9.15 kB
JavaScript
function _extends() {
return _extends = Object.assign ? Object.assign.bind() : function (n) {
for (var e = 1; e < arguments.length; e++) {
var t = arguments[e];
for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
}
return n;
}, _extends.apply(null, arguments);
}
var SystemMessageType;
(function (SystemMessageType) {
SystemMessageType["SYS"] = "SYS";
SystemMessageType["ERROR"] = "ERROR";
SystemMessageType["WARNING"] = "WARNING";
SystemMessageType["INFO"] = "INFO";
SystemMessageType["SUCCESS"] = "SUCCESS";
SystemMessageType["TIMEOUT"] = "TIMEOUT";
// Añade más tipos según necesites
})(SystemMessageType || (SystemMessageType = {}));
var Colors = /*#__PURE__*/function () {
function Colors() {
this.TIMEOUT = '\x1b[0m';
this.globalConfig = {
zoneHour: 0,
dateShow: true,
useBackground: false
};
this.colors = {
bright: '\x1b[1m',
dim: '\x1b[2m',
underscore: '\x1b[4m',
blink: '\x1b[5m',
reverse: '\x1b[7m',
hidden: '\x1b[8m',
black: '\x1b[30m',
red: '\x1b[31m',
green: '\x1b[32m',
yellow: '\x1b[33m',
blue: '\x1b[34m',
magenta: '\x1b[35m',
cyan: '\x1b[36m',
white: '\x1b[37m',
gray: '\x1b[90m',
orange: '\x1b[38;5;208m',
bgOrange: '\x1b[48;5;208m',
bgBlack: '\x1b[40m',
bgRed: '\x1b[41m',
bgGreen: '\x1b[42m',
bgYellow: '\x1b[43m',
bgBlue: '\x1b[44m',
bgMagenta: '\x1b[45m',
bgCyan: '\x1b[46m',
bgWhite: '\x1b[47m',
bgGray: '\x1b[100m',
// Nuevos colores
brightRed: '\x1b[91m',
brightGreen: '\x1b[92m',
brightYellow: '\x1b[93m',
brightBlue: '\x1b[94m',
brightMagenta: '\x1b[95m',
brightCyan: '\x1b[96m',
brightWhite: '\x1b[97m',
bgBrightRed: '\x1b[101m',
bgBrightGreen: '\x1b[102m',
bgBrightYellow: '\x1b[103m',
bgBrightBlue: '\x1b[104m',
bgBrightMagenta: '\x1b[105m',
bgBrightCyan: '\x1b[106m',
bgBrightWhite: '\x1b[107m'
};
}
var _proto = Colors.prototype;
_proto.getFormattedDate = function getFormattedDate(zoneHour) {
var currentDate = new Date();
var offset = currentDate.getTimezoneOffset() + zoneHour * 60; // Ajuste de zona horaria
var adjustedDate = new Date(currentDate.getTime() + offset * 60 * 1000);
return adjustedDate.getDate().toString().padStart(2, '0') + "-" + (adjustedDate.getMonth() + 1).toString().padStart(2, '0') + "-" + adjustedDate.getFullYear().toString().slice(-2) + " " + adjustedDate.getHours().toString().padStart(2, '0') + ":" + adjustedDate.getMinutes().toString().padStart(2, '0');
};
_proto.formatArguments = function formatArguments(args) {
return args.map(function (arg) {
if (typeof arg === 'object') {
try {
return JSON.stringify(arg, null, 2);
} catch (_unused) {
return String(arg);
}
}
return String(arg);
}).join(' ');
};
_proto.extractConfig = function extractConfig(args) {
// Buscar si el último argumento es un objeto de configuración
var lastArg = args[args.length - 1];
if (lastArg && typeof lastArg === 'object' && !Array.isArray(lastArg)) {
// Verificar si tiene propiedades de LogConfig
var configKeys = ['zoneHour', 'dateShow', 'useBackground'];
var hasConfigProps = configKeys.some(function (key) {
return key in lastArg;
});
if (hasConfigProps) {
return lastArg;
}
}
return undefined;
};
_proto.filterArgs = function filterArgs(args) {
var config = this.extractConfig(args);
if (config) {
// Remover el último argumento si es configuración
return args.slice(0, -1);
}
return args;
};
_proto.log = function log(color, text, config) {
var formattedText = text;
if (this.colors[color]) {
var _ref = config || {},
_ref$dateShow = _ref.dateShow,
dateShow = _ref$dateShow === void 0 ? this.globalConfig.dateShow : _ref$dateShow,
_ref$zoneHour = _ref.zoneHour,
zoneHour = _ref$zoneHour === void 0 ? this.globalConfig.zoneHour : _ref$zoneHour;
if (dateShow) {
var formattedDate = this.getFormattedDate(zoneHour || 0);
formattedText = this.colors['orange'] + "[DATE: " + this.colors['red'] + formattedDate + "] " + this.colors[color] + text + this.TIMEOUT;
} else {
formattedText = "" + this.colors[color] + text + this.TIMEOUT;
}
}
console.log(formattedText);
};
_proto.sys = function sys(type, config) {
var colorKey;
var bgColorKey;
var brightColorKey; // Para colores más brillantes y visibles
switch (type) {
case SystemMessageType.SYS:
colorKey = 'cyan';
bgColorKey = 'bgCyan';
brightColorKey = 'brightCyan';
break;
case SystemMessageType.ERROR:
colorKey = 'red';
bgColorKey = 'bgRed';
brightColorKey = 'brightRed';
break;
case SystemMessageType.WARNING:
colorKey = 'yellow';
bgColorKey = 'bgYellow';
brightColorKey = 'brightYellow';
break;
case SystemMessageType.INFO:
colorKey = 'blue';
bgColorKey = 'bgBlue';
brightColorKey = 'brightBlue';
break;
case SystemMessageType.SUCCESS:
colorKey = 'green';
bgColorKey = 'bgGreen';
brightColorKey = 'brightGreen';
break;
case SystemMessageType.TIMEOUT:
colorKey = 'orange';
bgColorKey = 'bgOrange';
brightColorKey = 'orange'; // Orange no tiene versión bright, usamos el normal
break;
default:
colorKey = ''; // Sin color si el tipo no se reconoce
bgColorKey = '';
brightColorKey = '';
}
// Usar configuración local o global para determinar si usar fondo
var _ref2 = config || {},
_ref2$useBackground = _ref2.useBackground,
useBackground = _ref2$useBackground === void 0 ? this.globalConfig.useBackground : _ref2$useBackground;
var prefix;
if (useBackground) {
// Modo original con fondo de color
prefix = this.colors[bgColorKey] + "[" + type + "]" + this.TIMEOUT;
} else {
// Modo mejorado: usar colores brillantes sin fondo para mejor visibilidad
prefix = "" + this.colors['bright'] + this.colors[brightColorKey] + "[" + type + "]" + this.TIMEOUT;
}
// Formatear todos los argumentos
for (var _len = arguments.length, args = new Array(_len > 2 ? _len - 2 : 0), _key = 2; _key < _len; _key++) {
args[_key - 2] = arguments[_key];
}
var formattedText = this.formatArguments(args);
this.log(colorKey, prefix + " " + formattedText, config);
};
_proto.system = function system() {
for (var _len2 = arguments.length, args = new Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
args[_key2] = arguments[_key2];
}
var config = this.extractConfig(args);
var filteredArgs = this.filterArgs(args);
this.sys.apply(this, ['SYS', config].concat(filteredArgs));
};
_proto.info = function info() {
for (var _len3 = arguments.length, args = new Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
args[_key3] = arguments[_key3];
}
var config = this.extractConfig(args);
var filteredArgs = this.filterArgs(args);
this.sys.apply(this, ['INFO', config].concat(filteredArgs));
};
_proto.warn = function warn() {
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
args[_key4] = arguments[_key4];
}
var config = this.extractConfig(args);
var filteredArgs = this.filterArgs(args);
this.sys.apply(this, ['WARNING', config].concat(filteredArgs));
};
_proto.success = function success() {
for (var _len5 = arguments.length, args = new Array(_len5), _key5 = 0; _key5 < _len5; _key5++) {
args[_key5] = arguments[_key5];
}
var config = this.extractConfig(args);
var filteredArgs = this.filterArgs(args);
this.sys.apply(this, ['SUCCESS', config].concat(filteredArgs));
};
_proto.timeout = function timeout() {
for (var _len6 = arguments.length, args = new Array(_len6), _key6 = 0; _key6 < _len6; _key6++) {
args[_key6] = arguments[_key6];
}
var config = this.extractConfig(args);
var filteredArgs = this.filterArgs(args);
this.sys.apply(this, ['TIMEOUT', config].concat(filteredArgs));
};
_proto.error = function error() {
for (var _len7 = arguments.length, args = new Array(_len7), _key7 = 0; _key7 < _len7; _key7++) {
args[_key7] = arguments[_key7];
}
var config = this.extractConfig(args);
var filteredArgs = this.filterArgs(args);
this.sys.apply(this, ['ERROR', config].concat(filteredArgs));
};
_proto.clear = function clear() {
console.clear();
};
_proto.setConfig = function setConfig(config) {
this.globalConfig = _extends({}, this.globalConfig, config);
};
return Colors;
}();
var colors = /*#__PURE__*/new Colors();
export { SystemMessageType, colors };
//# sourceMappingURL=colorslog.esm.js.map