4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

node.js で コンソール上での文字列の表示幅を得る方法。

Posted at

Plain-Text で、日本語を含む情報を整形表示しようとすると、
文字列数でなく、文字列表示幅 (全角文字なら2,半角文字なら1) の情報が必要になります。

現時点で使えそうなのは
  eastasianwidth  https://github.com/komagata/eastasianwidth
  unicode-eastasianwidt https://github.com/MiCHiLU/Unicode-EastAsianWidth-JS
があるようです。

もっとよいライブラリーを知っている方がいれば、教えていただけば幸いです。

提案:

このような日本語に特化したライブライー群のためのレポジトリーを qiita で用意し、qiita ユーザーで発展させるとよいのでは?
// 日本語を含む国際化対応は、日本人が頑張らないと...

経緯:

plain-text での Table を 出力する cli-table (0.0.1) は日本語に対応していなかった。

日本語をつかった例をテストコード ( https://github.com/LearnBoost/cli-table/blob/master/test/index.test.js ) に追加してみた。
現時点でテストをパスさせるには次のようになる。

'test KANJI': function() {
var table = new Table({ head: ["", "Header 1", "Header 2"], style: {'padding-left':0, 'padding-right':0, head:[], border:[]} }); // clear styles to prevent color output
table.push(
{"Header 3": ['v0.1', '日本語 '] }
, {"Header 4": ['v0.1', 'Japanese'] }
);

var expected = [
    '┌────────┬────────┬────────┐'
  , '│        │Header 1│Header 2│'
  , '├────────┼────────┼────────┤'
  , '│Header 3│v0.1    │日本語     │'
  , '├────────┼────────┼────────┤'
  , '│Header 4│v0.1    │Japanese│'
  , '└────────┴────────┴────────┘'
];

table.toString().should.eql(expected.join("\n"));

},

"日本語" のセルの幅が広くなっている。
cli-table は 文字数 == 表示幅の仮定で動作していると思われる。

パッチを当てるには Console で表示させた時の文字列幅を計算する関数が必要になる。
そこで利用できそうなライブラリーを探してみた。

$ npm search width
NAME                  DESCRIPTION                                                   AUTHOR         DATE              VERSION KEYWORDS
bandwidth             Node.js module which provides an Bandwidth API client         =scottbarstow  2013-04-03 16:07  0.0.5  catapult sms api voip api
buffer-more-ints      Add support for more integer widths to Buffer                 =dwragg        2012-09-24 21:00  0.0.1
char-size             return the size in pixels of a single character               =substack      2013-03-27 03:05  0.0.0  html css browser browserify
connect-ratelimit     connect middleware for ratelimiting clients                   =dharmafly     2013-01-08 09:15  0.0.5  connect middleware ip limit
eastasianwidth        Get East Asian Width from a character.                        =komagata      2013-03-30 08:27  0.0.1
eighty                utility for working with text in a fixed width buffer (like a console) =jden 2012-11-26 10:44  0.0.1  console text cli
jaCodeMap             japanese unicode full/half width code mapping library         =mah0x211      2011-03-13 14:14  0.0.4  japanese unicode converter
udp-rpc               A simple UDP-based RPC server built on Node.js for low-bandwidth IPC. =dfellis 2012-04-14 21:21  0.0.4
unicode-eastasianwidth Counting the unicode characters based Unicode East Asian Width database. It includes some tools for dealing with characters. =mi
visualwidth           unicode character visual width                                =tokuhirom     2012-08-02 13:44  0.0.1  unicode visualwidth
vnstat.js             A simple Web interface to display statistics extracted from an existing VnStat database =nicolargo 2011-09-25 16:29  0.1.0  monit
wcwidth.js            A JavaScript porting of C's wcwidth() and wcswidth()          =mycoboco      2012-12-06 06:59  0.0.2  wide character wc wide char
xkcd-js               Random XKCD Comic Generator based on width/height requirements =myoung34     2012-03-18 19:49  0.0.2

zws a library encode/decode text using zero width spaces =youpy 2012-08-13 18:08 0.1.0

これらのなかで使えそうなのは
eastasianwidth  https://github.com/komagata/eastasianwidth
unicode-eastasianwidt https://github.com/MiCHiLU/Unicode-EastAsianWidth-JS

4
5
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?