Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 355ad6c

Browse files
committed
switched to use COLUMNS and LINES env vars to change terminal dimensions
1 parent a8e1131 commit 355ad6c

File tree

6 files changed

+84
-103
lines changed

6 files changed

+84
-103
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -675,26 +675,16 @@ public function renderException(\Exception $e, OutputInterface $output)
675675
}
676676
}
677677

678-
/**
679-
* Returns the current terminal.
680-
*
681-
* @return Terminal
682-
*/
683-
public function getTerminal()
684-
{
685-
return $this->terminal;
686-
}
687-
688678
/**
689679
* Tries to figure out the terminal width in which this application runs.
690680
*
691681
* @return int|null
692682
*
693-
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
683+
* @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.
694684
*/
695685
protected function getTerminalWidth()
696686
{
697-
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
687+
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
698688

699689
return $this->terminal->getWidth();
700690
}
@@ -704,11 +694,11 @@ protected function getTerminalWidth()
704694
*
705695
* @return int|null
706696
*
707-
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
697+
* @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.
708698
*/
709699
protected function getTerminalHeight()
710700
{
711-
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
701+
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
712702

713703
return $this->terminal->getHeight();
714704
}
@@ -718,11 +708,11 @@ protected function getTerminalHeight()
718708
*
719709
* @return array Array containing width and height
720710
*
721-
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
711+
* @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead.
722712
*/
723713
public function getTerminalDimensions()
724714
{
725-
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
715+
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Create a Terminal instance instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
726716

727717
return array($this->terminal->getWidth(), $this->terminal->getHeight());
728718
}
@@ -737,14 +727,14 @@ public function getTerminalDimensions()
737727
*
738728
* @return Application The current application
739729
*
740-
* @deprecated since version 3.2, to be removed in 4.0. Use the getTerminal() method instead.
730+
* @deprecated since version 3.2, to be removed in 4.0. Set the COLUMNS and LINES env vars instead.
741731
*/
742732
public function setTerminalDimensions($width, $height)
743733
{
744-
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Use getTerminal() instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
734+
@trigger_error(sprintf('%s is deprecated as of 3.2 and will be removed in 4.0. Set the COLUMNS and LINES env vars instead.', __METHOD__, ArgumentResolverInterface::class), E_USER_DEPRECATED);
745735

746-
$this->terminal->setWidth($width);
747-
$this->terminal->setHeight($height);
736+
putenv('COLUMNS='.$width);
737+
putenv('LINES='.$height);
748738

749739
return $this;
750740
}

src/Symfony/Component/Console/Helper/ProgressBar.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -436,20 +436,6 @@ public function clear()
436436
$this->overwrite('');
437437
}
438438

