Skip to content

Commit 9e6d7a5

Browse files
committed
Terminal width taken also by env
1 parent d626fab commit 9e6d7a5

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"phpunit/phpunit": "^6.5",
1818
"roave/security-advisories": "dev-master",
1919
"slam/php-cs-fixer-extensions": "^1.10",
20-
"slam/php-debug-r": "^1.1"
20+
"slam/php-debug-r": "^1.1",
21+
"symfony/console": "^4.0"
2122
},
2223
"autoload": {
2324
"psr-4": {

lib/ErrorHandler.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,7 @@ public function getTerminalWidth(): int
7979
if (null === $this->terminalWidth) {
8080
$width = \getenv('COLUMNS');
8181

82-
// @codeCoverageIgnoreStart
83-
if (\defined('PHP_WINDOWS_VERSION_BUILD') and $ansicon = \getenv('ANSICON')) {
84-
$width = \preg_replace('{^(\d+)x.*$}', '$1', $ansicon);
85-
}
86-
// @codeCoverageIgnoreEnd
87-
88-
if (\preg_match('{rows.(\d+);.columns.(\d+);}i', \exec('stty -a 2> /dev/null | grep columns'), $match)) {
82+
if (false === $width and 1 === \preg_match('{rows.(\d+);.columns.(\d+);}i', \exec('stty -a 2> /dev/null | grep columns'), $match)) {
8983
$width = $match[2];
9084
}
9185

tests/ErrorHandlerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use ErrorException;
88
use PHPUnit\Framework\TestCase;
99
use Slam\ErrorHandler\ErrorHandler;
10+
use Symfony\Component\Console\Terminal;
1011

1112
final class ErrorHandlerTest extends TestCase
1213
{
@@ -36,6 +37,7 @@ protected function setUp()
3637

3738
protected function tearDown()
3839
{
40+
\putenv('COLUMNS');
3941
\ini_set('error_log', $this->backupErrorLog);
4042
@\unlink($this->errorLog);
4143
}
@@ -224,4 +226,23 @@ public function testUsernameInEmailSubject()
224226

225227
$this->assertContains($username, $message['subject']);
226228
}
229+
230+
public function testTerminalWidthByEnv()
231+
{
232+
$width = \mt_rand(1000, 9000);
233+
\putenv(\sprintf('COLUMNS=%s', $width));
234+
235+
$errorHandler = new ErrorHandler(function () {
236+
});
237+
238+
$this->assertSame($width, $errorHandler->getTerminalWidth());
239+
240+
\putenv('COLUMNS');
241+
242+
$errorHandler = new ErrorHandler(function () {
243+
});
244+
245+
$terminal = new Terminal();
246+
$this->assertSame($terminal->getWidth(), $errorHandler->getTerminalWidth());
247+
}
227248
}

0 commit comments

Comments
 (0)