Skip to content

Commit 83356da

Browse files
committed
added example for the table formatter
1 parent cff1768 commit 83356da

2 files changed

Lines changed: 64 additions & 1 deletion

File tree

examples/table.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace splitbrain\phpcli\examples;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
use splitbrain\phpcli\CLI;
8+
use splitbrain\phpcli\Colors;
9+
use splitbrain\phpcli\Options;
10+
use splitbrain\phpcli\TableFormatter;
11+
12+
class Table extends CLI
13+
{
14+
15+
/**
16+
* Register options and arguments on the given $options object
17+
*
18+
* @param Options $options
19+
* @return void
20+
*/
21+
protected function setup(Options $options)
22+
{
23+
$options->setHelp('This shows how the table formatter works by printing the current php.ini values');
24+
}
25+
26+
/**
27+
* Your main program
28+
*
29+
* Arguments and options have been parsed when this is run
30+
*
31+
* @param Options $options
32+
* @return void
33+
*/
34+
protected function main(Options $options)
35+
{
36+
$tf = new TableFormatter($this->colors);
37+
$tf->setBorder(' | '); // nice border between colmns
38+
39+
// show a header
40+
echo $tf->format(
41+
array('*', '30%', '30%'),
42+
array('ini setting', 'global', 'local')
43+
);
44+
45+
// a line across the whole width
46+
echo str_pad('', $tf->getMaxWidth(), '-') . "\n";
47+
48+
// colored columns
49+
$ini = ini_get_all();
50+
foreach ($ini as $val => $opts) {
51+
echo $tf->format(
52+
array('*', '30%', '30%'),
53+
array($val, $opts['global_value'], $opts['local_value']),
54+
array(Colors::C_CYAN, Colors::C_RED, Colors::C_GREEN)
55+
);
56+
}
57+
}
58+
}
59+
60+
$cli = new Table();
61+
$cli->run();

src/TableFormatter.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
class TableFormatter
66
{
7-
7+
/** @var string border between columns */
88
protected $border = ' ';
99

10+
/** @var int the terminal width */
1011
protected $max = 74;
1112

13+
/** @var Colors for coloring output */
1214
protected $colors;
1315

1416
/**

0 commit comments

Comments
 (0)