439-
/**
440-
* Gets the terminal.
441-
*
442-
* Can be useful to force terminal dimensions for functional tests.
443-
*
444-
* @return Terminal
445-
*
446-
* @internal
447-
*/
448-
public function getTerminal()
449-
{
450-
return $this->terminal;
451-
}
452-
453439
/**
454440
* Sets the progress bar format.
455441
*

src/Symfony/Component/Console/Terminal.php

Lines changed: 32 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
class Terminal
1515
{
16-
private $width;
17-
private $height;
16+
private static $width;
17+
private static $height;
1818

1919
/**
2020
* Gets the terminal width.
@@ -23,21 +23,15 @@ class Terminal
2323
*/
2424
public function getWidth()
2525
{
26-
if (null === $this->width) {
27-
$this->initDimensions();
26+
if ($width = trim(getenv('COLUMNS'))) {
27+
return (int) $width;
2828
}
2929

30-
return $this->width;
31-
}
30+
if (null === self::$width) {
31+
self::initDimensions();
32+
}
3233

33-
/**
34-
* Sets the terminal width.
35-
*
36-
* @param int $width
37-
*/
38-
public function setWidth($width)
39-
{
40-
$this->width = $width;
34+
return self::$width;
4135
}
4236

4337
/**
@@ -47,58 +41,49 @@ public function setWidth($width)
4741
*/
4842
public function getHeight()
4943
{
50-
if (null === $this->height) {
51-
$this->initDimensions();
44+
if ($height = trim(getenv('LINES'))) {
45+
return (int) $height;
5246
}
5347

54-
return $this->height;
55-
}
48+
if (null === self::$height) {
49+
self::initDimensions();
50+
}
5651

57-
/**
58-
* Sets the terminal height.
59-
*
60-
* @param int $height
61-
*/
62-
public function setHeight($height)
63-
{
64-
$this->height = $height;
52+
return self::$height;
6553
}
6654

67-
private function initDimensions()
55+
private static function initDimensions()
6856
{
69-
if (null !== $this->width && null !== $this->height) {
70-
return;
71-
}
72-
7357
$width = $height = null;
74-
if ($this->isWindowsEnvironment()) {
75-
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
58+
if ('\\' === DIRECTORY_SEPARATOR) {
59+
if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
7660
// extract [w, H] from "wxh (WxH)"
77-
$width = (int) $matches[1];
78-
$height = (int) $matches[2];
79-
} elseif (null != $dimensions = $this->getConsoleMode()) {
61+
// or [w, h] from "wxh"
62+
$width = $matches[1];
63+
$height = isset($matches[4]) ? $matches[4] : $matches[2];
64+
} elseif (null != $dimensions = self::getConsoleMode()) {
8065
// extract [w, h] from "wxh"
8166
$width = $dimensions[0];
8267
$height = $dimensions[1];
8368
}
84-
} elseif ($sttyString = $this->getSttyColumns()) {
69+
} elseif ($sttyString = self::getSttyColumns()) {
8570
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
8671
// extract [w, h] from "rows h; columns w;"
87-
$width = (int) $matches[1];
88-
$height = (int) $matches[2];
72+
$width = $matches[1];
73+
$height = $matches[2];
8974
} elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
9075
// extract [w, h] from "; h rows; w columns"
91-
$width = (int) $matches[2];
92-
$heighth = (int) $matches[1];
76+
$width = $matches[2];
77+
$height = $matches[1];
9378
}
9479
}
9580

96-
if (null === $this->width) {
97-
$this->width = $width;
81+
if (0 === self::$width) {
82+
self::$width = (int) $width ?: null;
9883
}
9984

100-
if (null === $this->height) {
101-
$this->height = $height;
85+
if (0 === self::$height) {
86+
self::$height = (int) $height ?: null;
10287
}
10388
}
10489

@@ -107,7 +92,7 @@ private function initDimensions()
10792
*
10893
* @return array|null An array composed of the width and the height or null if it could not be parsed
10994
*/
110-
private function getConsoleMode()
95+
private static function getConsoleMode()
11196
{
11297
if (!function_exists('proc_open')) {
11398
return;
@@ -135,7 +120,7 @@ private function getConsoleMode()
135120
*
136121
* @return string
137122
*/
138-
private function getSttyColumns()
123+
private static function getSttyColumns()
139124
{
140125
if (!function_exists('proc_open')) {
141126
return;
@@ -156,12 +141,4 @@ private function getSttyColumns()
156141
return $info;
157142
}
158143
}
159-
160-
/**
161-
* @return bool
162-
*/
163-
private function isWindowsEnvironment()
164-
{
165-
return '\\' === DIRECTORY_SEPARATOR;
166-
}
167144
}

src/Symfony/Component/Console/Tests/ApplicationTest.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -478,7 +478,7 @@ public function testSetCatchExceptions()
478478
{
479479
$application = new Application();
480480
$application->setAutoExit(false);
481-
$application->getTerminal()->setWidth(120);
481+
putenv('COLUMNS=120');
482482
$tester = new ApplicationTester($application);
483483

484484
$application->setCatchExceptions(true);
@@ -514,7 +514,7 @@ public function testRenderException()
514514
{
515515
$application = new Application();
516516
$application->setAutoExit(false);
517-
$application->getTerminal()->setWidth(120);
517+
putenv('COLUMNS=120');
518518
$tester = new ApplicationTester($application);
519519

520520
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
@@ -544,18 +544,19 @@ public function testRenderException()
544544

545545
$application = new Application();
546546
$application->setAutoExit(false);
547-
$application->getTerminal()->setWidth(32);
547+
putenv('COLUMNS=32');
548548
$tester = new ApplicationTester($application);
549549

550550
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
551551
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception4.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
552+
putenv('COLUMNS=120');
552553
}
553554

554555
public function testRenderExceptionWithDoubleWidthCharacters()
555556
{
556557
$application = new Application();
557558
$application->setAutoExit(false);
558-
$application->getTerminal()->setWidth(120);
559+
putenv('COLUMNS=120');
559560
$application->register('foo')->setCode(function () {
560561
throw new \Exception('エラーメッセージ');
561562
});
@@ -569,13 +570,14 @@ public function testRenderExceptionWithDoubleWidthCharacters()
569570

570571
$application = new Application();
571572
$application->setAutoExit(false);
572-
$application->getTerminal()->setWidth(32);
573+
putenv('COLUMNS=32');
573574
$application->register('foo')->setCode(function () {
574575
throw new \Exception('コマンドの実行中にエラーが発生しました。');
575576
});
576577
$tester = new ApplicationTester($application);
577578
$tester->run(array('command' => 'foo'), array('decorated' => false, 'capture_stderr_separately' => true));
578579
$this->assertStringEqualsFile(self::$fixturesPath.'/application_renderexception_doublewidth2.txt', $tester->getErrorOutput(true), '->renderException() wraps messages when they are bigger than the terminal');
580+
putenv('COLUMNS=120');
579581
}
580582

581583
public function testRun()

src/Symfony/Component/Console/Tests/Helper/ProgressBarTest.php

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -522,9 +522,10 @@ public function testWithSmallScreen()
522522
$output = $this->getOutputStream();
523523

524524
$bar = new ProgressBar($output);
525-
$bar->getTerminal()->setWidth(12);
525+
putenv('COLUMNS=12');
526526
$bar->start();
527527
$bar->advance();
528+
putenv('COLUMNS=120');
528529

529530
rewind($output->getStream());
530531
$this->assertEquals(
@@ -577,6 +578,8 @@ public function testMultilineFormat()
577578

578579
public function testAnsiColorsAndEmojis()
579580
{
581+
putenv('COLUMNS=156');
582+
580583
$bar = new ProgressBar($output = $this->getOutputStream(), 15);
581584
ProgressBar::setPlaceholderFormatterDefinition('memory', function (ProgressBar $bar) {
582585
static $i = 0;
@@ -592,30 +595,47 @@ public function testAnsiColorsAndEmojis()
592595

593596
$bar->setMessage('Starting the demo... fingers crossed', 'title');
594597
$bar->start();
595-
$bar->setMessage('Looks good to me...', 'title');
596-
$bar->advance(4);
597-
$bar->setMessage('Thanks, bye', 'title');
598-
$bar->finish();
599598

600599
rewind($output->getStream());
601600
$this->assertEquals(
602601
$this->generateOutput(
603602
" \033[44;37m Starting the demo... fingers crossed \033[0m\n".
604603
' 0/15 '.$progress.str_repeat($empty, 26)." 0%\n".
605604
" \xf0\x9f\x8f\x81 < 1 sec \033[44;37m 0 B \033[0m"
606-
).
605+
),
606+
stream_get_contents($output->getStream())
607+
);
608+
ftruncate($output->getStream(), 0);
609+
rewind($output->getStream());
610+
611+
$bar->setMessage('Looks good to me...', 'title');
612+
$bar->advance(4);
613+
614+
rewind($output->getStream());
615+
$this->assertEquals(
607616
$this->generateOutput(
608617
" \033[44;37m Looks good to me... \033[0m\n".
609618
' 4/15 '.str_repeat($done, 7).$progress.str_repeat($empty, 19)." 26%\n".
610619
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 97 KiB \033[0m"
611-
).
620+
),
621+
stream_get_contents($output->getStream())
622+
);
623+
ftruncate($output->getStream(), 0);
624+
rewind($output->getStream());
625+
626+
$bar->setMessage('Thanks, bye', 'title');
627+
$bar->finish();
628+
629+
rewind($output->getStream());
630+
$this->assertEquals(
612631
$this->generateOutput(
613632
" \033[44;37m Thanks, bye \033[0m\n".
614633
' 15/15 '.str_repeat($done, 28)." 100%\n".
615634
" \xf0\x9f\x8f\x81 < 1 sec \033[41;37m 195 KiB \033[0m"
616635
),
617636
stream_get_contents($output->getStream())
618637
);
638+
putenv('COLUMNS=120');
619639
}
620640

621641
public function testSetFormat()

src/Symfony/Component/Console/Tests/TerminalTest.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,16 @@ class TerminalTest extends \PHPUnit_Framework_TestCase
1717
{
1818
public function test()
1919
{
20+
putenv('COLUMNS=100');
21+
putenv('LINES=50');
2022
$terminal = new Terminal();
21-
$terminal->setWidth(100);
22-
$terminal->setHeight(50);
2323
$this->assertSame(100, $terminal->getWidth());
2424
$this->assertSame(50, $terminal->getHeight());
25+
26+
putenv('COLUMNS=120');
27+
putenv('LINES=60');
28+
$terminal = new Terminal();
29+
$this->assertSame(120, $terminal->getWidth());
30+
$this->assertSame(60, $terminal->getHeight());
2531
}
2632
}

0 commit comments

Comments
 (0)