Skip to content

Commit

Permalink
Wrapping text - stylistic changes
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaszmn committed Mar 30, 2020
1 parent b11b59f commit 33269ab
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 21 deletions.
20 changes: 10 additions & 10 deletions index.js
Expand Up @@ -106,9 +106,9 @@ module.exports = (text, options) => {

let contentWidth = widestLine(text) + padding.left + padding.right;

// Add 2 for border
if (contentWidth + 2 > columns) {
contentWidth = columns - 2;
const BORDERS_WIDTH = 2;
if (contentWidth + BORDERS_WIDTH > columns) {
contentWidth = columns - BORDERS_WIDTH;
const max = contentWidth - padding.left - padding.right;
const newLines = [];
for (const line of lines) {
Expand All @@ -122,9 +122,9 @@ module.exports = (text, options) => {
lines = newLines;
}

if (contentWidth + 2 + margin.left + margin.right > columns) {
if (contentWidth + BORDERS_WIDTH + margin.left + margin.right > columns) {
// Let's assume we have margins: left = 3, right = 5, in total = 8
const spaceForMargins = columns - contentWidth - 2;
const spaceForMargins = columns - contentWidth - BORDERS_WIDTH;
// Let's assume we have space = 4
const multiplier = spaceForMargins / (margin.left + margin.right);
// Here: multiplier = 4/8 = 0.5
Expand All @@ -146,10 +146,10 @@ module.exports = (text, options) => {
let marginLeft = PAD.repeat(margin.left);

if (options.float === 'center') {
const padWidth = Math.max((columns - contentWidth - 2) / 2, 0);
const padWidth = Math.max((columns - contentWidth - BORDERS_WIDTH) / 2, 0);
marginLeft = PAD.repeat(padWidth);
} else if (options.float === 'right') {
const padWidth = Math.max(columns - contentWidth - margin.right - 2, 0);
const padWidth = Math.max(columns - contentWidth - margin.right - BORDERS_WIDTH, 0);
marginLeft = PAD.repeat(padWidth);
}

Expand All @@ -158,14 +158,14 @@ module.exports = (text, options) => {
const bottom = colorizeBorder(marginLeft + chars.bottomLeft + horizontal + chars.bottomRight + NL.repeat(margin.bottom));
const side = colorizeBorder(chars.vertical);

const LINE_SEP = (contentWidth + 2 + margin.left >= columns) ? '' : NL;
const LINE_SEPARATOR = (contentWidth + BORDERS_WIDTH + margin.left >= columns) ? '' : NL;

const middle = lines.map(line => {
const paddingRight = PAD.repeat(contentWidth - stringWidth(line) - padding.left);
return marginLeft + side + colorizeContent(paddingLeft + line + paddingRight) + side;
}).join(LINE_SEP);
}).join(LINE_SEPARATOR);

return top + LINE_SEP + middle + LINE_SEP + bottom;
return top + LINE_SEPARATOR + middle + LINE_SEPARATOR + bottom;
};

module.exports._borderStyles = cliBoxes;
26 changes: 15 additions & 11 deletions test.js
Expand Up @@ -332,13 +332,13 @@ test('no wrapping when content = columns - 2 and no padding and no margin', t =>
const box = boxen(longContent);

// No endlines
t.is(box.indexOf('\n'), -1);
t.false(box.includes('\n'));

// Every line has full length
t.is(box.length, width * 3);

// There are no spaces around (and in this case - within)
t.is(box.indexOf(' '), -1);
t.false(box.includes(' '));
});

test('wrapping when content = columns - 1 and no padding and no margin', t => {
Expand All @@ -347,7 +347,7 @@ test('wrapping when content = columns - 1 and no padding and no margin', t => {
const box = boxen(longContent);

// No endlines
t.is(box.indexOf('\n'), -1);
t.false(box.includes('\n'));

// Every line has full length
t.is(box.length, width * 4);
Expand All @@ -362,7 +362,7 @@ test('wrapping when content = columns - 2 and padding = 1 and no margin', t => {
const box = boxen(longContent, {padding: 1});

// No endlines
t.is(box.indexOf('\n'), -1);
t.false(box.includes('\n'));

// Every line has full length: 3 normal lines + 1 wrapped + 2 padding = 6 lines
t.is(box.length, width * 6);
Expand All @@ -377,12 +377,12 @@ test('ignore margins when content = columns - 2 and no padding', t => {
const box = boxen(longContent, {margin: {left: 5, right: 5}});

// No endlines
t.is(box.indexOf('\n'), -1);
t.false(box.includes('\n'));

t.is(box.length, width * 3);

// There are no spaces around (and in this case - within)
t.is(box.indexOf(' '), -1);
t.false(box.includes(' '));
});

test('decrease margins when there is no space for them', t => {
Expand All @@ -396,7 +396,9 @@ test('decrease margins when there is no space for them', t => {
t.is(box.length - 2, 3 * boxWidth);

const lines = box.split('\n');
lines.forEach((line, index) => t.is(line.length, boxWidth, 'Length of line #' + index));
for (const [index, line] of lines.entries()) {
t.is(line.length, boxWidth, 'Length of line #' + index);
}

const expected = ' │' + 'x'.repeat(width - 8) + '│';
t.is(lines[1], expected);
Expand All @@ -414,7 +416,9 @@ test('proportionally decrease margins when there is no space for them', t => {
t.is(box.length - 2, boxWidth * 3);

const lines = box.split('\n');
lines.forEach((line, index) => t.is(line.length, boxWidth, 'Length of line #' + index));
for (const [index, line] of lines.entries()) {
t.is(line.length, boxWidth, 'Length of line #' + index);
}

const expected = ' │' + 'x'.repeat(width - 10) + '│';
t.is(lines[1], expected);
Expand All @@ -433,7 +437,7 @@ test('text is centered after wrapping', t => {
t.is(line.length, width, 'Length of line #' + index);
t.is(line, line.trim(), 'No margin of line #' + index);
if (index !== 2) {
t.is(line.indexOf(' '), -1, 'No spaces in line #' + index);
t.false(line.includes(' '), 'No spaces in line #' + index);
}

lines.push(line);
Expand All @@ -458,7 +462,7 @@ test('text is left-aligned after wrapping', t => {
t.is(line.length, width, 'Length of line #' + index);
t.is(line, line.trim(), 'No margin of line #' + index);
if (index !== 2) {
t.is(line.indexOf(' '), -1, 'No spaces in line #' + index);
t.false(line.includes(' '), 'No spaces in line #' + index);
}

lines.push(line);
Expand All @@ -482,7 +486,7 @@ test('text is right-aligned after wrapping', t => {
t.is(line.length, width, 'Length of line #' + index);
t.is(line, line.trim(), 'No margin of line #' + index);
if (index !== 2) {
t.is(line.indexOf(' '), -1, 'No spaces in line #' + index);
t.false(line.includes(' '), 'No spaces in line #' + index);
}

lines.push(line);
Expand Down

0 comments on commit 33269ab

Please sign in to comment.