wrap-ansi
Advanced tools
Comparing version 5.0.0 to 5.1.0
38
index.js
@@ -63,2 +63,22 @@ 'use strict'; | ||
// Trims spaces from a string ignoring invisible sequences | ||
const stringVisibleTrimSpacesRight = str => { | ||
const words = str.split(' '); | ||
let last = words.length; | ||
while (last > 0) { | ||
if (stringWidth(words[last - 1]) > 0) { | ||
break; | ||
} | ||
last--; | ||
} | ||
if (last === words.length) { | ||
return str; | ||
} | ||
return words.slice(0, last).join(' ') + words.slice(last).join(''); | ||
}; | ||
// The wrap-ansi module can be invoked | ||
@@ -81,3 +101,3 @@ // in either 'hard' or 'soft' wrap mode | ||
const lengths = wordLengths(string); | ||
const rows = ['']; | ||
let rows = ['']; | ||
@@ -92,3 +112,3 @@ for (const [index, word] of string.split(' ').entries()) { | ||
if (index !== 0) { | ||
if (rowLength === columns && (options.wordWrap === false || options.trim === false)) { | ||
if (rowLength >= columns && (options.wordWrap === false || options.trim === false)) { | ||
// If we start with a new word but the current row length equals the length of the columns, add a new row | ||
@@ -99,4 +119,6 @@ rows.push(''); | ||
rows[rows.length - 1] += ' '; | ||
rowLength++; | ||
if (rowLength > 0 || options.trim === false) { | ||
rows[rows.length - 1] += ' '; | ||
rowLength++; | ||
} | ||
} | ||
@@ -118,3 +140,3 @@ | ||
if (rowLength + lengths[index] > columns && rowLength > 0) { | ||
if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) { | ||
if (options.wordWrap === false && rowLength < columns) { | ||
@@ -136,4 +158,8 @@ wrapWord(rows, word, columns); | ||
pre = rows.map(row => options.trim === false ? row : row.trim()).join('\n'); | ||
if (options.trim !== false) { | ||
rows = rows.map(stringVisibleTrimSpacesRight); | ||
} | ||
pre = rows.join('\n'); | ||
for (const [index, character] of [...pre].entries()) { | ||
@@ -140,0 +166,0 @@ ret += character; |
{ | ||
"name": "wrap-ansi", | ||
"version": "5.0.0", | ||
"version": "5.1.0", | ||
"description": "Wordwrap a string with ANSI escape codes", | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
@@ -27,3 +27,17 @@ # 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) | ||
--- | ||
<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> | ||
--- | ||
## API | ||
@@ -88,4 +102,9 @@ | ||
## Security | ||
To report a security vulnerability, please use the [Tidelift security contact](https://tidelift.com/security). Tidelift will coordinate the fix and disclosure. | ||
## License | ||
MIT |
9624
149
109