Skip to content

Commit 8f206c8

Browse files
committed
fixed CS, simplified code
1 parent b030c24 commit 8f206c8

File tree

6 files changed

+176
-160
lines changed

6 files changed

+176
-160
lines changed

src/Symfony/Component/Console/Application.php

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
use Symfony\Component\Console\Event\ConsoleTerminateEvent;
3737
use Symfony\Component\Console\Exception\CommandNotFoundException;
3838
use Symfony\Component\Console\Exception\LogicException;
39-
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
39+
use Symfony\Component\Console\Terminal;
4040
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
4141

4242
/**
@@ -66,24 +66,19 @@ class Application
6666
private $definition;
6767
private $helperSet;
6868
private $dispatcher;
69+
private $terminal;
6970
private $defaultCommand;
7071
private $singleCommand;
7172

7273
/**
73-
* @var TerminalDimensionsProvider
74+
* @param string $name The name of the application
75+
* @param string $version The version of the application
7476
*/
75-
private $terminalDimensionsProvider;
76-
77-
/**
78-
* @param string $name The name of the application
79-
* @param string $version The version of the application
80-
* @param TerminalDimensionsProvider $terminalDimensionsProvider
81-
*/
82-
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN', TerminalDimensionsProvider $terminalDimensionsProvider = null)
77+
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN')
8378
{
8479
$this->name = $name;
8580
$this->version = $version;
86-
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider();
81+
$this->terminal = new Terminal();
8782
$this->defaultCommand = 'list';
8883
$this->helperSet = $this->getDefaultHelperSet();
8984
$this->definition = $this->getDefaultInputDefinition();
@@ -697,7 +692,7 @@ public function renderException(\Exception $e, OutputInterface $output)
697692
*/
698693
protected function getTerminalWidth()
699694
{
700-
return $this->terminalDimensionsProvider->getTerminalWidth();
695+
return $this->terminal->getWidth();
701696
}
702697

703698
/**
@@ -707,7 +702,7 @@ protected function getTerminalWidth()
707702
*/
708703
protected function getTerminalHeight()
709704
{
710-
return $this->terminalDimensionsProvider->getTerminalWidth();
705+
return $this->terminal->getHeight();
711706
}
712707

713708
/**
@@ -717,7 +712,7 @@ protected function getTerminalHeight()
717712
*/
718713
public function getTerminalDimensions()
719714
{
720-
return $this->terminalDimensionsProvider->getTerminalDimensions();
715+
return $this->terminal->getDimensions();
721716
}
722717

723718
/**
@@ -732,7 +727,7 @@ public function getTerminalDimensions()
732727
*/
733728
public function setTerminalDimensions($width, $height)
734729
{
735-
$this->terminalDimensionsProvider->setTerminalDimensions($width, $height);
730+
$this->terminal->setDimensions($width, $height);
736731

737732
return $this;
738733
}

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

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
use Symfony\Component\Console\Output\ConsoleOutputInterface;
1515
use Symfony\Component\Console\Output\OutputInterface;
1616
use Symfony\Component\Console\Exception\LogicException;
17-
use Symfony\Component\Console\Terminal\TerminalDimensionsProvider;
17+
use Symfony\Component\Console\Terminal;
1818

1919
/**
2020
* The ProgressBar provides helpers to display progress output.
@@ -45,29 +45,24 @@ class ProgressBar
4545
private $formatLineCount;
4646
private $messages = array();
4747
private $overwrite = true;
48+
private $terminal;
4849

4950
private static $formatters;
5051
private static $formats;
5152

5253
/**
53-
* @var TerminalDimensionsProvider
54+
* @param OutputInterface $output An OutputInterface instance
55+
* @param int $max Maximum steps (0 if unknown)
5456
*/
55-
private $terminalDimensionsProvider;
56-
57-
/**
58-
* @param OutputInterface $output An OutputInterface instance
59-
* @param int $max Maximum steps (0 if unknown)
60-
* @param TerminalDimensionsProvider $terminalDimensionsProvider
61-
*/
62-
public function __construct(OutputInterface $output, $max = 0, TerminalDimensionsProvider $terminalDimensionsProvider = null)
57+
public function __construct(OutputInterface $output, $max = 0)
6358
{
6459
if ($output instanceof ConsoleOutputInterface) {
6560
$output = $output->getErrorOutput();
6661
}
6762

6863
$this->output = $output;
6964
$this->setMaxSteps($max);
70-
$this->terminalDimensionsProvider = $terminalDimensionsProvider ?: new TerminalDimensionsProvider();
65+
$this->terminal = new Terminal();
7166

7267
if (!$this->output->isDecorated()) {
7368
// disable overwrite when output does not support ANSI codes.
@@ -443,6 +438,20 @@ public function clear()
443438
$this->overwrite('');
444439
}
445440

441+
/**
442+
* Gets the terminal.
443+
*
444+
* Can be useful to force terminal dimensions for functional tests.
445+
*
446+
* @return Terminal
447+
*
448+
* @internal
449+
*/
450+
public function getTerminal()
451+
{
452+
return $this->terminal;
453+
}
454+
446455
/**
447456
* Sets the progress bar format.
448457
*
@@ -617,7 +626,7 @@ private function buildLine()
617626
private function adjustLineWidthToTerminalWidth($line)
618627
{
619628
$lineLength = Helper::strlenWithoutDecoration($this->output->getFormatter(), $line);
620-
$terminalWidth = $this->terminalDimensionsProvider->getTerminalWidth();
629+
$terminalWidth = $this->terminal->getWidth();
621630
if ($lineLength > $terminalWidth) {
622631
$newBarWidth = $this->barWidth - $lineLength + $terminalWidth;
623632
$this->setBarWidth($newBarWidth);

src/Symfony/Component/Console/Terminal/TerminalDimensionsProvider.php renamed to src/Symfony/Component/Console/Terminal.php

Lines changed: 64 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -9,88 +9,103 @@
99
* file that was distributed with this source code.
1010
*/
1111

12-
namespace Symfony\Component\Console\Terminal;
12+
namespace Symfony\Component\Console;
1313

14-
class TerminalDimensionsProvider
14+
class Terminal
1515
{
16-
/**
17-
* @var int[]
18-
*/
19-
private $terminalDimensions = array();
16+
private $width;
17+
private $height;
2018

2119
/**
22-
* Tries to figure out the terminal dimensions based on the current environment.
20+
* Tries to figure out the terminal width in which this application runs.
2321
*
24-
* @return int[] Array containing width and height
22+
* @return int|null
2523
*/
26-
public function getTerminalDimensions()
24+
public function getWidth()
2725
{
28-
if ($this->terminalDimensions) {
29-
return $this->terminalDimensions;
30-
}
31-
32-
if ($this->isWindowsEnvironment()) {
33-
// extract [w, H] from "wxh (WxH)"
34-
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
35-
return array((int) $matches[1], (int) $matches[2]);
36-
}
37-
// extract [w, h] from "wxh"
38-
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) {
39-
return array((int) $matches[1], (int) $matches[2]);
40-
}
26+
if (null === $this->width) {
27+
$this->initDimensions();
4128
}
4229

43-
if ($sttyString = $this->getSttyColumns()) {
44-
// extract [w, h] from "rows h; columns w;"
45-
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
46-
return array((int) $matches[2], (int) $matches[1]);
47-
}
48-
// extract [w, h] from "; h rows; w columns"
49-
if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
50-
return array((int) $matches[2], (int) $matches[1]);
51-
}
52-
}
53-
54-
return array(null, null);
30+
return $this->width;
5531
}
5632

