Skip to content

Commit

Permalink
Add option to disable wrapping text
Browse files Browse the repository at this point in the history
When width is set, cell text is always wrapped. In some cases this is
undesirable. For example adding formating that will shrink the effective
size of the cell. see gajus#117
  • Loading branch information
IanEdington committed Jan 3, 2021
1 parent 91a462e commit fb30b3b
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 9 deletions.
6 changes: 3 additions & 3 deletions src/alignString.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ const alignCenter = (subject, width) => {
* @param {string} alignment One of the valid options (left, right, center).
* @returns {string}
*/
export default (subject, containerWidth, alignment) => {
export default (subject, containerWidth, alignment, disableWrap) => {
if (!_.isString(subject)) {
throw new TypeError('Subject parameter value must be a string.');
}
Expand All @@ -64,7 +64,7 @@ export default (subject, containerWidth, alignment) => {

const subjectWidth = stringWidth(subject);

if (subjectWidth > containerWidth) {
if (!disableWrap && subjectWidth > containerWidth) {
// console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);

throw new Error('Subject parameter value width cannot be greater than the container width.');
Expand All @@ -82,7 +82,7 @@ export default (subject, containerWidth, alignment) => {
return ' '.repeat(containerWidth);
}

const availableWidth = containerWidth - subjectWidth;
const availableWidth = Math.max(containerWidth - subjectWidth, 0);

if (alignment === 'left') {
return alignLeft(subject, availableWidth);
Expand Down
2 changes: 1 addition & 1 deletion src/alignTableData.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export default (rows, config) => {
if (stringWidth(value) === column.width) {
return value;
} else {
return alignString(value, column.width, column.alignment);
return alignString(value, column.width, column.alignment, column.disableWrap);
}
});
});
Expand Down
5 changes: 3 additions & 2 deletions src/calculateCellHeight.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import wrapCell from './wrapCell';
* @param {string} value
* @param {number} columnWidth
* @param {boolean} useWrapWord
* @param {boolean} disableWrap
* @returns {number}
*/
export default (value, columnWidth, useWrapWord = false) => {
export default (value, columnWidth, useWrapWord = false, disableWrap = false) => {
if (!_.isString(value)) {
throw new TypeError('Value must be a string.');
}
Expand All @@ -20,5 +21,5 @@ export default (value, columnWidth, useWrapWord = false) => {
throw new Error('Column width must be greater than 0.');
}

return wrapCell(value, columnWidth, useWrapWord).length;
return wrapCell(value, columnWidth, useWrapWord, disableWrap).length;
};
10 changes: 9 additions & 1 deletion src/calculateRowHeightIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ export default (rows, config) => {
if (!_.isBoolean(config.columns[index1].wrapWord)) {
throw new TypeError('column[index].wrapWord must be a boolean.');
}
if (!_.isBoolean(config.columns[index1].disableWrap)) {
throw new TypeError('column[index].disableWrap must be a boolean.');
}

cellHeightIndex[index1] = calculateCellHeight(value, config.columns[index1].width, config.columns[index1].wrapWord);
cellHeightIndex[index1] = calculateCellHeight(
value,
config.columns[index1].width,
config.columns[index1].wrapWord,
config.columns[index1].disableWrap
);
});

rowSpanIndex.push(_.max(cellHeightIndex));
Expand Down
1 change: 1 addition & 0 deletions src/makeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const makeColumns = (rows, columns = {}, columnDefault = {}) => {
truncate: Number.POSITIVE_INFINITY,
width: maximumColumnWidthIndex[index],
wrapWord: false,
disableWrap: false
}, columnDefault, columns[index]);
});

Expand Down
1 change: 1 addition & 0 deletions src/makeStreamConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
paddingRight: 1,
truncate: Number.POSITIVE_INFINITY,
wrapWord: false,
disableWrap: false
}, columnDefault, columns[index]);
});

Expand Down
7 changes: 6 additions & 1 deletion src/mapDataUsingRowHeightIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ export default (unmappedRows, rowHeightIndex, config) => {
// [{cell index within a virtual row; index1}]

cells.forEach((value, index1) => {
const cellLines = wrapCell(value, config.columns[index1].width, config.columns[index1].wrapWord);
const cellLines = wrapCell(
value,
config.columns[index1].width,
config.columns[index1].wrapWord,
config.columns[index1].disableWrap
);

cellLines.forEach((cellLine, index2) => {
rowHeight[index2][index1] = cellLine;
Expand Down
1 change: 1 addition & 0 deletions src/table.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import validateTableData from './validateTableData';
* @property {number} width Column width (default: auto).
* @property {number} truncate Number of characters are which the content will be truncated (default: Infinity).
* @property {boolean} wrapWord When true the text is broken at the nearest space or one of the special characters
* @property {boolean} disableWrap When true the text will not wrap based on width
* @property {number} paddingLeft Cell content padding width left (default: 1).
* @property {number} paddingRight Cell content padding width right (default: 1).
*/
Expand Down
8 changes: 7 additions & 1 deletion src/wrapCell.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ import wrapWord from './wrapWord';
* @param {string} cellValue
* @param {number} columnWidth
* @param {boolean} useWrapWord
* @param {boolean} disableWrap
* @returns {Array}
*/
export default (cellValue, columnWidth, useWrapWord) => {
export default (cellValue, columnWidth, useWrapWord, disableWrap) => {
// First split on literal newlines
const cellLines = cellValue.split('\n');

// early exit if wrapping of words is disabled
if (disableWrap) {
return cellLines;
}

// Then iterate over the list and word-wrap every remaining line if necessary.
for (let lineNr = 0; lineNr < cellLines.length;) {
let lineChunks;
Expand Down

0 comments on commit fb30b3b

Please sign in to comment.