2022-01-21 08:28:41 +00:00
|
|
|
'use strict';
|
2022-06-08 10:36:39 +00:00
|
|
|
const stringWidth = require('string-width');
|
|
|
|
const stripAnsi = require('strip-ansi');
|
|
|
|
const ansiStyles = require('ansi-styles');
|
|
|
|
|
|
|
|
const ESCAPES = new Set([
|
|
|
|
'\u001B',
|
|
|
|
'\u009B'
|
|
|
|
]);
|
|
|
|
|
|
|
|
const END_CODE = 39;
|
|
|
|
|
|
|
|
const ANSI_ESCAPE_BELL = '\u0007';
|
|
|
|
const ANSI_CSI = '[';
|
|
|
|
const ANSI_OSC = ']';
|
|
|
|
const ANSI_SGR_TERMINATOR = 'm';
|
|
|
|
const ANSI_ESCAPE_LINK = `${ANSI_OSC}8;;`;
|
|
|
|
|
|
|
|
const wrapAnsi = code => `${ESCAPES.values().next().value}${ANSI_CSI}${code}${ANSI_SGR_TERMINATOR}`;
|
|
|
|
const wrapAnsiHyperlink = uri => `${ESCAPES.values().next().value}${ANSI_ESCAPE_LINK}${uri}${ANSI_ESCAPE_BELL}`;
|
|
|
|
|
|
|
|
// Calculate the length of words split on ' ', ignoring
|
|
|
|
// the extra characters added by ansi escape codes
|
|
|
|
const wordLengths = string => string.split(' ').map(character => stringWidth(character));
|
|
|
|
|
|
|
|
// Wrap a long word across multiple rows
|
|
|
|
// Ansi escape codes do not count towards length
|
|
|
|
const wrapWord = (rows, word, columns) => {
|
|
|
|
const characters = [...word];
|
|
|
|
|
|
|
|
let isInsideEscape = false;
|
|
|
|
let isInsideLinkEscape = false;
|
|
|
|
let visible = stringWidth(stripAnsi(rows[rows.length - 1]));
|
|
|
|
|
|
|
|
for (const [index, character] of characters.entries()) {
|
|
|
|
const characterLength = stringWidth(character);
|
|
|
|
|
|
|
|
if (visible + characterLength <= columns) {
|
|
|
|
rows[rows.length - 1] += character;
|
|
|
|
} else {
|
|
|
|
rows.push(character);
|
|
|
|
visible = 0;
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (ESCAPES.has(character)) {
|
|
|
|
isInsideEscape = true;
|
|
|
|
isInsideLinkEscape = characters.slice(index + 1).join('').startsWith(ANSI_ESCAPE_LINK);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (isInsideEscape) {
|
|
|
|
if (isInsideLinkEscape) {
|
|
|
|
if (character === ANSI_ESCAPE_BELL) {
|
|
|
|
isInsideEscape = false;
|
|
|
|
isInsideLinkEscape = false;
|
|
|
|
}
|
|
|
|
} else if (character === ANSI_SGR_TERMINATOR) {
|
|
|
|
isInsideEscape = false;
|
|
|
|
}
|
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
visible += characterLength;
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (visible === columns && index < characters.length - 1) {
|
2022-01-21 08:28:41 +00:00
|
|
|
rows.push('');
|
|
|
|
visible = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
// It's possible that the last row we copy over is only
|
|
|
|
// ansi escape characters, handle this edge-case
|
2022-01-21 08:28:41 +00:00
|
|
|
if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) {
|
|
|
|
rows[rows.length - 2] += rows.pop();
|
|
|
|
}
|
2022-06-08 10:36:39 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Trims spaces from a string ignoring invisible sequences
|
|
|
|
const stringVisibleTrimSpacesRight = string => {
|
|
|
|
const words = string.split(' ');
|
|
|
|
let last = words.length;
|
|
|
|
|
|
|
|
while (last > 0) {
|
|
|
|
if (stringWidth(words[last - 1]) > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
last--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (last === words.length) {
|
|
|
|
return string;
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
return words.slice(0, last).join(' ') + words.slice(last).join('');
|
|
|
|
};
|
|
|
|
|
|
|
|
// The wrap-ansi module can be invoked in either 'hard' or 'soft' wrap mode
|
2022-01-21 08:28:41 +00:00
|
|
|
//
|
2022-06-08 10:36:39 +00:00
|
|
|
// 'hard' will never allow a string to take up more than columns characters
|
2022-01-21 08:28:41 +00:00
|
|
|
//
|
2022-06-08 10:36:39 +00:00
|
|
|
// 'soft' allows long words to expand past the column length
|
|
|
|
const exec = (string, columns, options = {}) => {
|
|
|
|
if (options.trim !== false && string.trim() === '') {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
let returnValue = '';
|
|
|
|
let escapeCode;
|
|
|
|
let escapeUrl;
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
const lengths = wordLengths(string);
|
|
|
|
let rows = [''];
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
for (const [index, word] of string.split(' ').entries()) {
|
|
|
|
if (options.trim !== false) {
|
|
|
|
rows[rows.length - 1] = rows[rows.length - 1].trimStart();
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
let rowLength = stringWidth(rows[rows.length - 1]);
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (index !== 0) {
|
|
|
|
if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) {
|
|
|
|
// If we start with a new word but the current row length equals the length of the columns, add a new row
|
|
|
|
rows.push('');
|
|
|
|
rowLength = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rowLength > 0 || options.trim === false) {
|
|
|
|
rows[rows.length - 1] += ' ';
|
|
|
|
rowLength++;
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
// In 'hard' wrap mode, the length of a line is never allowed to extend past 'columns'
|
|
|
|
if (options.hard && lengths[index] > columns) {
|
|
|
|
const remainingColumns = (columns - rowLength);
|
|
|
|
const breaksStartingThisLine = 1 + Math.floor((lengths[index] - remainingColumns - 1) / columns);
|
|
|
|
const breaksStartingNextLine = Math.floor((lengths[index] - 1) / columns);
|
|
|
|
if (breaksStartingNextLine < breaksStartingThisLine) {
|
2022-01-21 08:28:41 +00:00
|
|
|
rows.push('');
|
|
|
|
}
|
2022-06-08 10:36:39 +00:00
|
|
|
|
|
|
|
wrapWord(rows, word, columns);
|
2022-01-21 08:28:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) {
|
|
|
|
if (options.wordWrap === false && rowLength < columns) {
|
|
|
|
wrapWord(rows, word, columns);
|
2022-01-21 08:28:41 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
rows.push('');
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (rowLength + lengths[index] > columns && options.wordWrap === false) {
|
|
|
|
wrapWord(rows, word, columns);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2022-01-21 08:28:41 +00:00
|
|
|
rows[rows.length - 1] += word;
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (options.trim !== false) {
|
|
|
|
rows = rows.map(stringVisibleTrimSpacesRight);
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
const pre = [...rows.join('\n')];
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
for (const [index, character] of pre.entries()) {
|
|
|
|
returnValue += character;
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
if (ESCAPES.has(character)) {
|
|
|
|
const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`).exec(pre.slice(index).join('')) || {groups: {}};
|
|
|
|
if (groups.code !== undefined) {
|
|
|
|
const code = Number.parseFloat(groups.code);
|
|
|
|
escapeCode = code === END_CODE ? undefined : code;
|
|
|
|
} else if (groups.uri !== undefined) {
|
|
|
|
escapeUrl = groups.uri.length === 0 ? undefined : groups.uri;
|
|
|
|
}
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
const code = ansiStyles.codes.get(Number(escapeCode));
|
|
|
|
|
|
|
|
if (pre[index + 1] === '\n') {
|
|
|
|
if (escapeUrl) {
|
|
|
|
returnValue += wrapAnsiHyperlink('');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (escapeCode && code) {
|
|
|
|
returnValue += wrapAnsi(code);
|
|
|
|
}
|
|
|
|
} else if (character === '\n') {
|
|
|
|
if (escapeCode && code) {
|
|
|
|
returnValue += wrapAnsi(escapeCode);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (escapeUrl) {
|
|
|
|
returnValue += wrapAnsiHyperlink(escapeUrl);
|
2022-01-21 08:28:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
return returnValue;
|
|
|
|
};
|
2022-01-21 08:28:41 +00:00
|
|
|
|
2022-06-08 10:36:39 +00:00
|
|
|
// For each newline, invoke the method separately
|
|
|
|
module.exports = (string, columns, options) => {
|
|
|
|
return String(string)
|
|
|
|
.normalize()
|
|
|
|
.replace(/\r\n/g, '\n')
|
|
|
|
.split('\n')
|
|
|
|
.map(line => exec(line, columns, options))
|
|
|
|
.join('\n');
|
2022-01-21 08:28:41 +00:00
|
|
|
};
|