Skip to content

Commit bd39ed4

Browse files
committed
Merge pull request #15 from takamin/fix-the-table-to-be-broken-by-japanese-chars
Fix a bug that table was broken by wide-chars
2 parents 669a6d2 + 1c46d75 commit bd39ed4

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

README.md

+49
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,17 @@ You can put it to the console or a preformated text-file.
1212

1313
When a autoAlign option is set, the numbers are aligned by its fraction point.
1414

15+
CHANGES
16+
-------
17+
18+
* v0.3.3 - Bug fix : Measure a width of wide-chars of east asian characters correctly
19+
by using the npm eastasianwidth.
20+
* v0.3.2 - Change Test : Support mocha to test.
21+
* v0.3.1 - Bug fix : Do not count escape sequences in data for column width.
22+
* v0.3.0 - Enhance : Several columns or rows can be added at a time.
23+
* v0.2.0 - Enhance : Auto align mode is available.
24+
* v0.1.0 - Initial release
25+
1526
SAMPLE
1627
------
1728

@@ -92,6 +103,44 @@ NEPTUNE 102.0 49528 1638 11.0 23.5 16.1
92103
PLUTO 0.0146 2370 2095 0.7 1.3 -153.3
93104
```
94105

106+
### East asian characters
107+
108+
__japanese-foods-jp.js__
109+
110+
```
111+
var listit = require("list-it");
112+
var buf = listit.buffer();
113+
console.log(
114+
buf
115+
.d("1").d("寿司")
116+
.d("酢とご飯とシーフード")
117+
.d("健康的だ").nl()
118+
.d("2").d("焼肉")
119+
.d("日本のグリルされたお肉")
120+
.d("ジューシー").nl()
121+
.d("3").d("ラーメン")
122+
.d("日本のスープに入った麺")
123+
.d("大好き").nl()
124+
.d("4").d("天ぷら")
125+
.d("シーフードや野菜に衣をつけて揚げたもの")
126+
.d("おいしー").nl()
127+
.d("5").d("刺身")
128+
.d("大変フレッシュな魚のスライス")
129+
.d("食べてみて!あご落ちるぜ").nl()
130+
.toString());
131+
```
132+
133+
outputs:
134+
135+
```
136+
$ node sample/japanese-food-jp.js
137+
1 寿司 酢とご飯とシーフード 健康的だ
138+
2 焼肉 日本のグリルされたお肉 ジューシー
139+
3 ラーメン 日本のスープに入った麺 大好き
140+
4 天ぷら シーフードや野菜に衣をつけて揚げたもの おいしー
141+
5 刺身 大変フレッシュな魚のスライス 食べてみて!あご落ちるぜ
142+
```
143+
95144

96145
METHODS
97146
-------

lib/index.js

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(function() {
22
"use strict";
3+
var eaw = require('eastasianwidth');
34
var exports = {};
45
var privates = {};
56

@@ -130,13 +131,18 @@
130131
DataCell.prototype.setData = function(data) {
131132
this.data = data;
132133
};
134+
133135
DataCell.prototype.visibleLength = function() {
134136
if(typeof(this.data) != "string") {
135137
return ("" + this.data).length;
136138
}
139+
return DataCell.visibleLength(this.data);
140+
};
141+
142+
DataCell.visibleLength = function(data) {
137143
// Remove escape sequences for text style from the string
138-
var s = this.data.replace(/\x1b[^m]*m/g, '');
139-
return s.length;
144+
var s = data.replace(/\x1b[^m]*m/g, '');
145+
return eaw.length(s);
140146
};
141147

142148
//
@@ -181,7 +187,7 @@
181187
}
182188
}
183189
var s = "" + data;
184-
while(s.length < m) {
190+
while(DataCell.visibleLength(s) < m) {
185191
s += ' ';
186192
}
187193
return s;

package.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "list-it",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"description": "This module is used to create a preformatted text table.",
55
"repository": {
66
"type": "git",
@@ -29,6 +29,7 @@
2929
"chai": "^3.5.0"
3030
},
3131
"dependencies": {
32-
"ansi-escape-sequences": "^2.2.2"
32+
"ansi-escape-sequences": "^2.2.2",
33+
"eastasianwidth": "^0.1.1"
3334
}
3435
}

sample/japanese-food-jp.js

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
var listit = require("../lib");
2+
var buf = listit.buffer();
3+
console.log(
4+
buf
5+
.d("1").d("寿司")
6+
.d("酢とご飯とシーフード")
7+
.d("健康的だ").nl()
8+
.d("2").d("焼肉")
9+
.d("日本のグリルされたお肉")
10+
.d("ジューシー").nl()
11+
.d("3").d("ラーメン")
12+
.d("日本のスープに入った麺")
13+
.d("大好き").nl()
14+
.d("4").d("天ぷら")
15+
.d("シーフードや野菜に衣をつけて揚げたもの")
16+
.d("おいしー").nl()
17+
.d("5").d("刺身")
18+
.d("大変フレッシュな魚のスライス")
19+
.d("食べてみて!あご落ちるぜ").nl()
20+
.toString());

test/test.js

+17
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,21 @@
302302
});
303303
});
304304
});
305+
describe("Measure a width of wide-chars correctly.", function() {
306+
it("All Japanese", function() {
307+
var buffer = new listit.buffer();
308+
buffer
309+
.d("1").d("寿司").d("酢とご飯とシーフード").d("健康的だ").nl()
310+
.d("2").d("焼肉").d("日本のグリルされたお肉").d("ジューシー").nl()
311+
.d("3").d("ラーメン").d("日本のスープに入った麺").d("大好き").nl()
312+
.d("4").d("天ぷら").d("シーフードや野菜に衣をつけて揚げたもの").d("おいしー").nl()
313+
.d("5").d("刺身").d("大変フレッシュな魚のスライス").d("食べてみて!あご落ちるぜ").nl();
314+
assert.equal(buffer.toString(),
315+
"1 寿司 酢とご飯とシーフード 健康的だ \n" +
316+
"2 焼肉 日本のグリルされたお肉 ジューシー \n" +
317+
"3 ラーメン 日本のスープに入った麺 大好き \n" +
318+
"4 天ぷら シーフードや野菜に衣をつけて揚げたもの おいしー \n" +
319+
"5 刺身 大変フレッシュな魚のスライス 食べてみて!あご落ちるぜ");
320+
});
321+
});
305322
}((typeof(describe) == "function") ? describe : false, (typeof(it) == "function") ? it : false));

0 commit comments

Comments
 (0)