diff --git a/.travis.yml b/.travis.yml index 1492647..a8434a9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: node_js node_js: + - '14' - '12' - '10' - - '8' after_success: - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' diff --git a/index.js b/index.js index 7e46e48..d502255 100755 --- a/index.js +++ b/index.js @@ -76,8 +76,8 @@ const wrapWord = (rows, word, columns) => { }; // Trims spaces from a string ignoring invisible sequences -const stringVisibleTrimSpacesRight = str => { - const words = str.split(' '); +const stringVisibleTrimSpacesRight = string => { + const words = string.split(' '); let last = words.length; while (last > 0) { @@ -89,7 +89,7 @@ const stringVisibleTrimSpacesRight = str => { } if (last === words.length) { - return str; + return string; } return words.slice(0, last).join(' ') + words.slice(last).join(''); @@ -105,16 +105,16 @@ const exec = (string, columns, options = {}) => { return ''; } - let ret = ''; + let returnValue = ''; let escapeCode; - let escapeUri; + let escapeUrl; const lengths = wordLengths(string); let rows = ['']; for (const [index, word] of string.split(' ').entries()) { if (options.trim !== false) { - rows[rows.length - 1] = rows[rows.length - 1].trimLeft(); + rows[rows.length - 1] = rows[rows.length - 1].trimStart(); } let rowLength = stringWidth(rows[rows.length - 1]); @@ -169,40 +169,40 @@ const exec = (string, columns, options = {}) => { const pre = [...rows.join('\n')]; for (const [index, character] of pre.entries()) { - ret += character; + 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: {}}; if (groups.code !== undefined) { - const code = parseFloat(groups.code); - escapeCode = code === END_CODE ? null : code; + const code = Number.parseFloat(groups.code); + escapeCode = code === END_CODE ? undefined : code; } else if (groups.uri !== undefined) { - escapeUri = groups.uri.length === 0 ? null : groups.uri; + escapeUrl = groups.uri.length === 0 ? undefined : groups.uri; } } const code = ansiStyles.codes.get(Number(escapeCode)); if (pre[index + 1] === '\n') { - if (escapeUri) { - ret += wrapAnsiHyperlink(''); + if (escapeUrl) { + returnValue += wrapAnsiHyperlink(''); } if (escapeCode && code) { - ret += wrapAnsi(code); + returnValue += wrapAnsi(code); } } else if (character === '\n') { if (escapeCode && code) { - ret += wrapAnsi(escapeCode); + returnValue += wrapAnsi(escapeCode); } - if (escapeUri) { - ret += wrapAnsiHyperlink(escapeUri); + if (escapeUrl) { + returnValue += wrapAnsiHyperlink(escapeUrl); } } } - return ret; + return returnValue; }; // For each newline, invoke the method separately diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (https://sindresorhus.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: diff --git a/package.json b/package.json index 1d57c9f..bad751d 100644 --- a/package.json +++ b/package.json @@ -4,13 +4,14 @@ "description": "Wordwrap a string with ANSI escape codes", "license": "MIT", "repository": "chalk/wrap-ansi", + "funding": "https://github.com/chalk/wrap-ansi?sponsor=1", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, "engines": { - "node": ">=8" + "node": ">=10" }, "scripts": { "test": "xo && nyc ava" @@ -52,10 +53,10 @@ }, "devDependencies": { "ava": "^2.1.0", - "chalk": "^2.4.2", + "chalk": "^4.0.0", "coveralls": "^3.0.3", - "has-ansi": "^3.0.0", - "nyc": "^14.1.1", - "xo": "^0.24.0" + "has-ansi": "^4.0.0", + "nyc": "^15.0.1", + "xo": "^0.29.1" } } diff --git a/readme.md b/readme.md index d81a4d5..68779ba 100644 --- a/readme.md +++ b/readme.md @@ -1,15 +1,13 @@ -# wrap-ansi [![Build Status](https://travis-ci.org/chalk/wrap-ansi.svg?branch=master)](https://travis-ci.org/chalk/wrap-ansi) [![Coverage Status](https://coveralls.io/repos/github/chalk/wrap-ansi/badge.svg?branch=master)](https://coveralls.io/github/chalk/wrap-ansi?branch=master) +# wrap-ansi [![Build Status](https://travis-ci.com/chalk/wrap-ansi.svg?branch=master)](https://travis-ci.com/chalk/wrap-ansi) [![Coverage Status](https://coveralls.io/repos/github/chalk/wrap-ansi/badge.svg?branch=master)](https://coveralls.io/github/chalk/wrap-ansi?branch=master) > Wordwrap a string with [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code#Colors_and_Styles) - ## Install ``` $ npm install wrap-ansi ``` - ## Usage ```js @@ -24,7 +22,6 @@ console.log(wrapAnsi(input, 20)); - ## API ### wrapAnsi(string, columns, options?) @@ -49,26 +46,25 @@ Type: `object` ##### hard -Type: `boolean`
+Type: `boolean`\ Default: `false` By default the wrap is soft, meaning long words may extend past the column width. Setting this to `true` will make it hard wrap at the column width. ##### wordWrap -Type: `boolean`
+Type: `boolean`\ Default: `true` By default, an attempt is made to split words at spaces, ensuring that they don't extend past the configured columns. If wordWrap is `false`, each column will instead be completely filled splitting words as necessary. ##### trim -Type: `boolean`
+Type: `boolean`\ Default: `true` Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim. - ## Related - [slice-ansi](https://github.com/chalk/slice-ansi) - Slice a string with ANSI escape codes @@ -76,14 +72,12 @@ Whitespace on all lines is removed by default. Set this option to `false` if you - [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) - ---