Skip to content

Commit

Permalink
Require Node.js 18
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 28, 2023
1 parent d989bc4 commit 43069a6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 45 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
18 changes: 9 additions & 9 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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);
Expand Down Expand Up @@ -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();
}
};
Expand All @@ -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 '';
Expand All @@ -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)) {
Expand Down Expand Up @@ -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');
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"default": "./index.js"
},
"engines": {
"node": ">=12"
"node": ">=18"
},
"scripts": {
"test": "xo && nyc ava && tsd"
Expand Down Expand Up @@ -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"
}
}
28 changes: 6 additions & 22 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
## Install

```
$ npm install wrap-ansi
```sh
npm install wrap-ansi
```

## Usage
Expand All @@ -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

Expand Down Expand Up @@ -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)

---

<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>

0 comments on commit 43069a6

Please sign in to comment.