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 Aug 12, 2020
1 parent fc2c35b commit 3cb6b38
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/alignString.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export default (subject, containerWidth, alignment) => {
if (subjectWidth > containerWidth) {
// console.log('subjectWidth', subjectWidth, 'containerWidth', containerWidth, 'subject', subject);

throw new Error('Subject parameter value width cannot be greater than the container width.');
// throw new Error('Subject parameter value width cannot be greater than the container width.');
}

if (!_.isString(alignment)) {
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
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 = true) => {
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;
};
5 changes: 4 additions & 1 deletion src/calculateRowHeightIndex.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ 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
3 changes: 2 additions & 1 deletion src/makeConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ const makeColumns = (rows, columns = {}, columnDefault = {}) => {
paddingRight: 1,
truncate: Infinity,
width: maximumColumnWidthIndex[index],
wrapWord: false
wrapWord: false,
disableWrap: false,
}, columnDefault, columns[index]);
});

Expand Down
3 changes: 2 additions & 1 deletion src/makeStreamConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const makeColumns = (columnCount, columns = {}, columnDefault = {}) => {
paddingLeft: 1,
paddingRight: 1,
truncate: Infinity,
wrapWord: false
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
3 changes: 3 additions & 0 deletions src/schemas/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"wrapWord": {
"type": "boolean"
},
"disableWrap": {
"type": "boolean"
},
"truncate": {
"type": "number"
},
Expand Down
3 changes: 3 additions & 0 deletions src/schemas/streamConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@
"wrapWord": {
"type": "boolean"
},
"disableWrap": {
"type": "boolean"
},
"truncate": {
"type": "number"
},
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 truncateTableData from './truncateTableData';
* @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 not wrapped 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 3cb6b38

Please sign in to comment.