diff --git a/src/alignString.js b/src/alignString.js index 3e24fb5..af5a6ee 100644 --- a/src/alignString.js +++ b/src/alignString.js @@ -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.'); } @@ -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.'); @@ -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); diff --git a/src/alignTableData.js b/src/alignTableData.js index 8a7be7a..fb6dbc1 100644 --- a/src/alignTableData.js +++ b/src/alignTableData.js @@ -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); } }); }); diff --git a/src/calculateCellHeight.js b/src/calculateCellHeight.js index 3eca8d7..9dfc9a0 100644 --- a/src/calculateCellHeight.js +++ b/src/calculateCellHeight.js @@ -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.'); } @@ -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; }; diff --git a/src/calculateRowHeightIndex.js b/src/calculateRowHeightIndex.js index 2687902..3d843e8 100644 --- a/src/calculateRowHeightIndex.js +++ b/src/calculateRowHeightIndex.js @@ -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)); diff --git a/src/makeConfig.js b/src/makeConfig.js index 1471afc..f2b46e1 100644 --- a/src/makeConfig.js +++ b/src/makeConfig.js @@ -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]); }); diff --git a/src/makeStreamConfig.js b/src/makeStreamConfig.js index 884625a..35fe5ae 100644 --- a/src/makeStreamConfig.js +++ b/src/makeStreamConfig.js @@ -32,7 +32,8 @@ const makeColumns = (columnCount, columns = {}, columnDefault = {}) => { paddingLeft: 1, paddingRight: 1, truncate: Infinity, - wrapWord: false + wrapWord: false, + disableWrap: false }, columnDefault, columns[index]); }); diff --git a/src/mapDataUsingRowHeightIndex.js b/src/mapDataUsingRowHeightIndex.js index 6562bdf..726a416 100644 --- a/src/mapDataUsingRowHeightIndex.js +++ b/src/mapDataUsingRowHeightIndex.js @@ -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; diff --git a/src/schemas/config.json b/src/schemas/config.json index 1a4a998..a728819 100644 --- a/src/schemas/config.json +++ b/src/schemas/config.json @@ -44,6 +44,9 @@ "wrapWord": { "type": "boolean" }, + "disableWrap": { + "type": "boolean" + }, "truncate": { "type": "number" }, diff --git a/src/schemas/streamConfig.json b/src/schemas/streamConfig.json index 3519984..5b76ca9 100644 --- a/src/schemas/streamConfig.json +++ b/src/schemas/streamConfig.json @@ -44,6 +44,9 @@ "wrapWord": { "type": "boolean" }, + "disableWrap": { + "type": "boolean" + }, "truncate": { "type": "number" }, diff --git a/src/table.js b/src/table.js index 86af744..7f82d22 100644 --- a/src/table.js +++ b/src/table.js @@ -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). */ diff --git a/src/wrapCell.js b/src/wrapCell.js index 0c66260..1d713cb 100644 --- a/src/wrapCell.js +++ b/src/wrapCell.js @@ -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;