Permalink
Browse files

merged branch umpirsky/console-helper-table (PR #6368)

This PR was squashed before being merged into the master branch (closes #6368).

Discussion
----------

[2.3] [Console] TableHelper

When building a console application it may be useful to display tabular data.

`TableHelper` can display table header and rows, customizable alignment of columns, cell padding and colors.

Basic usage example:
```php
$table = $app->getHelperSet()->get('table');
$table
    ->setHeaders(array('ISBN', 'Title', 'Author'))
    ->setRows(array(
        array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'),
        array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'),
        array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'),
        array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'),
    ))
;
$table->render($output);
```
Output:
![table](https://f.cloud.github.com/assets/208957/14955/6fb4f500-46ca-11e2-8435-0f6b22f96e58.png)

If this PR gets merged I will submit doc PR as well.

I'm sure there is a plenty of room for improvements so any feedback is welcome.

Commits
-------

8de7813 [2.3] [Console] TableHelper
  • Loading branch information...
fabpot committed Apr 7, 2013
2 parents a7c9863 + 8de7813 commit 22adfdf504e1c9cf605738ffc3c350ec68b3d1b6
@@ -27,6 +27,7 @@
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Console\Helper\DialogHelper;
use Symfony\Component\Console\Helper\ProgressHelper;
+use Symfony\Component\Console\Helper\TableHelper;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Event\ConsoleForExceptionEvent;
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
@@ -1013,6 +1014,7 @@ protected function getDefaultHelperSet()
new FormatterHelper(),
new DialogHelper(),
new ProgressHelper(),
+ new TableHelper(),
));
}
@@ -4,6 +4,7 @@ CHANGELOG
2.3.0
-----
+ * added Table Helper for tabular data rendering
* added support for events in `Application`
* added a way to normalize EOLs in `ApplicationTester::getDisplay()` and `CommandTester::getDisplay()`
* added a way to set the progress bar progress via the `setCurrent` method
@@ -70,26 +70,6 @@ public function formatBlock($messages, $style, $large = false)
return implode("\n", $messages);
}
- /**
- * Returns the length of a string, using mb_strlen if it is available.
- *
- * @param string $string The string to check its length
- *
- * @return integer The length of the string
- */
- private function strlen($string)
- {
- if (!function_exists('mb_strlen')) {
- return strlen($string);
- }
-
- if (false === $encoding = mb_detect_encoding($string)) {
- return strlen($string);
- }
-
- return mb_strlen($string, $encoding);
- }
-
/**
* {@inheritDoc}
*/
@@ -39,4 +39,24 @@ public function getHelperSet()
{
return $this->helperSet;
}
+
+ /**
+ * Returns the length of a string, using mb_strlen if it is available.
+ *
+ * @param string $string The string to check its length
+ *
+ * @return integer The length of the string
+ */
+ protected function strlen($string)
+ {
+ if (!function_exists('mb_strlen')) {
+ return strlen($string);
+ }
+
+ if (false === $encoding = mb_detect_encoding($string)) {
+ return strlen($string);
+ }
+
+ return mb_strlen($string, $encoding);
+ }
}
@@ -14,7 +14,7 @@
use Symfony\Component\Console\Output\OutputInterface;
/**
- * The Progress class providers helpers to display progress output.
+ * The Progress class provides helpers to display progress output.
*
* @author Chris Jones <leeked@gmail.com>
* @author Fabien Potencier <fabien@symfony.com>
@@ -320,7 +320,7 @@ private function initialize()
}
if ($this->max > 0) {
- $this->widths['max'] = $this->getLength($this->max);
+ $this->widths['max'] = $this->strlen($this->max);
$this->widths['current'] = $this->widths['max'];
} else {
$this->barCharOriginal = $this->barChar;
@@ -356,7 +356,7 @@ private function generate($finish = false)
}
}
- $emptyBars = $this->barWidth - $completeBars - $this->getLength($this->progressChar);
+ $emptyBars = $this->barWidth - $completeBars - $this->strlen($this->progressChar);
$bar = str_repeat($this->barChar, $completeBars);
if ($completeBars < $this->barWidth) {
$bar .= $this->progressChar;
@@ -419,7 +419,7 @@ private function humaneTime($secs)
*/
private function overwrite(OutputInterface $output, $message)
{
- $length = $this->getLength($message);
+ $length = $this->strlen($message);
// append whitespace to match the last line's length
if (null !== $this->lastMessagesLength && $this->lastMessagesLength > $length) {
@@ -430,26 +430,7 @@ private function overwrite(OutputInterface $output, $message)
$output->write("\x0D");
$output->write($message);
- $this->lastMessagesLength = $this->getLength($message);
- }
-
- /**
- * Wrapper arround strlen: uses multi-byte function if available
- *
- * @param string $string
- * @return integer
- */
- private function getLength($string)
- {
- if (!function_exists('mb_strlen')) {
- return strlen($string);
- }
-
- if (false === $encoding = mb_detect_encoding($string)) {
- return strlen($string);
- }
-
- return mb_strlen($string, $encoding);
+ $this->lastMessagesLength = $this->strlen($message);
}
/**
Oops, something went wrong.

0 comments on commit 22adfdf

Please sign in to comment.