Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdba73e7

Browse files
committed
switched to use COLUMNS and LINES env vars to change terminal dimensions
1 parenta8e1131 commitdba73e7

File tree

6 files changed

+82
-110
lines changed

6 files changed

+82
-110
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-
publicfunctiongetTerminal()
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
protectedfunctiongetTerminalWidth()
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
protectedfunctiongetTerminalHeight()
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
publicfunctiongetTerminalDimensions()
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
returnarray($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 thegetTerminal() method instead.
730+
* @deprecated since version 3.2, to be removed in 4.0.Set theCOLUMNS and LINES env vars instead.
741731
*/
742732
publicfunctionsetTerminalDimensions($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-
publicfunctiongetTerminal()
449-
{
450-
return$this->terminal;
451-
}
452-
453439
/**
454440
* Sets the progress bar format.
455441
*

‎src/Symfony/Component/Console/Terminal.php‎

Lines changed: 30 additions & 62 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+
privatestatic$width;
17+
privatestatic$height;
1818

1919
/**
2020
* Gets the terminal width.
@@ -23,21 +23,15 @@ class Terminal
2323
*/
2424
publicfunctiongetWidth()
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-
publicfunctionsetWidth($width)
39-
{
40-
$this->width =$width;
34+
returnself::$width;
4135
}
4236

4337
/**
@@ -47,67 +41,49 @@ public function setWidth($width)
4741
*/
4842
publicfunctiongetHeight()
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-
publicfunctionsetHeight($height)
63-
{
64-
$this->height =$height;
52+
returnself::$height;
6553
}
6654

67-
privatefunctioninitDimensions()
55+
privatestaticfunctioninitDimensions()
6856
{
69-
if (null !==$this->width &&null !==$this->height) {
70-
return;
71-
}
72-
73-
$width =$height =null;
74-
if ($this->isWindowsEnvironment()) {
75-
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/',trim(getenv('ANSICON')),$matches)) {
57+
if ('\\' ===DIRECTORY_SEPARATOR) {
58+
if (preg_match('/^(\d+)x(\d+)(?: \((\d+)x(\d+)\))?$/',trim(getenv('ANSICON')),$matches)) {
7659
// extract [w, H] from "wxh (WxH)"
77-
$width = (int)$matches[1];
78-
$height = (int)$matches[2];
79-
}elseif (null !=$dimensions =$this->getConsoleMode()) {
60+
// or [w, h] from "wxh"
61+
self::$width = (int)$matches[1];
62+
self::$height =isset($matches[4]) ? (int)$matches[4] : (int)$matches[2];
63+
}elseif (null !=$dimensions =self::getConsoleMode()) {
8064
// extract [w, h] from "wxh"
81-
$width =$dimensions[0];
82-
$height =$dimensions[1];
65+
self::$width = (int)$dimensions[0];
66+
self::$height = (int)$dimensions[1];
8367
}
84-
}elseif ($sttyString =$this->getSttyColumns()) {
68+
}elseif ($sttyString =self::getSttyColumns()) {
8569
if (preg_match('/rows.(\d+);.columns.(\d+);/i',$sttyString,$matches)) {
8670
// extract [w, h] from "rows h; columns w;"
87-
$width = (int)$matches[1];
88-
$height = (int)$matches[2];
71+
self::$width = (int)$matches[2];
72+
self::$height = (int)$matches[1];
8973
}elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i',$sttyString,$matches)) {
9074
// extract [w, h] from "; h rows; w columns"
91-
$width = (int)$matches[2];
92-
$heighth = (int)$matches[1];
75+
self::$width = (int)$matches[2];
76+
self::$height = (int)$matches[1];
9377
}
9478
}
95-
96-
if (null ===$this->width) {
97-
$this->width =$width;
98-
}
99-
100-
if (null ===$this->height) {
101-
$this->height =$height;
102-
}
10379
}
10480

10581
/**
10682
* Runs and parses mode CON if it's available, suppressing any error output.
10783
*
10884
* @return array|null An array composed of the width and the height or null if it could not be parsed
10985
*/
110-
privatefunctiongetConsoleMode()
86+
privatestaticfunctiongetConsoleMode()
11187
{
11288
if (!function_exists('proc_open')) {
11389
return;
@@ -135,7 +111,7 @@ private function getConsoleMode()
135111
*
136112
* @return string
137113
*/
138-
privatefunctiongetSttyColumns()
114+
privatestaticfunctiongetSttyColumns()
139115
{
140116
if (!function_exists('proc_open')) {
141117
return;
@@ -156,12 +132,4 @@ private function getSttyColumns()
156132
return$info;
157133
}
158134
}
159-
160-
/**
161-
* @return bool
162-
*/
163-
privatefunctionisWindowsEnvironment()
164-
{
165-
return'\\' ===DIRECTORY_SEPARATOR;
166-
}
167135
}

‎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 =newApplication();
480480
$application->setAutoExit(false);
481-
$application->getTerminal()->setWidth(120);
481+
putenv('COLUMNS=120');
482482
$tester =newApplicationTester($application);
483483

484484
$application->setCatchExceptions(true);
@@ -514,7 +514,7 @@ public function testRenderException()
514514
{
515515
$application =newApplication();
516516
$application->setAutoExit(false);
517-
$application->getTerminal()->setWidth(120);
517+
putenv('COLUMNS=120');
518518
$tester =newApplicationTester($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 =newApplication();
546546
$application->setAutoExit(false);
547-
$application->getTerminal()->setWidth(32);
547+
putenv('COLUMNS=32');
548548
$tester =newApplicationTester($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
publicfunctiontestRenderExceptionWithDoubleWidthCharacters()
555556
{
556557
$application =newApplication();
557558
$application->setAutoExit(false);
558-
$application->getTerminal()->setWidth(120);
559+
putenv('COLUMNS=120');
559560
$application->register('foo')->setCode(function () {
560561
thrownew \Exception('エラーメッセージ');
561562
});
@@ -569,13 +570,14 @@ public function testRenderExceptionWithDoubleWidthCharacters()
569570

570571
$application =newApplication();
571572
$application->setAutoExit(false);
572-
$application->getTerminal()->setWidth(32);
573+
putenv('COLUMNS=32');
573574
$application->register('foo')->setCode(function () {
574575
thrownew \Exception('コマンドの実行中にエラーが発生しました。');
575576
});
576577
$tester =newApplicationTester($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
publicfunctiontestRun()

‎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 =newProgressBar($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
publicfunctiontestAnsiColorsAndEmojis()
579580
{
581+
putenv('COLUMNS=156');
582+
580583
$bar =newProgressBar($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
publicfunctiontestSetFormat()

‎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
publicfunctiontest()
1919
{
20+
putenv('COLUMNS=100');
21+
putenv('LINES=50');
2022
$terminal =newTerminal();
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 =newTerminal();
29+
$this->assertSame(120,$terminal->getWidth());
30+
$this->assertSame(60,$terminal->getHeight());
2531
}
2632
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp