Skip to content

Commit

Permalink
[Console] Added a way to set terminal dimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
lyrixx authored and fabpot committed Aug 9, 2013
1 parent 5cbfe30 commit ce32981
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/Symfony/Component/Console/Application.php
Expand Up @@ -66,6 +66,7 @@ class Application
private $definition;
private $helperSet;
private $dispatcher;
private $terminalDimensions;

/**
* Constructor.
Expand Down Expand Up @@ -829,6 +830,10 @@ protected function getTerminalHeight()
*/
public function getTerminalDimensions()
{
if ($this->terminalDimensions) {
return $this->terminalDimensions;
}

if (defined('PHP_WINDOWS_VERSION_BUILD')) {
// extract [w, H] from "wxh (WxH)"
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
Expand All @@ -854,6 +859,23 @@ public function getTerminalDimensions()
return array(null, null);
}

/**
* Sets terminal dimensions.
*
* Can be useful to force terminal dimensions for functional tests.
*
* @param integer $width The width
* @param integer $height The height
*
* @return Application The current application
*/
public function setTerminalDimensions($width, $height)
{
$this->terminalDimensions = array($width, $height);

return $this;
}

/**
* Configures the input and output instances based on the user arguments and options.
*
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Expand Up @@ -4,6 +4,7 @@ CHANGELOG
2.4.0
-----

* added a way to force terminal dimensions
* [BC BREAK] made descriptors use output instead of returning a string

2.3.0
Expand Down
15 changes: 15 additions & 0 deletions src/Symfony/Component/Console/Tests/ApplicationTest.php
Expand Up @@ -792,6 +792,21 @@ public function testRunDispatchesAllEventsWithException()
$this->assertContains('before.foo.after.caught.', $tester->getDisplay());
}

public function testTerminalDimensions()
{
$application = new Application();
$originalDimensions = $application->getTerminalDimensions();
$this->assertCount(2, $originalDimensions);

$width = 80;
if ($originalDimensions[0] == $width) {
$width = 100;
}

$application->setTerminalDimensions($width, 80);
$this->assertSame(array($width, 80), $application->getTerminalDimensions());
}

protected function getDispatcher()
{
$dispatcher = new EventDispatcher;
Expand Down

0 comments on commit ce32981

Please sign in to comment.