Skip to content

Commit

Permalink
Handle custom colored strings correctly
Browse files Browse the repository at this point in the history
Currently strlen ignores any color code that makes use of ';', this
includes light colors as well as 256 color codes. This adjusts the regex
to detect these other color types.
  • Loading branch information
mal committed Jan 25, 2014
1 parent bf6a6c4 commit 16eb354
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/cli-table/utils.js
Expand Up @@ -77,7 +77,7 @@ exports.options = options;
// see: http://en.wikipedia.org/wiki/ANSI_escape_code
//
exports.strlen = function(str){
var code = /\u001b\[\d+m/g;
var code = /\u001b\[(?:\d*;){0,2}\d*m/g;
var stripped = ("" + str).replace(code,'');
var split = stripped.split("\n");
return split.reduce(function (memo, s) { return (s.length > memo) ? s.length : memo }, 0);
Expand Down
12 changes: 9 additions & 3 deletions test/index.test.js
Expand Up @@ -101,16 +101,22 @@ module.exports = {
style: {head: ['red'], border: ['grey']}
});

var off = '\u001b[39m'
, red = '\u001b[31m'
, orange = '\u001b[38;5;221m'
, grey = '\u001b[90m'

, c256s = orange + 'v0.1' + off;

table.push(
['v0.1', 'rauchg@gmail.com']
[c256s, 'rauchg@gmail.com']
);

var off = '\u001b[39m', red = '\u001b[31m', grey = '\u001b[90m';
var expected = [
grey + '┌──────┬──────────────────┐' + off
, grey + '│' + off + red + ' Rel ' + off + grey + '│' + off + red + ' By ' + off + grey + '│' + off
, grey + '├──────┼──────────────────┤' + off
, grey + '│' + off + ' v0.1 ' + grey + '│' + off + ' rauchg@gmail.com ' + grey + '│' + off
, grey + '│' + off + ' ' + c256s + ' ' + grey + '│' + off + ' rauchg@gmail.com ' + grey + '│' + off
, grey + '└──────┴──────────────────┘' + off
];

Expand Down

0 comments on commit 16eb354

Please sign in to comment.