Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[minor] fixed the length calculation if colors are used on input

  • Loading branch information...
commit 48df3c130b7a440594a195a2df0e1fee421cfc65 1 parent e34d6b4
nicoreed authored
Showing with 18 additions and 4 deletions.
  1. +18 −4 lib/cliff.js
View
22 lib/cliff.js
@@ -72,11 +72,10 @@ cliff.columnMajor = function (rows) {
cliff.arrayLengths = function (arrs) {
var i, lengths = [];
for (i = 0; i < arrs.length; i += 1) {
- lengths.push(winston.longestElement(arrs[i].map(function(elem) {
+ lengths.push(longestElement(arrs[i].map(function(elem) {
return cliff.stringifyLiteral(elem);
})));
}
-
return lengths;
};
@@ -98,7 +97,7 @@ cliff.stringifyRows = function (rows, colors) {
for (i = 0; i < row.length; i += 1) {
item = cliff.stringifyLiteral(row[i]);
item = colorize ? item[colors[i]] : item;
- length = item.length;
+ length = real_length(item);
padding = length < lengths[i] ? lengths[i] - length + 2 : 2;
rowtext += item + new Array(padding).join(' ');
}
@@ -241,4 +240,19 @@ cliff.typeOf = function typeOf (value) {
}
return s;
-};
+}
+
+var real_length = function (str) {
+ return ("" + str).replace(/\u001b\[\d+m/g,'').length;
+}
+
+var longestElement = function (a) {
+ var l = 0;
+ for (var i = 0; i < a.length; i++) {
+ var new_l = real_length(a[i]);
+ if (l < new_l) {
+ l = new_l;
+ }
+ }
+ return l;
+}
Please sign in to comment.
Something went wrong with that request. Please try again.