Table layout breaks when added rows contain unicode characters:
$ cat tab.pl
#!/usr/bin/perl
use strict;
use warnings;
use Text::ASCIITable;
my $t = Text::ASCIITable->new({ headingText => 'test table' });
$t->setCols('Id','Name','Price');
$t->addRow(1,'黒黒黒黒黒黒黒',24.4);
$t->addRow(2,'ラララララ', 5.2);
$t->addRow(3,'実行実行実行',12.3);
$t->addRowLine();
$t->addRow('','Total',57.9);
print $t;
$ ./tab.pl
.----------------------.
| test table |
+----+---------+-------+
| Id | Name | Price |
+----+---------+-------+
| 1 | 黒黒黒黒黒黒黒 | 24.4 |
| 2 | ラララララ | 5.2 |
| 3 | 実行実行実行 | 12.3 |
+----+---------+-------+
| | Total | 57.9 |
'----+---------+-------'
It can be fixed if instead of length() Text::ASCIITable::count() used
columns() from Unicode::GCString
Simple proof of concept:
$ cat tab.pl
#!/usr/bin/perl
use strict;
use warnings;
use Text::ASCIITable;
use Unicode::GCString;
{no warnings 'redefine';
sub Text::ASCIITable::count
{
my ($self, $str) = @_;
my $gc = Unicode::GCString->new(Encode::decode('UTF-8',$str));
return $gc->columns;
}}
my $t = Text::ASCIITable->new({ headingText => 'test table' });
$t->setCols('Id','Name','Price');
$t->addRow(1,'黒黒黒黒黒黒黒',24.4);
$t->addRow(2,'ラララララ', 5.2);
$t->addRow(3,'実行実行実行',12.3);
$t->addRowLine();
$t->addRow('','Total',57.9);
print $t;
$ ./tab.pl
.-----------------------------.
| test table |
+----+----------------+-------+
| Id | Name | Price |
+----+----------------+-------+
| 1 | 黒黒黒黒黒黒黒 | 24.4 |
| 2 | ラララララ | 5.2 |
| 3 | 実行実行実行 | 12.3 |
+----+----------------+-------+
| | Total | 57.9 |
'----+----------------+-------'
Table looks correct in terminal.