diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d36e1a8..346585c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,11 +10,11 @@ jobs: fail-fast: false matrix: node-version: - - 14 - - 12 + - 20 + - 18 steps: - - uses: actions/checkout@v2 - - uses: actions/setup-node@v2 + - uses: actions/checkout@v4 + - uses: actions/setup-node@v4 with: node-version: ${{ matrix.node-version }} - run: npm install diff --git a/index.d.ts b/index.d.ts index 95471ca..ba98338 100644 --- a/index.d.ts +++ b/index.d.ts @@ -24,8 +24,8 @@ export type Options = { /** Wrap words to the specified column width. -@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. @example ``` diff --git a/index.js b/index.js index f851e3b..10bd0bf 100755 --- a/index.js +++ b/index.js @@ -15,7 +15,7 @@ const ANSI_SGR_TERMINATOR = 'm'; const ANSI_ESCAPE_LINK = `${ANSI_OSC}8;;`; 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}`; // Calculate the length of words split on ' ', ignoring // the extra characters added by ansi escape codes @@ -28,7 +28,7 @@ const wrapWord = (rows, word, columns) => { let isInsideEscape = false; let isInsideLinkEscape = false; - let visible = stringWidth(stripAnsi(rows[rows.length - 1])); + let visible = stringWidth(stripAnsi(rows.at(-1))); for (const [index, character] of characters.entries()) { const characterLength = stringWidth(character); @@ -70,7 +70,7 @@ const wrapWord = (rows, word, columns) => { // It's possible that the last row we copy over is only // 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(); } }; @@ -95,11 +95,11 @@ const stringVisibleTrimSpacesRight = string => { return words.slice(0, last).join(' ') + words.slice(last).join(''); }; -// 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 = {}) => { if (options.trim !== false && string.trim() === '') { return ''; @@ -114,10 +114,10 @@ const exec = (string, columns, options = {}) => { for (const [index, word] of string.split(' ').entries()) { 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)); if (index !== 0) { if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) { @@ -215,7 +215,7 @@ const exec = (string, columns, options = {}) => { export default function wrapAnsi(string, columns, options) { return String(string) .normalize() - .replace(/\r\n/g, '\n') + .replaceAll('\r\n', '\n') .split('\n') .map(line => exec(line, columns, options)) .join('\n'); diff --git a/package.json b/package.json index 198a5db..8824a85 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "default": "./index.js" }, "engines": { - "node": ">=12" + "node": ">=18" }, "scripts": { "test": "xo && nyc ava && tsd" @@ -53,17 +53,17 @@ "text" ], "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" } } diff --git a/readme.md b/readme.md index 21f6fed..86327dc 100644 --- a/readme.md +++ b/readme.md @@ -4,8 +4,8 @@ ## Install -``` -$ npm install wrap-ansi +```sh +npm install wrap-ansi ``` ## Usage @@ -32,13 +32,15 @@ Wrap words to the specified column width. Type: `string` -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 Type: `number` -Number of columns to wrap the text to. +The number of columns to wrap the text to. #### options @@ -71,21 +73,3 @@ Whitespace on all lines is removed by default. Set this option to `false` if you - [cli-truncate](https://github.com/sindresorhus/cli-truncate) - Truncate a string to a specific width in the terminal - [chalk](https://github.com/chalk/chalk) - Terminal string styling done right - [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) - ---- - -