Skip to content

Commit cd0c5b1

Browse files
committed
Do not count escape sequence for column width
1 parent e0d4e8f commit cd0c5b1

File tree

3 files changed

+58
-26
lines changed

3 files changed

+58
-26
lines changed

lib/index.js

+10-3
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,14 @@
130130
DataCell.prototype.setData = function(data) {
131131
this.data = data;
132132
};
133+
DataCell.prototype.visibleLength = function() {
134+
if(typeof(this.data) != "string") {
135+
return ("" + this.data).length;
136+
}
137+
// Remove escape sequences for text style from the string
138+
var s = this.data.replace(/\x1b[^m]*m/g, '');
139+
return s.length;
140+
};
133141

134142
//
135143
// Column class
@@ -147,11 +155,10 @@
147155
return this.width;
148156
};
149157
Column.prototype.setCellAt = function(row, cell) {
150-
var data = cell.getData();
151-
var s = "" + data;
152-
var width = s.length;
158+
var width = cell.visibleLength();
153159
this.cellAtRow[row] = cell;
154160
if(this.opt.autoAlign) {
161+
var data = cell.getData();
155162
if(typeof(data) == "number") {
156163
this.updateNumWidth(data);
157164
width = this.getNumMaxWidth();

package.json

+32-22
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,34 @@
11
{
2-
"name": "list-it",
3-
"version": "0.3.0",
4-
"description":
5-
"This module is used to create a preformatted text table.",
6-
"repository": {
7-
"type": "git",
8-
"url": "https://github.com/takamin/list-it"
9-
},
10-
"directories": {
11-
"lib": "./lib"
12-
},
13-
"main": "./lib/index.js",
14-
"scripts": {
15-
"test": "node ./test/test.js"
16-
},
17-
"keywords": [ "console", "pre", "text", "column", "table", "format", "cli" ],
18-
"author": "Koji Takami",
19-
"license": "MIT",
20-
"devDependencies": {
21-
"ansi-escape-sequences": "^2.2.2",
22-
"chai": "^3.5.0"
23-
}
2+
"name": "list-it",
3+
"version": "0.3.1",
4+
"description": "This module is used to create a preformatted text table.",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/takamin/list-it"
8+
},
9+
"directories": {
10+
"lib": "./lib"
11+
},
12+
"main": "./lib/index.js",
13+
"scripts": {
14+
"test": "node ./test/test.js"
15+
},
16+
"keywords": [
17+
"console",
18+
"pre",
19+
"text",
20+
"column",
21+
"table",
22+
"format",
23+
"cli"
24+
],
25+
"author": "Koji Takami",
26+
"license": "MIT",
27+
"devDependencies": {
28+
"ansi-escape-sequences": "^2.2.2",
29+
"chai": "^3.5.0"
30+
},
31+
"dependencies": {
32+
"ansi-escape-sequences": "^2.2.2"
33+
}
2434
}

test/test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@
250250
"C 333.3 D -4.444");
251251
});
252252
});
253-
describe("bidimentional data", function() {
253+
describe("bidimensional data", function() {
254254
it("no autoAlign", function() {
255255
var buffer = new listit.buffer();
256256
buffer.d([
@@ -274,4 +274,19 @@
274274
});
275275
});
276276
});
277+
describe("Do not count escape sequences for column width,", function() {
278+
Object.keys(ansi.style).forEach(function(key) {
279+
var style = ansi.style[key];
280+
it("style " + key + "#1", function() {
281+
var buffer = new listit.buffer();
282+
buffer.d([ styled("A", style), "B" ], ["C", "D"]);
283+
assert.equal(buffer.toString(), styled("A", style) + " B\n" + "C D");
284+
});
285+
it("style " + key + "#2", function() {
286+
var buffer = new listit.buffer();
287+
buffer.d([ "A", styled("B", style) ], ["C", "D"]);
288+
assert.equal(buffer.toString(), "A " + styled("B", style) + "\n" + "C D");
289+
});
290+
});
291+
});
277292
}());

0 commit comments

Comments
 (0)