terminal-styling
Version:
A npm package to add some styling to terminal output
117 lines (116 loc) • 4.83 kB
JavaScript
;
var __spreadArray = (this && this.__spreadArray) || function (to, from) {
for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
to[j] = from[i];
return to;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Console = void 0;
var Colors_1 = require("../colors/Colors");
/**
* Utility class for styled and categorized console output.
*
* Provides methods for printing error, warning, info, debug, table,
* and custom-colored messages using ANSI escape codes.
*
* @export
* @class Console
*/
var Console = /** @class */ (function () {
function Console() {
/**
* Logs an error message in red.
*
* @param {message} consoleLogMessage - The message to log.
* @example
* console.err("This is an error");
*/
this.err = function (consoleLogMessage) {
process.stdout.write(Colors_1.color.foregroundColor('ff0000') + consoleLogMessage + "\n");
};
/**
* Logs a warning message in yellow.
*
* @param {message} consoleLogMessage - The warning message.
* @example
* console.warn("This is a warning");
*/
this.warn = function (consoleLogMessage) {
process.stdout.write(Colors_1.color.foregroundColor('ffff00') + consoleLogMessage + "\n");
};
/**
* Logs an informational message in green.
*
* @param {message} consoleLogMessage - The info message.
* @example
* console.info("System ready");
*/
this.info = function (consoleLogMessage) {
process.stdout.write(Colors_1.color.foregroundColor('008000') + consoleLogMessage + "\n");
};
/**
* Logs data as a formatted table using process.stdout.write.
*
* @param {...any[]} args - Data to be displayed in a table format.
* @example
* console.table([{ name: "Alice", age: 25 }, { name: "Bob", age: 30 }]);
*/
this.table = function () {
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var data = args[0];
if (!Array.isArray(data) || data.length === 0 || typeof data[0] !== 'object') {
process.stdout.write('Invalid table data.\n');
return;
}
var headers = Object.keys(data[0]);
var rows = data.map(function (row) { return headers.map(function (h) { var _a; return String((_a = row[h]) !== null && _a !== void 0 ? _a : ''); }); });
var allRows = __spreadArray([headers], rows);
// Calculate max width per column
var colWidths = headers.map(function (_, i) {
return Math.max.apply(Math, allRows.map(function (row) { return row[i].length; }));
});
var printRow = function (row) {
var line = row.map(function (cell, i) { return cell.padEnd(colWidths[i]); }).join(' | ');
process.stdout.write(line + '\n');
};
// Print table
printRow(headers);
process.stdout.write(colWidths.map(function (w) { return '-'.repeat(w); }).join('-+-') + '\n');
rows.forEach(printRow);
};
/**
* Logs a message with a custom foreground and optional background color using hex codes.
*
* @param {string} text_color - Foreground color as a hex string (e.g. "ff0000").
* @param {string} consoleLogMessage - The message to log.
* @param {string} [bg_color] - Optional background color as a hex string (e.g. "000000").
* @example
* console.customColor("00ffff", "Cyan text");
* console.customColor("ffffff", "White on black", "000000");
*/
this.customColor = function (text_color, consoleLogMessage, bg_color) {
var resetAll = '\x1b[0m';
var out = '';
out += Colors_1.color.foregroundColor(text_color);
if (bg_color)
out += Colors_1.color.backgroundColor(bg_color);
out += consoleLogMessage + resetAll;
process.stdout.write(out + "\n");
};
/**
* Logs a debug message without styling.
*
* @param {message} consoleLogMessage - The debug message.
* @example
* console.debug("Debug: variable x = 42");
*/
this.debug = function (consoleLogMessage) {
process.stdout.write(consoleLogMessage + "\n");
};
}
return Console;
}());
exports.Console = Console;