Skip to content

Commit 70a1f80

Browse files
committed
Removed the duplicated code used to get the command console window width
1 parent 6ae913b commit 70a1f80

File tree

1 file changed

+4
-68
lines changed

1 file changed

+4
-68
lines changed

src/Symfony/Component/Console/Style/SymfonyStyle.php

Lines changed: 4 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace Symfony\Component\Console\Style;
1313

14+
use Symfony\Component\Console\Application;
1415
use Symfony\Component\Console\Formatter\OutputFormatter;
1516
use Symfony\Component\Console\Helper\Helper;
1617
use Symfony\Component\Console\Helper\ProgressBar;
@@ -312,74 +313,9 @@ private function getProgressBar()
312313

313314
private function getTerminalWidth()
314315
{
315-
if ('\\' === DIRECTORY_SEPARATOR) {
316-
// extract [w, H] from "wxh (WxH)"
317-
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) {
318-
return (int) $matches[1];
319-
}
320-
// extract [w, h] from "wxh"
321-
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) {
322-
return (int) $matches[1];
323-
}
324-
}
325-
326-
if ($sttyString = $this->getSttyColumns()) {
327-
// extract [w, h] from "rows h; columns w;"
328-
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) {
329-
return (int) $matches[2];
330-
}
331-
// extract [w, h] from "; h rows; w columns"
332-
if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) {
333-
return (int) $matches[2];
334-
}
335-
}
336-
}
316+
$application = new Application();
317+
list ($width, $height) = $application->getTerminalDimensions();
337318

338-
/**
339-
* Runs and parses stty -a if it's available, suppressing any error output.
340-
*
341-
* @return string
342-
*/
343-
private function getSttyColumns()
344-
{
345-
if (!function_exists('proc_open')) {
346-
return;
347-
}
348-
349-
$descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
350-
$process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true));
351-
if (is_resource($process)) {
352-
$info = stream_get_contents($pipes[1]);
353-
fclose($pipes[1]);
354-
fclose($pipes[2]);
355-
proc_close($process);
356-
357-
return $info;
358-
}
359-
}
360-
361-
/**
362-
* Runs and parses mode CON if it's available, suppressing any error output.
363-
*
364-
* @return string <width>x<height> or null if it could not be parsed
365-
*/
366-
private function getConsoleMode()
367-
{
368-
if (!function_exists('proc_open')) {
369-
return;
370-
}
371-
372-
$descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w'));
373-
$process = proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => true));
374-
if (is_resource($process)) {
375-
$info = stream_get_contents($pipes[1]);
376-
fclose($pipes[1]);
377-
fclose($pipes[2]);
378-
proc_close($process);
379-
380-
if (preg_match('/-{8}+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) {
381-
return $matches[2].'x'.$matches[1];
382-
}
383-
}
319+
return $width;
384320
}
385321
}

0 commit comments

Comments
 (0)