Skip to content

Commit

Permalink
Merge 11747c4 into 3483a30
Browse files Browse the repository at this point in the history
  • Loading branch information
SamVerschueren committed Jul 22, 2017
2 parents 3483a30 + 11747c4 commit 4616d76
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 3 deletions.
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,15 @@ function exec(str, cols, opts) {
const rows = [''];

for (let i = 0, word; (word = words[i]) !== undefined; i++) {
let rowLength = stringWidth(rows[rows.length - 1]);
let rowLength = stringWidth(options.trim === false ? rows[rows.length - 1] : rows[rows.length - 1].trim());

if (rowLength || word === '') {
if (rowLength === cols && options.wordWrap === 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;
}

if (rowLength) {
rows[rows.length - 1] += ' ';
rowLength++;
}
Expand Down Expand Up @@ -133,7 +139,7 @@ function exec(str, cols, opts) {
rows[rows.length - 1] += word;
}

pre = rows.map(x => x.trim()).join('\n');
pre = rows.map(x => options.trim === false ? x : x.trim()).join('\n');

for (let j = 0; j < pre.length; j++) {
const y = pre[j];
Expand Down
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@ 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`<br>
Default: `true`

Whitespace on all lines is removed by default. Set this option to `false` if you don't want to trim.


## Related

Expand Down
8 changes: 8 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,14 @@ test('no word-wrapping', t => {
t.is(res3, 'The q\nuick\nbrown\n\u001B[31mfox j\u001B[39m\n\u001B[31mumped\u001B[39m\n\u001B[31mover\u001B[39m\n\u001B[31m\u001B[39mthe l\nazy \u001B[32md\u001B[39m\n\u001B[32mog an\u001B[39m\n\u001B[32md the\u001B[39m\n\u001B[32mn ran\u001B[39m\n\u001B[32maway\u001B[39m\n\u001B[32mwith\u001B[39m\n\u001B[32mthe u\u001B[39m\n\u001B[32mnicor\u001B[39m\n\u001B[32mn.\u001B[39m');
});

test('no word-wrapping and no trimming', t => {
const res = m(fixture3, 13, {wordWrap: false, trim: false});
t.is(res, '12345678\n901234567890 \n12345');

const res2 = m(fixture, 5, {wordWrap: false, trim: false});
t.is(res2, 'The q\nuick \nbrown\n \u001B[31mfox \u001B[39m\njumpe\nd ove\nr \u001B[39mthe\n lazy\n \u001B[32mdog \u001B[39m\nand t\nhen r\nan aw\nay wi\nth th\ne uni\ncorn.\u001B[39m');
});

// https://github.com/chalk/wrap-ansi/issues/10
test.failing('supports fullwidth characters', t => {
t.is(m('안녕하세', 4, {hard: true}), '안녕\n하세');
Expand Down

0 comments on commit 4616d76

Please sign in to comment.