Comparing version 8.1.0 to 9.0.0
@@ -27,4 +27,4 @@ export type Options = { | ||
@param string - String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk). Newline characters will be normalized to `\n`. | ||
@param columns - Number of columns to wrap the text to. | ||
@param string - A string with ANSI escape codes, like one styled by [`chalk`](https://github.com/chalk/chalk). Newline characters will be normalized to `\n`. | ||
@param columns - The number of columns to wrap the text to. | ||
@@ -31,0 +31,0 @@ @example |
32
index.js
@@ -18,3 +18,3 @@ import stringWidth from 'string-width'; | ||
const wrapAnsiCode = 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}`; | ||
const wrapAnsiHyperlink = url => `${ESCAPES.values().next().value}${ANSI_ESCAPE_LINK}${url}${ANSI_ESCAPE_BELL}`; | ||
@@ -32,3 +32,3 @@ // Calculate the length of words split on ' ', ignoring | ||
let isInsideLinkEscape = false; | ||
let visible = stringWidth(stripAnsi(rows[rows.length - 1])); | ||
let visible = stringWidth(stripAnsi(rows.at(-1))); | ||
@@ -47,3 +47,5 @@ for (const [index, character] of characters.entries()) { | ||
isInsideEscape = true; | ||
isInsideLinkEscape = characters.slice(index + 1).join('').startsWith(ANSI_ESCAPE_LINK); | ||
const ansiEscapeLinkCandidate = characters.slice(index + 1, index + 1 + ANSI_ESCAPE_LINK.length).join(''); | ||
isInsideLinkEscape = ansiEscapeLinkCandidate === ANSI_ESCAPE_LINK; | ||
} | ||
@@ -74,3 +76,3 @@ | ||
// ansi escape characters, handle this edge-case | ||
if (!visible && rows[rows.length - 1].length > 0 && rows.length > 1) { | ||
if (!visible && rows.at(-1).length > 0 && rows.length > 1) { | ||
rows[rows.length - 2] += rows.pop(); | ||
@@ -100,7 +102,7 @@ } | ||
// The wrap-ansi module can be invoked in either 'hard' or 'soft' wrap mode | ||
// The wrap-ansi module can be invoked in either 'hard' or 'soft' wrap mode. | ||
// | ||
// 'hard' will never allow a string to take up more than columns characters | ||
// 'hard' will never allow a string to take up more than columns characters. | ||
// | ||
// 'soft' allows long words to expand past the column length | ||
// 'soft' allows long words to expand past the column length. | ||
const exec = (string, columns, options = {}) => { | ||
@@ -120,6 +122,6 @@ if (options.trim !== false && string.trim() === '') { | ||
if (options.trim !== false) { | ||
rows[rows.length - 1] = rows[rows.length - 1].trimStart(); | ||
rows[rows.length - 1] = rows.at(-1).trimStart(); | ||
} | ||
let rowLength = stringWidth(rows[rows.length - 1]); | ||
let rowLength = stringWidth(rows.at(-1)); | ||
@@ -173,4 +175,8 @@ if (index !== 0) { | ||
const pre = [...rows.join('\n')]; | ||
const preString = rows.join('\n'); | ||
const pre = [...preString]; | ||
// We need to keep a separate index as `String#slice()` works on Unicode code units, while `pre` is an array of codepoints. | ||
let preStringIndex = 0; | ||
for (const [index, character] of pre.entries()) { | ||
@@ -180,3 +186,3 @@ returnValue += character; | ||
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: {}}; | ||
const {groups} = new RegExp(`(?:\\${ANSI_CSI}(?<code>\\d+)m|\\${ANSI_ESCAPE_LINK}(?<uri>.*)${ANSI_ESCAPE_BELL})`).exec(preString.slice(preStringIndex)) || {groups: {}}; | ||
if (groups.code !== undefined) { | ||
@@ -209,2 +215,4 @@ const code = Number.parseFloat(groups.code); | ||
} | ||
preStringIndex += character.length; | ||
} | ||
@@ -219,3 +227,3 @@ | ||
.normalize() | ||
.replace(/\r\n/g, '\n') | ||
.replaceAll('\r\n', '\n') | ||
.split('\n') | ||
@@ -222,0 +230,0 @@ .map(line => exec(line, columns, options)) |
{ | ||
"name": "wrap-ansi", | ||
"version": "8.1.0", | ||
"version": "9.0.0", | ||
"description": "Wordwrap a string with ANSI escape codes", | ||
@@ -19,3 +19,3 @@ "license": "MIT", | ||
"engines": { | ||
"node": ">=12" | ||
"node": ">=18" | ||
}, | ||
@@ -57,15 +57,15 @@ "scripts": { | ||
"dependencies": { | ||
"ansi-styles": "^6.1.0", | ||
"string-width": "^5.0.1", | ||
"strip-ansi": "^7.0.1" | ||
"ansi-styles": "^6.2.1", | ||
"string-width": "^7.0.0", | ||
"strip-ansi": "^7.1.0" | ||
}, | ||
"devDependencies": { | ||
"ava": "^3.15.0", | ||
"chalk": "^4.1.2", | ||
"ava": "^5.3.1", | ||
"chalk": "^5.3.0", | ||
"coveralls": "^3.1.1", | ||
"has-ansi": "^5.0.1", | ||
"nyc": "^15.1.0", | ||
"tsd": "^0.25.0", | ||
"xo": "^0.44.0" | ||
"tsd": "^0.29.0", | ||
"xo": "^0.56.0" | ||
} | ||
} |
@@ -7,5 +7,5 @@ # wrap-ansi | ||
```sh | ||
npm install wrap-ansi | ||
``` | ||
$ npm install wrap-ansi | ||
``` | ||
@@ -36,4 +36,6 @@ ## Usage | ||
String with ANSI escape codes. Like one styled by [`chalk`](https://github.com/chalk/chalk). Newline characters will be normalized to `\n`. | ||
A string with ANSI escape codes, like one styled by [`chalk`](https://github.com/chalk/chalk). | ||
Newline characters will be normalized to `\n`. | ||
#### columns | ||
@@ -43,3 +45,3 @@ | ||
Number of columns to wrap the text to. | ||
The number of columns to wrap the text to. | ||
@@ -77,19 +79,1 @@ #### options | ||
- [jsesc](https://github.com/mathiasbynens/jsesc) - Generate ASCII-only output from Unicode strings. Useful for creating test fixtures. | ||
## Maintainers | ||
- [Sindre Sorhus](https://github.com/sindresorhus) | ||
- [Josh Junon](https://github.com/qix-) | ||
- [Benjamin Coe](https://github.com/bcoe) | ||
--- | ||
<div align="center"> | ||
<b> | ||
<a href="https://tidelift.com/subscription/pkg/npm-wrap_ansi?utm_source=npm-wrap-ansi&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a> | ||
</b> | ||
<br> | ||
<sub> | ||
Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies. | ||
</sub> | ||
</div> |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
208
11484
76
+ Addedemoji-regex@10.4.0(transitive)
+ Addedget-east-asian-width@1.2.0(transitive)
+ Addedstring-width@7.2.0(transitive)
- Removedeastasianwidth@0.2.0(transitive)
- Removedemoji-regex@9.2.2(transitive)
- Removedstring-width@5.1.2(transitive)
Updatedansi-styles@^6.2.1
Updatedstring-width@^7.0.0
Updatedstrip-ansi@^7.1.0