Skip to content

Commit 3ee1c56

Browse files
committed
Merge pull request #12 from takamin/useColor
Involving issue 11: escape sequences in a text are not counted for column width
2 parents b5e016b + cd0c5b1 commit 3ee1c56

File tree

3 files changed

+87
-27
lines changed

3 files changed

+87
-27
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-21
Original file line numberDiff line numberDiff line change
@@ -1,23 +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-
"chai": "^3.5.0"
22-
}
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+
}
2334
}

test/test.js

+45-3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
var chai = require('chai');
55
var should = chai.should();
66
var assert = chai.assert;
7+
var ansi = require('ansi-escape-sequences');
8+
var style = {};
9+
var fg = {};
10+
var bg = {};
11+
Object.keys(ansi.style).forEach(function(key) {
12+
var value = ansi.style[key];
13+
if(/^bg-/.test(key)) {
14+
bg[key.substr(3)] = value;
15+
} else {
16+
fg[key] = value;
17+
}
18+
});
19+
Object.keys(fg).forEach(function(key) {
20+
if( !(key in bg) ) {
21+
style[key] = fg[key];
22+
delete fg[key];
23+
}
24+
});
725
var col = 0;
826
var logbuf = listit.buffer();
927
process.on("exit", function() {
@@ -15,16 +33,25 @@
1533
func();
1634
titles = titles.slice(0, titles.length - 1);
1735
}
36+
function styled(message, styles) {
37+
var args = Array.apply(null, arguments);
38+
styles = args.slice(1);
39+
styles.push(message, ansi.style.reset);
40+
return styles.join('');
41+
}
1842
function it(name, test) {
1943
logbuf.d(titles.join(" ") + " " + name);
2044
try {
2145
test();
22-
logbuf.d("OK");
46+
logbuf.d(styled(" PASS ",
47+
bg.green, fg.white));
2348
logbuf.nl();
2449
} catch(ex) {
25-
logbuf.d("FAIL");
50+
logbuf.d(styled(" FAIL ",
51+
bg.red, fg.white, style.bold));
2652
logbuf.nl();
2753
test();
54+
console.error(ex);
2855
}
2956
}
3057
describe("listit.buffer", function() {
@@ -223,7 +250,7 @@
223250
"C 333.3 D -4.444");
224251
});
225252
});
226-
describe("bidimentional data", function() {
253+
describe("bidimensional data", function() {
227254
it("no autoAlign", function() {
228255
var buffer = new listit.buffer();
229256
buffer.d([
@@ -247,4 +274,19 @@
247274
});
248275
});
249276
});
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+
});
250292
}());

0 commit comments

Comments
 (0)