diff --git a/index.js b/index.js index d80c74c..f9cf58e 100755 --- a/index.js +++ b/index.js @@ -41,8 +41,9 @@ const wrapWord = (rows, word, columns) => { } if (ESCAPES.has(character)) { + const ansiEscapeLinkCandidate = characters.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join(''); isInsideEscape = true; - isInsideLinkEscape = characters.slice(index + 1).join('').startsWith(ANSI_ESCAPE_LINK); + isInsideLinkEscape = ansiEscapeLinkCandidate === ANSI_ESCAPE_LINK; } if (isInsideEscape) { @@ -164,13 +165,17 @@ const exec = (string, columns, options = {}) => { rows = rows.map(row => stringVisibleTrimSpacesRight(row)); } - const pre = [...rows.join('\n')]; + const preString = rows.join('\n'); + const pre = [...preString]; + + // Account for unicode characters with length 2 + let preStringIndex = 0; for (const [index, character] of pre.entries()) { returnValue += character; if (ESCAPES.has(character)) { - const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?\\d+)m|\\${ANSI_ESCAPE_LINK}(?.*)${ANSI_ESCAPE_BELL})`).exec(pre.slice(index).join('')) || {groups: {}}; + const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?\\d+)m|\\${ANSI_ESCAPE_LINK}(?.*)${ANSI_ESCAPE_BELL})`).exec(preString.slice(preStringIndex)) || {groups: {}}; if (groups.code !== undefined) { const code = Number.parseFloat(groups.code); escapeCode = code === END_CODE ? undefined : code; @@ -198,6 +203,8 @@ const exec = (string, columns, options = {}) => { returnValue += wrapAnsiHyperlink(escapeUrl); } } + + preStringIndex += character.length; } return returnValue;