Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Merge pull request #5711 from nicktacular/console
Browse files Browse the repository at this point in the history
Implemented writeTextBlock method in Zend\Console\Adapter\AbstractAdapter
  • Loading branch information
weierophinney committed Mar 4, 2014
2 parents bb7ce5b + 1124958 commit 0e2e75c
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 0 deletions.
39 changes: 39 additions & 0 deletions library/Zend/Console/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ public function writeBox(
* @param int $y Block Y coordinate (row)
* @param null|int $color (optional) Text color
* @param null|int $bgColor (optional) Text background color
* @throws Exception\InvalidArgumentException
*/
public function writeTextBlock(
$text,
Expand All @@ -298,6 +299,44 @@ public function writeTextBlock(
$color = null,
$bgColor = null
) {
if ($x < 0 || $y < 0) {
throw new Exception\InvalidArgumentException('Supplied X,Y coordinates are invalid.');
}

if ($width < 1) {
throw new Exception\InvalidArgumentException('Invalid width supplied.');
}

if (null !== $height && $height < 1) {
throw new Exception\InvalidArgumentException('Invalid height supplied.');
}

//ensure the text is not wider than the width
if (strlen($text) > $width) {
$text = wordwrap($text, $width, PHP_EOL, true);
} else {
//just write the line at the spec'd position
$this->setPos($x, $y);
$this->write($text, $color, $bgColor);

return;
}

//convert to array of lines
$lines = explode(PHP_EOL, $text);

//truncate if height was specified
if (null !== $height && count($lines) > $height) {
$lines = array_slice($lines, 0, $height);
}

//write each line
$curY = $y;
foreach ($lines as $line) {
$this->setPos($x, $curY);
$this->write($line, $color, $bgColor);
$curY++;//next line
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions library/Zend/Console/Adapter/Posix.php
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,7 @@ protected function setTTYMode($mode)
* Get the final color code and throw exception on error
*
* @param null|int|Xterm256 $color
* @param string $type (optional) Foreground 'fg' or background 'bg'.
* @throws Exception\BadMethodCallException
* @return string
*/
Expand Down
68 changes: 68 additions & 0 deletions tests/ZendTest/Console/Adapter/AbstractAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,4 +144,72 @@ public function testEncodeText()
$encodedText = $this->adapter->encodeText(utf8_decode($text));
$this->assertEquals(utf8_decode($text),$encodedText);
}

public function testWriteTextBlockSameAsWidth()
{
//set some text that's the same size as the width
$text = 'hello there, I am short!';
$width = strlen($text);

ob_start();
$this->adapter->writeTextBlock($text, $width);
$this->assertSame($text, ob_get_clean());
}

public function testTextBlockLongUnbreakableWord()
{
$text = 'thisisaverylongwordthatwontbreakproperlysothereyouhaveit and here is some more text';
$expected = array('thisisaver', 'ylongwordt', 'hatwontbre', 'akproperly'
, 'sothereyou', 'haveit and', 'here is', 'some more'
, 'text');

ob_start();
$this->adapter->writeTextBlock($text, 10);
$this->assertSame($expected, $this->adapter->writtenData);

//just get rid of the data in ob
ob_get_clean();
}
public function testTextBlockLongerThanHeight()
{
$text = 'thisisaverylongwordthatwontbreakproperlysothereyouhaveit and here is some more text';
$expected = array('thisisaver', 'ylongwordt', 'hatwontbre');

//reset tracking of written data
$this->adapter->writtenData = array();

ob_start();
$this->adapter->writeTextBlock($text, 10, 3);
$this->assertSame($expected, $this->adapter->writtenData);

//just get rid of the data in ob
ob_get_clean();
}

/**
* @expectedException \Zend\Console\Exception\InvalidArgumentException
* @expectedExceptionMessage Supplied X,Y coordinates are invalid.
*/
public function testInvalidCoords()
{
$this->adapter->writeTextBlock('', 1, 1, -1, -9);
}

/**
* @expectedException \Zend\Console\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid width supplied.
*/
public function testInvalidWidth()
{
$this->adapter->writeTextBlock('', 0);
}

/**
* @expectedException \Zend\Console\Exception\InvalidArgumentException
* @expectedExceptionMessage Invalid height supplied.
*/
public function testInvalidHeight()
{
$this->adapter->writeTextBlock('', 80, 0, 2, 2);
}
}
14 changes: 14 additions & 0 deletions tests/ZendTest/Console/TestAssets/ConsoleAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ class ConsoleAdapter extends AbstractAdapter

public $testIsUtf8 = true;

public $writtenData = array();

/**
* Read a single line from the console input
*
Expand Down Expand Up @@ -86,4 +88,16 @@ public function getWidth()
{
return $this->testWidth;
}

/**
* Tracks exactly what data has been written
* @param string $text
* @param null $color
* @param null $bgColor
*/
public function write($text, $color = null, $bgColor = null)
{
$this->writtenData[] = $text;
parent::write($text, $color, $bgColor);
}
}

0 comments on commit 0e2e75c

Please sign in to comment.