11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ciconst stringWidth = require('string-width');
31cb0ef41Sopenharmony_ciconst stripAnsi = require('strip-ansi');
41cb0ef41Sopenharmony_ciconst ansiStyles = require('ansi-styles');
51cb0ef41Sopenharmony_ci
61cb0ef41Sopenharmony_ciconst ESCAPES = new Set([
71cb0ef41Sopenharmony_ci	'\u001B',
81cb0ef41Sopenharmony_ci	'\u009B'
91cb0ef41Sopenharmony_ci]);
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst END_CODE = 39;
121cb0ef41Sopenharmony_ci
131cb0ef41Sopenharmony_ciconst ANSI_ESCAPE_BELL = '\u0007';
141cb0ef41Sopenharmony_ciconst ANSI_CSI = '[';
151cb0ef41Sopenharmony_ciconst ANSI_OSC = ']';
161cb0ef41Sopenharmony_ciconst ANSI_SGR_TERMINATOR = 'm';
171cb0ef41Sopenharmony_ciconst ANSI_ESCAPE_LINK = `${ANSI_OSC}8;;`;
181cb0ef41Sopenharmony_ci
191cb0ef41Sopenharmony_ciconst wrapAnsi = code => `${ESCAPES.values().next().value}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
201cb0ef41Sopenharmony_ciconst wrapAnsiHyperlink = uri => `${ESCAPES.values().next().value}${ANSI_ESCAPE_LINK}${uri}${ANSI_ESCAPE_BELL}`;
211cb0ef41Sopenharmony_ci
221cb0ef41Sopenharmony_ci// Calculate the length of words split on ' ', ignoring
231cb0ef41Sopenharmony_ci// the extra characters added by ansi escape codes
241cb0ef41Sopenharmony_ciconst wordLengths = string => string.split(' ').map(character => stringWidth(character));
251cb0ef41Sopenharmony_ci
261cb0ef41Sopenharmony_ci// Wrap a long word across multiple rows
271cb0ef41Sopenharmony_ci// Ansi escape codes do not count towards length
281cb0ef41Sopenharmony_ciconst wrapWord = (rows, word, columns) => {
291cb0ef41Sopenharmony_ci	const characters = [...word];
301cb0ef41Sopenharmony_ci
311cb0ef41Sopenharmony_ci	let isInsideEscape = false;
321cb0ef41Sopenharmony_ci	let isInsideLinkEscape = false;
331cb0ef41Sopenharmony_ci	let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
341cb0ef41Sopenharmony_ci
351cb0ef41Sopenharmony_ci	for (const [index, character] of characters.entries()) {
361cb0ef41Sopenharmony_ci		const characterLength = stringWidth(character);
371cb0ef41Sopenharmony_ci
381cb0ef41Sopenharmony_ci		if (visible + characterLength <= columns) {
391cb0ef41Sopenharmony_ci			rows[rows.length - 1] += character;
401cb0ef41Sopenharmony_ci		} else {
411cb0ef41Sopenharmony_ci			rows.push(character);
421cb0ef41Sopenharmony_ci			visible = 0;
431cb0ef41Sopenharmony_ci		}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci		if (ESCAPES.has(character)) {
461cb0ef41Sopenharmony_ci			isInsideEscape = true;
471cb0ef41Sopenharmony_ci			isInsideLinkEscape = characters.slice(index + 1).join('').startsWith(ANSI_ESCAPE_LINK);
481cb0ef41Sopenharmony_ci		}
491cb0ef41Sopenharmony_ci
501cb0ef41Sopenharmony_ci		if (isInsideEscape) {
511cb0ef41Sopenharmony_ci			if (isInsideLinkEscape) {
521cb0ef41Sopenharmony_ci				if (character === ANSI_ESCAPE_BELL) {
531cb0ef41Sopenharmony_ci					isInsideEscape = false;
541cb0ef41Sopenharmony_ci					isInsideLinkEscape = false;
551cb0ef41Sopenharmony_ci				}
561cb0ef41Sopenharmony_ci			} else if (character === ANSI_SGR_TERMINATOR) {
571cb0ef41Sopenharmony_ci				isInsideEscape = false;
581cb0ef41Sopenharmony_ci			}
591cb0ef41Sopenharmony_ci
601cb0ef41Sopenharmony_ci			continue;
611cb0ef41Sopenharmony_ci		}
621cb0ef41Sopenharmony_ci
631cb0ef41Sopenharmony_ci		visible += characterLength;
641cb0ef41Sopenharmony_ci
651cb0ef41Sopenharmony_ci		if (visible === columns && index < characters.length - 1) {
661cb0ef41Sopenharmony_ci			rows.push('');
671cb0ef41Sopenharmony_ci			visible = 0;
681cb0ef41Sopenharmony_ci		}
691cb0ef41Sopenharmony_ci	}
701cb0ef41Sopenharmony_ci
711cb0ef41Sopenharmony_ci	// It's possible that the last row we copy over is only
721cb0ef41Sopenharmony_ci	// ansi escape characters, handle this edge-case
731cb0ef41Sopenharmony_ci	if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
741cb0ef41Sopenharmony_ci		rows[rows.length - 2] += rows.pop();
751cb0ef41Sopenharmony_ci	}
761cb0ef41Sopenharmony_ci};
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_ci// Trims spaces from a string ignoring invisible sequences
791cb0ef41Sopenharmony_ciconst stringVisibleTrimSpacesRight = string => {
801cb0ef41Sopenharmony_ci	const words = string.split(' ');
811cb0ef41Sopenharmony_ci	let last = words.length;
821cb0ef41Sopenharmony_ci
831cb0ef41Sopenharmony_ci	while (last > 0) {
841cb0ef41Sopenharmony_ci		if (stringWidth(words[last - 1]) > 0) {
851cb0ef41Sopenharmony_ci			break;
861cb0ef41Sopenharmony_ci		}
871cb0ef41Sopenharmony_ci
881cb0ef41Sopenharmony_ci		last--;
891cb0ef41Sopenharmony_ci	}
901cb0ef41Sopenharmony_ci
911cb0ef41Sopenharmony_ci	if (last === words.length) {
921cb0ef41Sopenharmony_ci		return string;
931cb0ef41Sopenharmony_ci	}
941cb0ef41Sopenharmony_ci
951cb0ef41Sopenharmony_ci	return words.slice(0, last).join(' ') + words.slice(last).join('');
961cb0ef41Sopenharmony_ci};
971cb0ef41Sopenharmony_ci
981cb0ef41Sopenharmony_ci// The wrap-ansi module can be invoked in either 'hard' or 'soft' wrap mode
991cb0ef41Sopenharmony_ci//
1001cb0ef41Sopenharmony_ci// 'hard' will never allow a string to take up more than columns characters
1011cb0ef41Sopenharmony_ci//
1021cb0ef41Sopenharmony_ci// 'soft' allows long words to expand past the column length
1031cb0ef41Sopenharmony_ciconst exec = (string, columns, options = {}) => {
1041cb0ef41Sopenharmony_ci	if (options.trim !== false && string.trim() === '') {
1051cb0ef41Sopenharmony_ci		return '';
1061cb0ef41Sopenharmony_ci	}
1071cb0ef41Sopenharmony_ci
1081cb0ef41Sopenharmony_ci	let returnValue = '';
1091cb0ef41Sopenharmony_ci	let escapeCode;
1101cb0ef41Sopenharmony_ci	let escapeUrl;
1111cb0ef41Sopenharmony_ci
1121cb0ef41Sopenharmony_ci	const lengths = wordLengths(string);
1131cb0ef41Sopenharmony_ci	let rows = [''];
1141cb0ef41Sopenharmony_ci
1151cb0ef41Sopenharmony_ci	for (const [index, word] of string.split(' ').entries()) {
1161cb0ef41Sopenharmony_ci		if (options.trim !== false) {
1171cb0ef41Sopenharmony_ci			rows[rows.length - 1] = rows[rows.length - 1].trimStart();
1181cb0ef41Sopenharmony_ci		}
1191cb0ef41Sopenharmony_ci
1201cb0ef41Sopenharmony_ci		let rowLength = stringWidth(rows[rows.length - 1]);
1211cb0ef41Sopenharmony_ci
1221cb0ef41Sopenharmony_ci		if (index !== 0) {
1231cb0ef41Sopenharmony_ci			if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) {
1241cb0ef41Sopenharmony_ci				// If we start with a new word but the current row length equals the length of the columns, add a new row
1251cb0ef41Sopenharmony_ci				rows.push('');
1261cb0ef41Sopenharmony_ci				rowLength = 0;
1271cb0ef41Sopenharmony_ci			}
1281cb0ef41Sopenharmony_ci
1291cb0ef41Sopenharmony_ci			if (rowLength > 0 || options.trim === false) {
1301cb0ef41Sopenharmony_ci				rows[rows.length - 1] += ' ';
1311cb0ef41Sopenharmony_ci				rowLength++;
1321cb0ef41Sopenharmony_ci			}
1331cb0ef41Sopenharmony_ci		}
1341cb0ef41Sopenharmony_ci
1351cb0ef41Sopenharmony_ci		// In 'hard' wrap mode, the length of a line is never allowed to extend past 'columns'
1361cb0ef41Sopenharmony_ci		if (options.hard && lengths[index] > columns) {
1371cb0ef41Sopenharmony_ci			const remainingColumns = (columns - rowLength);
1381cb0ef41Sopenharmony_ci			const breaksStartingThisLine = 1 + Math.floor((lengths[index] - remainingColumns - 1) / columns);
1391cb0ef41Sopenharmony_ci			const breaksStartingNextLine = Math.floor((lengths[index] - 1) / columns);
1401cb0ef41Sopenharmony_ci			if (breaksStartingNextLine < breaksStartingThisLine) {
1411cb0ef41Sopenharmony_ci				rows.push('');
1421cb0ef41Sopenharmony_ci			}
1431cb0ef41Sopenharmony_ci
1441cb0ef41Sopenharmony_ci			wrapWord(rows, word, columns);
1451cb0ef41Sopenharmony_ci			continue;
1461cb0ef41Sopenharmony_ci		}
1471cb0ef41Sopenharmony_ci
1481cb0ef41Sopenharmony_ci		if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) {
1491cb0ef41Sopenharmony_ci			if (options.wordWrap === false && rowLength < columns) {
1501cb0ef41Sopenharmony_ci				wrapWord(rows, word, columns);
1511cb0ef41Sopenharmony_ci				continue;
1521cb0ef41Sopenharmony_ci			}
1531cb0ef41Sopenharmony_ci
1541cb0ef41Sopenharmony_ci			rows.push('');
1551cb0ef41Sopenharmony_ci		}
1561cb0ef41Sopenharmony_ci
1571cb0ef41Sopenharmony_ci		if (rowLength + lengths[index] > columns && options.wordWrap === false) {
1581cb0ef41Sopenharmony_ci			wrapWord(rows, word, columns);
1591cb0ef41Sopenharmony_ci			continue;
1601cb0ef41Sopenharmony_ci		}
1611cb0ef41Sopenharmony_ci
1621cb0ef41Sopenharmony_ci		rows[rows.length - 1] += word;
1631cb0ef41Sopenharmony_ci	}
1641cb0ef41Sopenharmony_ci
1651cb0ef41Sopenharmony_ci	if (options.trim !== false) {
1661cb0ef41Sopenharmony_ci		rows = rows.map(stringVisibleTrimSpacesRight);
1671cb0ef41Sopenharmony_ci	}
1681cb0ef41Sopenharmony_ci
1691cb0ef41Sopenharmony_ci	const pre = [...rows.join('\n')];
1701cb0ef41Sopenharmony_ci
1711cb0ef41Sopenharmony_ci	for (const [index, character] of pre.entries()) {
1721cb0ef41Sopenharmony_ci		returnValue += character;
1731cb0ef41Sopenharmony_ci
1741cb0ef41Sopenharmony_ci		if (ESCAPES.has(character)) {
1751cb0ef41Sopenharmony_ci			const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`).exec(pre.slice(index).join('')) || {groups: {}};
1761cb0ef41Sopenharmony_ci			if (groups.code !== undefined) {
1771cb0ef41Sopenharmony_ci				const code = Number.parseFloat(groups.code);
1781cb0ef41Sopenharmony_ci				escapeCode = code === END_CODE ? undefined : code;
1791cb0ef41Sopenharmony_ci			} else if (groups.uri !== undefined) {
1801cb0ef41Sopenharmony_ci				escapeUrl = groups.uri.length === 0 ? undefined : groups.uri;
1811cb0ef41Sopenharmony_ci			}
1821cb0ef41Sopenharmony_ci		}
1831cb0ef41Sopenharmony_ci
1841cb0ef41Sopenharmony_ci		const code = ansiStyles.codes.get(Number(escapeCode));
1851cb0ef41Sopenharmony_ci
1861cb0ef41Sopenharmony_ci		if (pre[index + 1] === '\n') {
1871cb0ef41Sopenharmony_ci			if (escapeUrl) {
1881cb0ef41Sopenharmony_ci				returnValue += wrapAnsiHyperlink('');
1891cb0ef41Sopenharmony_ci			}
1901cb0ef41Sopenharmony_ci
1911cb0ef41Sopenharmony_ci			if (escapeCode && code) {
1921cb0ef41Sopenharmony_ci				returnValue += wrapAnsi(code);
1931cb0ef41Sopenharmony_ci			}
1941cb0ef41Sopenharmony_ci		} else if (character === '\n') {
1951cb0ef41Sopenharmony_ci			if (escapeCode && code) {
1961cb0ef41Sopenharmony_ci				returnValue += wrapAnsi(escapeCode);
1971cb0ef41Sopenharmony_ci			}
1981cb0ef41Sopenharmony_ci
1991cb0ef41Sopenharmony_ci			if (escapeUrl) {
2001cb0ef41Sopenharmony_ci				returnValue += wrapAnsiHyperlink(escapeUrl);
2011cb0ef41Sopenharmony_ci			}
2021cb0ef41Sopenharmony_ci		}
2031cb0ef41Sopenharmony_ci	}
2041cb0ef41Sopenharmony_ci
2051cb0ef41Sopenharmony_ci	return returnValue;
2061cb0ef41Sopenharmony_ci};
2071cb0ef41Sopenharmony_ci
2081cb0ef41Sopenharmony_ci// For each newline, invoke the method separately
2091cb0ef41Sopenharmony_cimodule.exports = (string, columns, options) => {
2101cb0ef41Sopenharmony_ci	return String(string)
2111cb0ef41Sopenharmony_ci		.normalize()
2121cb0ef41Sopenharmony_ci		.replace(/\r\n/g, '\n')
2131cb0ef41Sopenharmony_ci		.split('\n')
2141cb0ef41Sopenharmony_ci		.map(line => exec(line, columns, options))
2151cb0ef41Sopenharmony_ci		.join('\n');
2161cb0ef41Sopenharmony_ci};
217