Skip to content

Commit

Permalink
Merge 7ec2497 into 6ec84f0
Browse files Browse the repository at this point in the history
  • Loading branch information
stroncium committed Mar 11, 2019
2 parents 6ec84f0 + 7ec2497 commit e6a99fc
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
35 changes: 29 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,23 @@ const wrapWord = (rows, word, columns) => {
}
};

// Trims spaces from a string ignoring invisible sequences
const stringVisibleTrimSpacesRight = str => {
const words = str.split(' ');
const first = 0;
let last = words.length;

while (last > first) {
if (stringWidth(words[last - 1]) > 0) {
break;
}

last--;
}

return words.slice(0, first).join('') + words.slice(first, last).join(' ') + words.slice(last, words.length).join('');
};

// The wrap-ansi module can be invoked
// in either 'hard' or 'soft' wrap mode
//
Expand All @@ -77,7 +94,7 @@ const exec = (string, columns, options = {}) => {
let escapeCode;

const lengths = wordLengths(string);
const rows = [''];
let rows = [''];

for (const [index, word] of string.split(' ').entries()) {
if (options.trim !== false) {
Expand All @@ -87,14 +104,16 @@ const exec = (string, columns, options = {}) => {
let rowLength = stringWidth(rows[rows.length - 1]);

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
rows.push('');
rowLength = 0;
}

rows[rows.length - 1] += ' ';
rowLength++;
if (rowLength > 0 || options.trim === false) {
rows[rows.length - 1] += ' ';
rowLength++;
}
}

// In 'hard' wrap mode, the length of a line is
Expand All @@ -111,7 +130,7 @@ const exec = (string, columns, options = {}) => {
continue;
}

if (rowLength + lengths[index] > columns && rowLength > 0) {
if (rowLength + lengths[index] > columns && rowLength > 0 && lengths[index] > 0) {
if (options.wordWrap === false && rowLength < columns) {
wrapWord(rows, word, columns);
continue;
Expand All @@ -128,7 +147,11 @@ const exec = (string, columns, options = {}) => {
rows[rows.length - 1] += word;
}

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()) {
ret += character;
Expand Down
3 changes: 2 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@ test('#23, properly wraps whitespace with no trimming', t => {
});

test('#24, trims leading and trailing whitespace only on actual wrapped lines and only with trimming', t => {
t.is(wrapAnsi(' foo bar ', 3), 'foo\nbar');
t.is(wrapAnsi(' foo bar ', 6), 'foo\nbar');
t.is(wrapAnsi(' foo bar ', 42), 'foo bar');
t.is(wrapAnsi(' foo bar ', 42, {trim: false}), ' foo bar ');
});

test.failing('#24, trims leading and trailing whitespace inside a color block only on actual wrapped lines and only with trimming', t => {
test('#24, trims leading and trailing whitespace inside a color block only on actual wrapped lines and only with trimming', t => {
t.is(wrapAnsi(chalk.blue(' foo bar '), 6), chalk.blue('foo\nbar'));
t.is(wrapAnsi(chalk.blue(' foo bar '), 42), chalk.blue('foo bar'));
t.is(wrapAnsi(chalk.blue(' foo bar '), 42, {trim: false}), chalk.blue(' foo bar '));
Expand Down

0 comments on commit e6a99fc

Please sign in to comment.