UNPKG

stylesh

Version:

A powerful and elegant terminal styling library with colors, gradients, borders, themes, and animations for Node.js

47 lines 1.28 kB
"use strict"; /** * Text width calculation utilities * Properly handles ANSI escape codes */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getVisibleWidth = getVisibleWidth; exports.getMaxLineWidth = getMaxLineWidth; exports.parseTextLines = parseTextLines; exports.stripAnsi = stripAnsi; const ANSI_REGEX = /\x1b\[[0-9;]*m/g; /** * Calculate the visible width of a string (excluding ANSI codes) */ function getVisibleWidth(text) { const cleanText = text.replace(ANSI_REGEX, ''); return cleanText.length; } /** * Split text into lines and get the maximum visible width */ function getMaxLineWidth(text, delimiter = '\n') { const lines = text.split(delimiter); let maxWidth = 0; for (const line of lines) { const width = getVisibleWidth(line); if (width > maxWidth) { maxWidth = width; } } return maxWidth; } /** * Get text lines and their properties */ function parseTextLines(text, delimiter = '\n') { const lines = text.split(delimiter); const maxWidth = getMaxLineWidth(text, delimiter); return { lines, maxWidth }; } /** * Strip all ANSI codes from text */ function stripAnsi(text) { return text.replace(ANSI_REGEX, ''); } //# sourceMappingURL=textWidth.js.map