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

Skip to content

Commit c92b2b4

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

File tree

6 files changed

+63
-96
lines changed

6 files changed

+63
-96
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: 16 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,6 @@ public function getWidth()
3030
return $this->width;
3131
}
3232

33-
/**
34-
* Sets the terminal width.
35-
*
36-
* @param int $width
37-
*/
38-
public function setWidth($width)
39-
{
40-
$this->width = $width;
41-
}
42-
4333
/**
4434
* Gets the terminal height.
4535
*
@@ -54,28 +44,21 @@ public function getHeight()
5444
return $this->height;
5545
}
5646

57-
/**
58-
* Sets the terminal height.
59-
*
60-
* @param int $height
61-
*/
62-
public function setHeight($height)
63-
{
64-
$this->height = $height;
65-
}
66-
6747
private function initDimensions()
6848
{
69-
if (null !== $this->width && null !== $this->height) {
49+
$this->width = (int) trim(getenv('COLUMNS'));
50+
$this->height = (int) trim(getenv('LINES'));
51+
if (0 !== $this->width && 0 !== $this->height) {
7052
return;
7153
}
7254

7355
$width = $height = null;
74-
if ($this->isWindowsEnvironment()) {
75-
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
56+
if ('\\' === DIRECTORY_SEPARATOR) {
57+
if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/', trim(getenv('ANSICON')), $matches)) {
7658
// extract [w, H] from "wxh (WxH)"
77-
$width = (int) $matches[1];
78-
$height = (int) $matches[2];
59+
// or [w, h] from "wxh"
60+
$width = $matches[1];
61+
$height = isset($matches[4]) ? $matches[4] : $matches[2];
7962
} elseif (null != $dimensions = $this->getConsoleMode()) {
8063
// extract [w, h] from "wxh"
8164
$width = $dimensions[0];
@@ -84,21 +67,21 @@ private function initDimensions()
8467
} elseif ($sttyString = $this->getSttyColumns()) {
8568
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
8669
// extract [w, h] from "rows h; columns w;"
87-
$width = (int) $matches[1];
88-
$height = (int) $matches[2];
70+
$width = $matches[1];
71+
$height = $matches[2];
8972
} elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
9073
// extract [w, h] from "; h rows; w columns"
91-
$width = (int) $matches[2];
92-
$heighth = (int) $matches[1];
74+
$width = $matches[2];
75+
$height = $matches[1];
9376
}
9477
}
9578

96-
if (null === $this->width) {
97-
$this->width = $width;
79+
if (0 === $this->width) {
80+
$this->width = (int) $width ?: null;
9881
}
9982

100-
if (null === $this->height) {
101-
$this->height = $height;
83+
if (0 === $this->height) {
84+
$this->height = (int) $height ?: null;
10285
}
10386
}
10487

@@ -156,12 +139,4 @@ private function getSttyColumns()
156139
return $info;
157140
}
158141
}
159-
160-
/**
161-
* @return bool
162-
*/
163-
private function isWindowsEnvironment()
164-
{
165-
return '\\' === DIRECTORY_SEPARATOR;
166-
}
167142
}

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

Lines changed: 9 additions & 12 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()
@@ -1019,16 +1021,11 @@ public function testRunWithDispatcherAddingInputOptions()
10191021
public function testTerminalDimensions()
10201022
{
10211023
$application = new Application();
1024+
$application->setTerminalDimensions(120, 80);
10221025
$originalDimensions = $application->getTerminalDimensions();
10231026
$this->assertCount(2, $originalDimensions);
10241027

1025-
$width = 80;
1026-
if ($originalDimensions[0] == $width) {
1027-
$width = 100;
1028-
}
1029-
1030-
$application->setTerminalDimensions($width, 80);
1031-
$this->assertSame(array($width, 80), $application->getTerminalDimensions());
1028+
$this->assertSame(array(120, 80), $application->getTerminalDimensions());
10321029
}
10331030

10341031
protected function getDispatcher($skipCommand = false)

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

Lines changed: 26 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,23 +595,39 @@ 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".

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ 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());
2525
}

0 commit comments

Comments
 (0)