2 const stringWidth = require('string-width');
3 const stripAnsi = require('strip-ansi');
4 const ansiStyles = require('ansi-styles');
6 const ESCAPES = new Set([
13 const wrapAnsi = code => `${ESCAPES.values().next().value}[${code}m`;
15 // Calculate the length of words split on ' ', ignoring
16 // the extra characters added by ansi escape codes
17 const wordLengths = string => string.split(' ').map(character => stringWidth(character));
19 // Wrap a long word across multiple rows
20 // Ansi escape codes do not count towards length
21 const wrapWord = (rows, word, columns) => {
22 const characters = [...word];
24 let insideEscape = false;
25 let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
27 for (const [index, character] of characters.entries()) {
28 const characterLength = stringWidth(character);
30 if (visible + characterLength <= columns) {
31 rows[rows.length - 1] += character;
37 if (ESCAPES.has(character)) {
39 } else if (insideEscape && character === 'm') {
48 visible += characterLength;
50 if (visible === columns && index < characters.length - 1) {
56 // It's possible that the last row we copy over is only
57 // ansi escape characters, handle this edge-case
58 if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
59 rows[rows.length - 2] += rows.pop();
63 // Trims spaces from a string ignoring invisible sequences
64 const stringVisibleTrimSpacesRight = str => {
65 const words = str.split(' ');
66 let last = words.length;
69 if (stringWidth(words[last - 1]) > 0) {
76 if (last === words.length) {
80 return words.slice(0, last).join(' ') + words.slice(last).join('');
83 // The wrap-ansi module can be invoked
84 // in either 'hard' or 'soft' wrap mode
86 // 'hard' will never allow a string to take up more
87 // than columns characters
89 // 'soft' allows long words to expand past the column length
90 const exec = (string, columns, options = {}) => {
91 if (options.trim !== false && string.trim() === '') {
99 const lengths = wordLengths(string);
102 for (const [index, word] of string.split(' ').entries()) {
103 if (options.trim !== false) {
104 rows[rows.length - 1] = rows[rows.length - 1].trimLeft();
107 let rowLength = stringWidth(rows[rows.length - 1]);
110 if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) {
111 // If we start with a new word but the current row length equals the length of the columns, add a new row
116 if (rowLength > 0 || options.trim === false) {
117 rows[rows.length - 1] += ' ';
122 // In 'hard' wrap mode, the length of a line is
123 // never allowed to extend past 'columns'
124 if (options.hard && lengths[index] > columns) {
125 const remainingColumns = (columns - rowLength);
126 const breaksStartingThisLine = 1 + Math.floor((lengths[index] - remainingColumns - 1) / columns);
127 const breaksStartingNextLine = Math.floor((lengths[index] - 1) / columns);
128 if (breaksStartingNextLine < breaksStartingThisLine) {
132 wrapWord(rows, word, columns);
136 if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) {
137 if (options.wordWrap === false && rowLength < columns) {
138 wrapWord(rows, word, columns);
145 if (rowLength + lengths[index] > columns && options.wordWrap === false) {
146 wrapWord(rows, word, columns);
150 rows[rows.length - 1] += word;
153 if (options.trim !== false) {
154 rows = rows.map(stringVisibleTrimSpacesRight);
157 pre = rows.join('\n');
159 for (const [index, character] of [...pre].entries()) {
162 if (ESCAPES.has(character)) {
163 const code = parseFloat(/\d[^m]*/.exec(pre.slice(index, index + 4)));
164 escapeCode = code === END_CODE ? null : code;
167 const code = ansiStyles.codes.get(Number(escapeCode));
169 if (escapeCode && code) {
170 if (pre[index + 1] === '\n') {
171 ret += wrapAnsi(code);
172 } else if (character === '\n') {
173 ret += wrapAnsi(escapeCode);
181 // For each newline, invoke the method separately
182 module.exports = (string, columns, options) => {
183 return String(string)
186 .map(line => exec(line, columns, options))