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

fix: new lines in cells broke table format #466

Merged
merged 1 commit into from
Apr 19, 2024
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
33 changes: 18 additions & 15 deletions src/utils/string-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,26 @@ export const splitTextIntoTextsOfMinLen = (
): string[] => {
const ret: string[] = [];

const spaceSeparatedStrings = inpStr.split(' ');
const lines = inpStr.split(/[\n\r]/);

let now: string[] = [];
let cnt = 0;
spaceSeparatedStrings.forEach((strWithoutSpace) => {
const consoleWidth = findWidthInConsole(strWithoutSpace, charLength);
if (cnt + consoleWidth <= width) {
cnt += consoleWidth + 1; // 1 for the space
now.push(strWithoutSpace);
} else {
ret.push(now.join(' '));
now = [strWithoutSpace];
cnt = consoleWidth + 1;
}
});
ret.push(now.join(' '));
lines.forEach((line) => {
const spaceSeparatedStrings = line.split(' ');

let now: string[] = [];
let cnt = 0;
spaceSeparatedStrings.forEach((strWithoutSpace) => {
const consoleWidth = findWidthInConsole(strWithoutSpace, charLength);
if (cnt + consoleWidth <= width) {
cnt += consoleWidth + 1; // 1 for the space
now.push(strWithoutSpace);
} else {
if (now.length > 0) ret.push(now.join(' '));
now = [strWithoutSpace];
cnt = consoleWidth + 1;
}
});
ret.push(now.join(' '));
});
return ret;
};

Expand Down
21 changes: 21 additions & 0 deletions test/__snapshots__/newLines.test.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Testing line breaks in table cells does not break the table 1`] = `
"β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  foo β”‚  bar  β”‚ baz  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ Sed dictum β”‚  Pellentesque  β”‚ * Cras ac ligula aliquet, ultrices erat  β”‚
β”‚   β”‚  habitant morbi tristique senectus et  β”‚ quis, laoreet lorem.  β”‚
β”‚   β”‚  netus et malesuada fames ac turpis  β”‚ * Mauris rutrum nisi et nisl  β”‚
β”‚   β”‚ egestas. Maecenas ut scelerisque felis.  β”‚ sollicitudin semper.  β”‚
β”‚   β”‚  Etiam ut leo fringilla lacus posuere  β”‚ * Nunc laoreet felis auctor diam aliquet β”‚
β”‚   β”‚  dignissim aliquet eu nulla.  β”‚ semper.  β”‚
β”‚   β”‚   β”‚ * Integer aliquet urna imperdiet orci  β”‚
β”‚   β”‚  Etiam placerat ultricies sollicitudin.  β”‚ pulvinar, at vulputate ante consectetur. β”‚
β”‚   β”‚  Maecenas ultrices feugiat tempor. Nam  β”‚ * Pellentesque a lectus porttitor,  β”‚
β”‚   β”‚  non nisi vel diam tristique commodo.  β”‚ condimentum lacus non, venenatis lacus.  β”‚
β”‚   β”‚ Etiam sit amet nisi sagittis, fringilla  β”‚   β”‚
β”‚   β”‚  tortor sed, efficitur nisl. Maecenas  β”‚   β”‚
β”‚   β”‚  tincidunt lobortis dapibus.  β”‚   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜"
`;
30 changes: 30 additions & 0 deletions test/newLines.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Table } from '../index';

describe('Testing line breaks in table cells', () => {
it('does not break the table', () => {
// Create a table
const p = new Table({
columns: [
{ name: 'foo', alignment: 'right' },
{ name: 'bar', alignment: 'center', minLen: 40 },
{ name: 'baz', alignment: 'left', minLen: 40 },
],
});

p.addRows([
{
foo: 'Sed dictum',
bar: 'Pellentesque\rhabitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas ut scelerisque felis. Etiam ut leo fringilla lacus posuere dignissim aliquet eu nulla.\n\nEtiam placerat ultricies sollicitudin. Maecenas ultrices feugiat tempor. Nam non nisi vel diam tristique commodo. Etiam sit amet nisi sagittis, fringilla tortor sed, efficitur nisl. Maecenas tincidunt lobortis dapibus.',
baz: `* Cras ac ligula aliquet, ultrices erat quis, laoreet lorem.
* Mauris rutrum nisi et nisl sollicitudin semper.
* Nunc laoreet felis auctor diam aliquet semper.
* Integer aliquet urna imperdiet orci pulvinar, at vulputate ante consectetur.
* Pellentesque a lectus porttitor, condimentum lacus non, venenatis lacus.`,
},
]);

// print
p.printTable();
expect(p.render()).toMatchSnapshot();
});
});