Comparing version 0.3.1 to 0.3.2
{ | ||
"name": "tabacol", | ||
"version": "0.3.1", | ||
"version": "0.3.2", | ||
"description": "Output a table in string format from data in JSON format", | ||
@@ -23,4 +23,6 @@ "main": "index.js", | ||
"ansi-256-colors": "^1.1.0", | ||
"ansi-regex": "^3.0.0", | ||
"ansi-styles": "^2.2.1", | ||
"chalk": "^2.1.0" | ||
"chalk": "^2.1.0", | ||
"strip-ansi": "^4.0.0" | ||
}, | ||
@@ -27,0 +29,0 @@ "devDependencies": { |
@@ -0,1 +1,3 @@ | ||
const stripAnsi = require('strip-ansi'); | ||
/** | ||
@@ -14,3 +16,6 @@ * Return the final width to use for each column | ||
const longestContent = prd.map(c => c.reduce((t, l) => { | ||
if (l.length > t) return l.length; | ||
// clean the string from ansi code that should not be taken into account to | ||
// compute the lenght of the content | ||
const cleanString = stripAnsi(l); | ||
if (cleanString.length > t) return cleanString.length; | ||
return t; | ||
@@ -17,0 +22,0 @@ }, 0)); |
@@ -0,1 +1,4 @@ | ||
const stripAnsi = require('strip-ansi'); | ||
const ansiRegex = require('ansi-regex'); | ||
/** | ||
@@ -8,3 +11,4 @@ * Pad a string on the right with spaces | ||
const padStringRight = (str, lgt) => { | ||
const r = str.length >= lgt ? 0 : lgt - str.length; | ||
const cleanStr = stripAnsi(str); | ||
const r = cleanStr.length >= lgt ? 0 : lgt - cleanStr.length; | ||
return `${str}${' '.repeat(r)}`; | ||
@@ -20,3 +24,4 @@ }; | ||
const padStringLeft = (str, lgt) => { | ||
const r = str.length >= lgt ? 0 : lgt - str.length; | ||
const cleanStr = stripAnsi(str); | ||
const r = cleanStr.length >= lgt ? 0 : lgt - cleanStr.length; | ||
return `${' '.repeat(r)}${str}`; | ||
@@ -48,10 +53,29 @@ }; | ||
/** | ||
* Truncate a string so that it has the correct length and add ellipsis | ||
* Truncate a string with potential ANSI codes so the content has correct length and add ellipsis | ||
* | ||
* @param {string} str The string to truncate | ||
* @param {uint} lgt Maximum length of the final string | ||
* @param {number} lgt Maximum length of the content on the final string | ||
* @returns {string} The truncated string | ||
*/ | ||
const truncateString = (str, lgt) => { | ||
if (str.length <= lgt) return str; | ||
else if (str.length > lgt) return `${str.slice(0, lgt - 1)}…`; | ||
const truncateStringKeepAnsi = (str, lgt) => { | ||
const cleanStr = stripAnsi(str); | ||
// if the content of the string fits the max size, return as is | ||
if (cleanStr.length <= lgt) return str; | ||
// if the string is longer, truncate it and keeping the potential ansi codes on it | ||
else if (cleanStr.length > lgt) { | ||
const newCleanStr = `${cleanStr.slice(0, lgt - 1)}…`; | ||
// try to get ansi codes from the original string and if there are some, reput them | ||
const ansiCodes = str.match(ansiRegex()); | ||
if (ansiCodes) { | ||
if (ansiCodes.length === 2) return `${ansiCodes[0]}${newCleanStr}${ansiCodes[1]}`; | ||
} | ||
// if there was no ansi codes, we just return the truncated string | ||
return newCleanStr; | ||
} | ||
// if we can't process the string at all | ||
return 'Not a string'; | ||
@@ -65,3 +89,3 @@ }; | ||
getArray0N, | ||
truncateString, | ||
truncateStringKeepAnsi, | ||
}; |
const chalk = require('chalk'); | ||
const { | ||
truncateString, | ||
truncateStringKeepAnsi, | ||
padStringLeft, | ||
@@ -42,3 +42,3 @@ padStringRight, | ||
// truncate the content of the cell if necessary | ||
let cell = truncateString(content, fw); | ||
let cell = truncateStringKeepAnsi(content, fw); | ||
@@ -45,0 +45,0 @@ let textAlign = str => padStringRight(str, fw); |
const chai = require('chai'); | ||
const chalk = require('chalk'); | ||
const compute = require('../src/compute'); | ||
@@ -7,3 +8,3 @@ | ||
const testData1 = [ | ||
['line1col1 12', 'line2col1', 'line3col1'], | ||
[chalk.blue('line1col1 12'), 'line2col1', 'line3col1'], | ||
['line1col2 yeah you know !', 'line2col2', 'line3col2'], | ||
@@ -10,0 +11,0 @@ ['l1- c3', 'l2 -c2', 'l3 -c3'], |
@@ -97,3 +97,3 @@ const chai = require('chai'); | ||
it('should return the same string as input', (done) => { | ||
expect(helper.truncateString('supercoolstring', 30)).to.equal('supercoolstring'); | ||
expect(helper.truncateStringKeepAnsi('supercoolstring', 30)).to.equal('supercoolstring'); | ||
done(); | ||
@@ -103,3 +103,3 @@ }); | ||
it('should return the same string as input', (done) => { | ||
expect(helper.truncateString('supercoolstring', 15)).to.equal('supercoolstring'); | ||
expect(helper.truncateStringKeepAnsi('supercoolstring', 15)).to.equal('supercoolstring'); | ||
done(); | ||
@@ -109,3 +109,3 @@ }); | ||
it('should return a truncated string', (done) => { | ||
expect(helper.truncateString('supercoolstring', 8)).to.equal('superco…'); | ||
expect(helper.truncateStringKeepAnsi('supercoolstring', 8)).to.equal('superco…'); | ||
done(); | ||
@@ -115,5 +115,5 @@ }); | ||
it('should return an error string', (done) => { | ||
expect(helper.truncateString(12, 8)).to.equal('Not a string'); | ||
expect(helper.truncateStringKeepAnsi(12, 8)).to.equal('Not a string'); | ||
done(); | ||
}); | ||
}); |
const chai = require('chai'); | ||
const chalk = require('chalk'); | ||
const tabacol = require('../index'); | ||
@@ -9,3 +10,3 @@ | ||
['row1 col0', 'row1 col1', 'row1 col2'], | ||
['row2 col0', 'row2 col1', 'row2 col2'], | ||
['row2 col0', chalk.blue.bgGreen.bold('row3 col1'), 'row2 col2'], | ||
['row3 col0', 'row3 col1', 'row3 col2'], | ||
@@ -12,0 +13,0 @@ ]; |
37042
933
5
+ Addedansi-regex@^3.0.0
+ Addedstrip-ansi@^4.0.0
+ Addedansi-regex@3.0.1(transitive)
+ Addedstrip-ansi@4.0.0(transitive)