easy-console-color
Version:
Easily add color and style to console output in both web and shell environments using intuitive placeholders.
79 lines (78 loc) • 2.17 kB
JavaScript
var types = {
reset: "\x1b[0m",
bright: "\x1b[1m",
dim: "\x1b[2m",
underscore: "\x1b[4m",
blink: "\x1b[5m",
reverse: "\x1b[7m",
hidden: "\x1b[8m",
};
var colors = {
fgBlack: "\x1b[30m",
fgRed: "\x1b[31m",
fgGreen: "\x1b[32m",
fgYellow: "\x1b[33m",
fgBlue: "\x1b[34m",
fgMagenta: "\x1b[35m",
fgCyan: "\x1b[36m",
fgWhite: "\x1b[37m",
bgBlack: "\x1b[40m",
bgRed: "\x1b[41m",
bgGreen: "\x1b[42m",
bgYellow: "\x1b[43m",
bgBlue: "\x1b[44m",
bgMagenta: "\x1b[45m",
bgCyan: "\x1b[46m",
bgWhite: "\x1b[47m",
};
var rewrite = function (args) {
for (var i in args) {
if (typeof args[i] !== "string")
continue;
var text = args[i];
var matches = text.match(/\$\([a-zA-Z]+\)/g);
if (!matches)
continue;
for (var k in matches) {
var match = matches[k];
var matched = match.replace(/\$\(|\)/g, "");
var futures = "";
futures += types[matched] || colors[matched] || "";
text = text.replace(match, futures);
}
args[i] = text + types.reset;
}
return args;
};
var consoleLog = console.log;
var consoleError = console.error;
var consoleWarn = console.warn;
var consoleInfo = console.info;
console.log = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return consoleLog.apply(void 0, rewrite(args));
};
console.error = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return consoleError.apply(void 0, rewrite(args));
};
console.warn = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return consoleWarn.apply(void 0, rewrite(args));
};
console.info = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
return consoleInfo.apply(void 0, rewrite(args));
};