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

Add wrapOnWordBoundary option for consistently readable word wrapping #217

Merged
merged 1 commit into from Mar 31, 2022

Conversation

speedytwenty
Copy link
Collaborator

@speedytwenty speedytwenty commented May 4, 2021

This is in response to #125:

The current wordWrap functionality wraps only on space characters and truncates overhanging text.

Current wordWrap:

const table = new Table({
  head: ['1234', '1234'],
  colWidths: [4, 4],
  wordWrap: true,
});
table.push(['one', 'The Quick']);
table.push(['two', 'Brown Fox']);
table.push(['three', 'Jumped Over']);
table.push(['four', 'A Lazy']);
table.push(['five', 'Dog']);
console.log(table.toString());

Outputs.. not very useful anything:

┌────┬────┐
│ 1… │ 1… │
├────┼────┤
│ o… │ T… │
│    │ Q… │
├────┼────┤
│ t… │ B… │
│    │ F… │
├────┼────┤
│ t… │ J… │
│    │ O… │
├────┼────┤
│ f… │ A  │
│    │ L… │
├────┼────┤
│ f… │ D… │
└────┴────┘

Proposed feature: "wrapWords" Table Option

The "wrapWords" option produces an alternative word wrapping that truly wraps without truncating words.

const table = new Table({
  head: ['1234', '1234'],
  colWidths: [4, 4], // no word wrap without fixed width AFAIK
  wordWrap: true,
  wrapOnWordBoundary: false, // <- NEW
});
table.push(
  ['one', 'The Quick'],
  ['two', 'Brown Fox'],
  ['three', 'Jumped Over'],
  ['four', 'A Lazy'],
  ['five', 'Dog'],
);
console.log(table.toString());

Outputs

┌────┬────┐
│ 12 │ 12 │
│ 34 │ 34 │
├────┼────┤
│ on │ Th │
│ e  │ e  │
│    │ Qu │
│    │ ic │
│    │ k  │
├────┼────┤
│ tw │ Br │
│ o  │ ow │
│    │ n  │
│    │ Fo │
│    │ x  │
├────┼────┤
│ th │ Ju │
│ re │ mp │
│ e  │ ed │
│    │  O │
│    │ ve │
│    │ r  │
├────┼────┤
│ fo │ A  │
│ ur │ La │
│    │ zy │
├────┼────┤
│ fi │ Do │
│ ve │ g  │
└────┴────┘

It would be awesome to set wordWrap and wrapOnWordBoundary at the cell level level, but this global addition as-is provides a useful new option without introducing compatibility issues or needed redundant unit tests.

@speedytwenty
Copy link
Collaborator Author

Alright, I revamped this to clarify the interface and added tests that generate into docs for the new wrapOnWordBoundaryOption.

Usage (with existing wordWrap option):

const table = new Table({
  colWidths: [3, 3],
  wordWrap: true,
  wrapOnWordBoundary: false, // <-- NEW
});
table.push(['Wrap', 'Text']);
console.log(table.toString());

Outputs:

┌───┬───┐
│ W │ T │
│ r │ e │
│ a │ x │
│ p │ t │
└───┴───┘

Noticing "wrap" is a popular search on this repository, I also added an anchor-link in the readme to the word wrap options and improved the tests/docs for the wordWrap option as well.

No unit tests were added for Cell but it gets tested in the example tests. Cell passes on the wrapOnWordBoundary table option, defaulting to true—so as not to incur a "breaking change" to the API.

@speedytwenty speedytwenty merged commit 3c893d5 into cli-table:master Mar 31, 2022
@speedytwenty speedytwenty deleted the wrap-words branch March 31, 2022 21:22
@speedytwenty speedytwenty changed the title [Feature] - Alternative word wrap option Add wrapOnWordBoundary option for consistently readable word wrapping Apr 5, 2022
@speedytwenty speedytwenty changed the title Add wrapOnWordBoundary option for consistently readable word wrapping Add wrapOnWordBoundary option for consistently readable word wrapping (#125) Apr 5, 2022
@speedytwenty speedytwenty changed the title Add wrapOnWordBoundary option for consistently readable word wrapping (#125) Add wrapOnWordBoundary option for consistently readable word wrapping Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to set wordWrap: break-word to always wrap newlines at arbitrary break points
1 participant