console table
1.3.0
ConsoleTabeは、コンソールスタイルのテーブルを簡単に構築できます。ターミナル/シェルに表形式データを表示するのに役立ちます。これは、phplucidframeのコンポーネントです。
ライセンス:MIT
composer require phplucidframe/console-table
require 'src/LucidFrame/Console/ConsoleTable.php';
$table = new LucidFrameConsoleConsoleTable();
$table
->addHeader('Language')
->addHeader('Year')
->addRow()
->addColumn('PHP')
->addColumn(1994)
->addRow()
->addColumn('C++')
->addColumn(1983)
->addRow()
->addColumn('C')
->addColumn(1970)
->display()
;
echo $table->getTable();などのgetTableメソッドを使用してテーブルを印刷することもできます。
出力:
+----------+------+
| Language | Year |
+----------+------+
| PHP | 1994 |
| C++ | 1983 |
| C | 1970 |
+----------+------+
また、 setHeaders()およびaddRow配列で使用することもできます。
require 'src/LucidFrame/Console/ConsoleTable.php';
$table = new LucidFrameConsoleConsoleTable();
$table
->setHeaders(array('Language', 'Year'))
->addRow(array('PHP', 1994))
->addRow(array('C++', 1983))
->addRow(array('C', 1970))
->setPadding(2)
->display()
;
出力:
+------------+--------+
| Language | Year |
+------------+--------+
| PHP | 1994 |
| C++ | 1983 |
| C | 1970 |
+------------+--------+
require 'src/LucidFrame/Console/ConsoleTable.php';
$table = new LucidFrameConsoleConsoleTable();
$table
->setHeaders(array('Language', 'Year'))
->addRow(array('PHP', 1994))
->addRow(array('C++', 1983))
->addRow(array('C', 1970))
->setIndent(4)
->display()
;
出力:
+----------+------+
| Language | Year |
+----------+------+
| PHP | 1994 |
| C++ | 1983 |
| C | 1970 |
+----------+------+
require 'src/LucidFrame/Console/ConsoleTable.php';
$table = new LucidFrameConsoleConsoleTable();
$table
->setHeaders(array('Language', 'Year'))
->addRow(array('PHP', 1994))
->addRow(array('C++', 1983))
->addRow(array('C', 1970))
->hideBorder()
->display()
;
出力:
Language Year
----------------
PHP 1994
C++ 1983
C 1970
require 'src/LucidFrame/Console/ConsoleTable.php';
$table = new LucidFrameConsoleConsoleTable();
$table
->addRow(array('PHP', 1994))
->addRow(array('C++', 1983))
->addRow(array('C', 1970))
->hideBorder()
->display()
;
出力:
PHP 1994
C++ 1983
C 1970
require 'src/LucidFrame/Console/ConsoleTable.php';
$table = new LucidFrameConsoleConsoleTable();
$table
->setHeaders(array('Language', 'Year'))
->addRow(array('PHP', 1994))
->addRow(array('C++', 1983))
->addRow(array('C', 1970))
->showAllBorders()
->display()
;
または、各行にaddBorderLine()使用できます。
$table
->setHeaders(array('Language', 'Year'))
->addRow(array('PHP', 1994))
->addBorderLine()
->addRow(array('C++', 1983))
->addBorderLine()
->addRow(array('C', 1970))
->display()
;
出力
+----------+------+
| Language | Year |
+----------+------+
| PHP | 1994 |
+----------+------+
| C++ | 1983 |
+----------+------+
| C | 1970 |
+----------+------+
マシンにphpunitをインストールしている場合は、プロジェクトルートでテストを実行できます。
composer install
phpunit tests
phpunitをお持ちでない場合は、ターミナルでこれを単純に実行できます。
php example.php