5733
/**
58-
* Tries to figure out the terminal width in which this application runs.
34+
* Sets the terminal width.
5935
*
60-
* @return int|null
36+
* @param int
6137
*/
62-
public function getTerminalWidth()
38+
public function setWidth($width)
6339
{
64-
return $this->getTerminalDimensions()[0];
40+
$this->width = $width;
6541
}
6642

6743
/**
6844
* Tries to figure out the terminal height in which this application runs.
6945
*
7046
* @return int|null
7147
*/
72-
public function getTerminalHeight()
48+
public function getHeight()
7349
{
74-
return $this->getTerminalDimensions()[1];
50+
if (null === $this->height) {
51+
$this->initDimensions();
52+
}
53+
54+
return $this->height;
7555
}
7656

7757
/**
78-
* Sets terminal dimensions.
79-
*
80-
* Can be useful to force terminal dimensions for functional tests.
58+
* Sets the terminal height.
8159
*
82-
* @param int $width
83-
* @param int $height
60+
* @param int
8461
*/
85-
public function setTerminalDimensions($width, $height)
62+
public function setHeight($height)
8663
{
87-
$this->terminalDimensions = array($width, $height);
64+
$this->height = $height;
65+
}
66+
67+
private function initDimensions()
68+
{
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)) {
76+
// extract [w, H] from "wxh (WxH)"
77+
$width = (int) $matches[1];
78+
$height = (int) $matches[2];
79+
} elseif (null != $dimensions = $this->getConsoleMode()) {
80+
// extract [w, h] from "wxh"
81+
$width = $dimensions[0];
82+
$height = $dimensions[1];
83+
}
84+
} elseif ($sttyString = $this->getSttyColumns()) {
85+
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
86+
// extract [w, h] from "rows h; columns w;"
87+
$width = (int) $matches[1];
88+
$height = (int) $matches[2];
89+
} elseif (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
90+
// extract [w, h] from "; h rows; w columns"
91+
$width = (int) $matches[2];
92+
$heighth = (int) $matches[1];
93+
}
94+
}
95+
96+
if (null === $this->width) {
97+
$this->width = $width;
98+
}
99+
100+
if (null === $this->height) {
101+
$this->height = $height;
102+
}
88103
}
89104

90105
/**
91106
* Runs and parses mode CON if it's available, suppressing any error output.
92107
*
93-
* @return string <width>x<height> or null if it could not be parsed
108+
* @return array|null An array composed of the width and the height or null if it could not be parsed
94109
*/
95110
private function getConsoleMode()
96111
{
@@ -110,7 +125,7 @@ private function getConsoleMode()
110125
proc_close($process);
111126

112127
if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
113-
return $matches[2].'x'.$matches[1];
128+
return array((int) $matches[2], (int) $matches[1]);
114129
}
115130
}
116131
}

0 commit comments

Comments
 (0)