UNPKG

@juparog/print-table

Version:

print-table is a simple library that allows you to print beautiful and customizable tables in the console using ANSI escape codes. It is ideal for displaying tabular data in a clear and visually appealing way.

171 lines 6.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.padArrayWithAling = exports.alignHorizontalText = exports.getPadding = exports.combineArrays = exports.removeANSICodes = exports.truncateText = exports.SequenceCodeBuilder = exports.SequenceColorCode = exports.SequenceModeCode = exports.SequenceBuilder = void 0; const constants_1 = require("./constants"); /** * Generate the ANSI sequence code for a given mode. * @param args - The mode to be applied. * @returns - The ANSI sequence code. */ function SequenceBuilder(...args) { if (args.length === 0) { return ''; } return `${constants_1.ESC_CODE}${constants_1.CSI_CODE}${args.join(';')}${constants_1.END_CODE}`; } exports.SequenceBuilder = SequenceBuilder; /** * Generate the ANSI sequence code for a given mode. * @param select - The mode to be applied. * @param value - The value to be formatted. * @param mode - The mode to be applied. * @returns - The ANSI sequence code. */ function SequenceModeCode(select, value, mode) { if (select && typeof select === 'function') { return select(value) ? SequenceBuilder(mode) : ''; } if (select) { return SequenceBuilder(mode); } return ''; } exports.SequenceModeCode = SequenceModeCode; /** * Generate the ANSI sequence code for a given color. * @param select - The color to be applied. * @param value - The value to be formatted. * @returns - The ANSI sequence code. */ function SequenceColorCode(select, value) { if (select && typeof select === 'function') { return SequenceBuilder(select(value)); } if (select) { return SequenceBuilder(select); } return ''; } exports.SequenceColorCode = SequenceColorCode; /** * Generate the ANSI sequence code for a given value and options. * @param value - The value to be formatted. * @param options - The options to be applied to the value. * @returns - The ANSI sequence code. */ function SequenceCodeBuilder(value, options) { let sequenceCodes = ''; sequenceCodes += SequenceModeCode(options.bold, value, constants_1.BOLD_MODE); sequenceCodes += SequenceModeCode(options.faint, value, constants_1.FAINT_MODE); sequenceCodes += SequenceModeCode(options.italicize, value, constants_1.ITALICIZE_MODE); sequenceCodes += SequenceModeCode(options.underline, value, constants_1.UNDERLINE_MODE); sequenceCodes += SequenceModeCode(options.slowBlink, value, constants_1.SLOW_BLINK_MODE); sequenceCodes += SequenceModeCode(options.rapidBlink, value, constants_1.RAPID_BLINK_MODE); sequenceCodes += SequenceModeCode(options.inverse, value, constants_1.INVERSE_MODE); sequenceCodes += SequenceModeCode(options.hidden, value, constants_1.HIDDEN_MODE); sequenceCodes += SequenceModeCode(options.strikeThrough, value, constants_1.STRIKE_THROUGH_MODE); sequenceCodes += SequenceColorCode(options.color, value); sequenceCodes += SequenceColorCode(options.bgColor, value); return sequenceCodes; } exports.SequenceCodeBuilder = SequenceCodeBuilder; /** * Truncates a text to a specified maximum length and appends an ellipsis if truncated. * @param text - The input text to be truncated. * @param maxLength - The maximum length of the truncated text. * @returns The truncated text. */ function truncateText(text, maxLength) { if (text.length <= maxLength) { return text; } else { return text.substring(0, maxLength) + '...'; } } exports.truncateText = truncateText; /** * Remove ANSI codes from a string. * @param input - The input string. * @returns The string without ANSI codes. */ function removeANSICodes(input) { // eslint-disable-next-line no-control-regex return input.replace(/\x1B\[[0-9;]*[JKmsu]/g, ''); } exports.removeANSICodes = removeANSICodes; /** * Combine the matrices in `data` exchanging rows for columns and * joining the elements of each row with separator. * * @param {string[][]} data - The array of data to combine. * @param {string} separator - The separator to join the elements of each row. * @returns {string[]} - The array resulting from the combination. */ function combineArrays(data, separator) { if (data.length === 0) { return []; } const transposedData = data[0].map((_, colIndex) => data.map((row) => row[colIndex])); const combinedRows = transposedData.map((row) => row.join(separator)); return combinedRows; } exports.combineArrays = combineArrays; /** * Get the padding for a string. * @param value - The string to be padded. * @param width - The width of the padding. * @param char - The character to use for padding. * @returns The padding string. */ function getPadding(value, width, char) { if (value.length > width) { return ''; } return char.repeat(width - value.length); } exports.getPadding = getPadding; /** * Aligns a text to the left, right, or center. * @param value - The string to be aligned. * @param padding - The padding to be added. * @param align - The alignment of the text. Default: `AlignmentText.LEFT`. * @returns The aligned text. */ function alignHorizontalText(value, padding, align = constants_1.AlignmentText.LEFT) { if (align.startsWith('right')) { return padding + value; } if (align.startsWith('center')) { const center = Math.floor(padding.length / 2); return (padding.substring(0, center) + value + padding.substring(center, padding.length)); } return value + padding; } exports.alignHorizontalText = alignHorizontalText; /** * Pads an array of cell fragments to a specified length with alignment. * @param cellFragments - An array of cell fragments. * @param length - The desired length of the resulting array. * @param align - The alignment for padding. Default: `AlignmentText.TOP`. * @returns A new array with padded cell fragments. */ function padArrayWithAling(cellFragments, length, align = constants_1.AlignmentText.TOP) { if (align.endsWith('bottom')) { return [...Array(length - cellFragments.length).fill(''), ...cellFragments]; } if (align.endsWith('middle')) { const top = Math.floor((length - cellFragments.length) / 2); const bottom = Math.ceil((length - cellFragments.length) / 2); return [ ...Array(top).fill(''), ...cellFragments, ...Array(bottom).fill(''), ]; } return [...cellFragments, ...Array(length - cellFragments.length).fill('')]; } exports.padArrayWithAling = padArrayWithAling; //# sourceMappingURL=utils.js.map