Skip to content

Commit

Permalink
Don't trim whitespace in input text needlessly (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
Caesarovich authored Sep 17, 2021
1 parent dff6c30 commit 760e247
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ module.exports = (text, options) => {

const columns = terminalColumns();

let contentWidth = widestLine(wrapAnsi(text, columns - BORDERS_WIDTH, {hard: true})) + padding.left + padding.right;
let contentWidth = widestLine(wrapAnsi(text, columns - BORDERS_WIDTH, {hard: true, trim: false})) + padding.left + padding.right;

// This prevents the title bar to exceed the console's width
let title = options.title && options.title.slice(0, columns - 4 - margin.left - margin.right);
Expand Down
20 changes: 10 additions & 10 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -679,13 +679,13 @@ test('box not overflowing terminal with words and margin', t => {

const lines = [];
for (let index = 1; index < 6; ++index) {
const line = box.slice(index * (word.length + 4), (index + 1) * (word.length + 4));
const line = box.slice(index * (word.length + 5), (index + 1) * (word.length + 5));
lines.push(line);
}

for (const line of lines) {
t.is(line.trim()[0], '│', 'First character of line isn\'t box border');
t.is(line.trim()[word.length], '│', 'Last character of line isn\'t box border');
t.is(line.trim()[word.length + 1], '│', 'Last character of line isn\'t box border');
}
});

Expand All @@ -707,8 +707,8 @@ test('text is centered after wrapping when using words', t => {
t.is(paddingRight, rightPad, 'Padding right in line #' + index);
};

checkAlign({index: 1, leftPad: 0, rightPad: 0});
checkAlign({index: 2, leftPad: sentence.length, rightPad: sentence.length});
checkAlign({index: 1, leftPad: 0, rightPad: 1});
checkAlign({index: 2, leftPad: sentence.length, rightPad: sentence.length + 1});
});

test('text is left-aligned after wrapping when using words', t => {
Expand All @@ -729,8 +729,8 @@ test('text is left-aligned after wrapping when using words', t => {
t.is(paddingRight, rightPad, 'Padding right in line #' + index);
};

checkAlign({index: 1, leftPad: 0, rightPad: 0});
checkAlign({index: 2, leftPad: 0, rightPad: sentence.length * 2});
checkAlign({index: 1, leftPad: 0, rightPad: 1});
checkAlign({index: 2, leftPad: 0, rightPad: (sentence.length * 2) + 1});
});

test('text is right-aligned after wrapping when using words', t => {
Expand All @@ -751,8 +751,8 @@ test('text is right-aligned after wrapping when using words', t => {
t.is(paddingRight, rightPad, 'Padding right in line #' + index);
};

checkAlign({index: 1, leftPad: 0, rightPad: 0});
checkAlign({index: 2, leftPad: sentence.length * 2, rightPad: 0});
checkAlign({index: 1, leftPad: 1, rightPad: 0});
checkAlign({index: 2, leftPad: (sentence.length * 2) + 1, rightPad: 0});
});

test('text is right-aligned after wrapping when using words, with padding', t => {
Expand All @@ -776,6 +776,6 @@ test('text is right-aligned after wrapping when using words, with padding', t =>
t.is(paddingRight, rightPad, 'Padding right in line #' + index);
};

checkAlign({index: 1, leftPad: 1, rightPad: 1});
checkAlign({index: 2, leftPad: (sentence.length * 2) + 1, rightPad: 1});
checkAlign({index: 1, leftPad: 2, rightPad: 1});
checkAlign({index: 2, leftPad: (sentence.length * 2) + 2, rightPad: 1});
});

0 comments on commit 760e247

Please sign in to comment.