[Console] added a compact layout for the table helper

This commit is contained in:
Fabien Potencier 2013-10-02 09:34:37 +02:00
parent e9ea73311f
commit da9bee08e4
2 changed files with 76 additions and 28 deletions

View File

@ -23,6 +23,7 @@ class TableHelper extends Helper
{ {
const LAYOUT_DEFAULT = 0; const LAYOUT_DEFAULT = 0;
const LAYOUT_BORDERLESS = 1; const LAYOUT_BORDERLESS = 1;
const LAYOUT_COMPACT = 2;
/** /**
* Table headers. * Table headers.
@ -45,6 +46,7 @@ class TableHelper extends Helper
private $crossingChar; private $crossingChar;
private $cellHeaderFormat; private $cellHeaderFormat;
private $cellRowFormat; private $cellRowFormat;
private $cellRowContentFormat;
private $borderFormat; private $borderFormat;
private $padType; private $padType;
@ -89,7 +91,22 @@ class TableHelper extends Helper
->setVerticalBorderChar(' ') ->setVerticalBorderChar(' ')
->setCrossingChar(' ') ->setCrossingChar(' ')
->setCellHeaderFormat('<info>%s</info>') ->setCellHeaderFormat('<info>%s</info>')
->setCellRowFormat('<comment>%s</comment>') ->setCellRowFormat('%s')
->setCellRowContentFormat(' %s ')
->setBorderFormat('%s')
->setPadType(STR_PAD_RIGHT)
;
break;
case self::LAYOUT_COMPACT:
$this
->setPaddingChar(' ')
->setHorizontalBorderChar('')
->setVerticalBorderChar(' ')
->setCrossingChar('')
->setCellHeaderFormat('<info>%s</info>')
->setCellRowFormat('%s')
->setCellRowContentFormat('%s')
->setBorderFormat('%s') ->setBorderFormat('%s')
->setPadType(STR_PAD_RIGHT) ->setPadType(STR_PAD_RIGHT)
; ;
@ -102,7 +119,8 @@ class TableHelper extends Helper
->setVerticalBorderChar('|') ->setVerticalBorderChar('|')
->setCrossingChar('+') ->setCrossingChar('+')
->setCellHeaderFormat('<info>%s</info>') ->setCellHeaderFormat('<info>%s</info>')
->setCellRowFormat('<comment>%s</comment>') ->setCellRowFormat('%s')
->setCellRowContentFormat(' %s ')
->setBorderFormat('%s') ->setBorderFormat('%s')
->setPadType(STR_PAD_RIGHT) ->setPadType(STR_PAD_RIGHT)
; ;
@ -241,6 +259,20 @@ class TableHelper extends Helper
return $this; return $this;
} }
/**
* Sets row cell content format.
*
* @param string $cellRowContentFormat
*
* @return TableHelper
*/
public function setCellRowContentFormat($cellRowContentFormat)
{
$this->cellRowContentFormat = $cellRowContentFormat;
return $this;
}
/** /**
* Sets table border format. * Sets table border format.
* *
@ -313,11 +345,13 @@ class TableHelper extends Helper
return; return;
} }
if (!$this->horizontalBorderChar && !$this->crossingChar) {
return;
}
$markup = $this->crossingChar; $markup = $this->crossingChar;
for ($column = 0; $column < $count; $column++) { for ($column = 0; $column < $count; $column++) {
$markup .= str_repeat($this->horizontalBorderChar, $this->getColumnWidth($column)) $markup .= str_repeat($this->horizontalBorderChar, $this->getColumnWidth($column)).$this->crossingChar;
.$this->crossingChar
;
} }
$this->output->writeln(sprintf($this->borderFormat, $markup)); $this->output->writeln(sprintf($this->borderFormat, $markup));
@ -370,15 +404,9 @@ class TableHelper extends Helper
$width += strlen($cell) - mb_strlen($cell, $encoding); $width += strlen($cell) - mb_strlen($cell, $encoding);
} }
$this->output->write(sprintf( $content = sprintf($this->cellRowContentFormat, $cell);
$cellFormat,
str_pad( $this->output->write(sprintf($cellFormat, str_pad($content, $width, $this->paddingChar, $this->padType)));
$this->paddingChar.$cell.$this->paddingChar,
$width,
$this->paddingChar,
$this->padType
)
));
} }
/** /**
@ -420,7 +448,7 @@ class TableHelper extends Helper
$lengths[] = $this->getCellWidth($row, $column); $lengths[] = $this->getCellWidth($row, $column);
} }
return $this->columnWidths[$column] = max($lengths) + 2; return $this->columnWidths[$column] = max($lengths) + strlen($this->cellRowContentFormat) - 2;
} }
/** /**

View File

@ -81,15 +81,17 @@ class TableHelperTest extends \PHPUnit_Framework_TestCase
public function testRenderProvider() public function testRenderProvider()
{ {
$books = 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'),
);
return array( return array(
array( array(
array('ISBN', 'Title', 'Author'), array('ISBN', 'Title', 'Author'),
array( $books,
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'),
),
TableHelper::LAYOUT_DEFAULT, TableHelper::LAYOUT_DEFAULT,
<<<TABLE <<<TABLE
+---------------+--------------------------+------------------+ +---------------+--------------------------+------------------+
@ -105,14 +107,32 @@ TABLE
), ),
array( array(
array('ISBN', 'Title', 'Author'), array('ISBN', 'Title', 'Author'),
array( $books,
array('99921-58-10-7', 'Divine Comedy', 'Dante Alighieri'), TableHelper::LAYOUT_COMPACT,
array('9971-5-0210-0', 'A Tale of Two Cities', 'Charles Dickens'), <<<TABLE
array('960-425-059-0', 'The Lord of the Rings', 'J. R. R. Tolkien'), ISBN Title Author
array('80-902734-1-6', 'And Then There Were None', 'Agatha Christie'), 99921-58-10-7 Divine Comedy Dante Alighieri
), 9971-5-0210-0 A Tale of Two Cities Charles Dickens
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie
TABLE
),
array(
array('ISBN', 'Title', 'Author'),
$books,
TableHelper::LAYOUT_BORDERLESS, TableHelper::LAYOUT_BORDERLESS,
" =============== ========================== ================== \n ISBN Title Author \n =============== ========================== ================== \n 99921-58-10-7 Divine Comedy Dante Alighieri \n 9971-5-0210-0 A Tale of Two Cities Charles Dickens \n 960-425-059-0 The Lord of the Rings J. R. R. Tolkien \n 80-902734-1-6 And Then There Were None Agatha Christie \n =============== ========================== ================== \n" <<<TABLE
=============== ========================== ==================
ISBN Title Author
=============== ========================== ==================
99921-58-10-7 Divine Comedy Dante Alighieri
9971-5-0210-0 A Tale of Two Cities Charles Dickens
960-425-059-0 The Lord of the Rings J. R. R. Tolkien
80-902734-1-6 And Then There Were None Agatha Christie
=============== ========================== ==================
TABLE
), ),
array( array(
array('ISBN', 'Title'), array('ISBN', 'Title'),