Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't trim whitespace in input text needlessly #63

Merged
merged 2 commits into from
Sep 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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});
});