-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[Console] progress bar fix #19012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Console] progress bar fix #19012
Changes from all commits
b030c24
8f206c8
a589635
bf7a5c5
2f81247
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,20 +65,19 @@ class Application | |
private $definition; | ||
private $helperSet; | ||
private $dispatcher; | ||
private $terminalDimensions; | ||
private $terminal; | ||
private $defaultCommand; | ||
private $singleCommand; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param string $name The name of the application | ||
* @param string $version The version of the application | ||
*/ | ||
public function __construct($name = 'UNKNOWN', $version = 'UNKNOWN') | ||
{ | ||
$this->name = $name; | ||
$this->version = $version; | ||
$this->terminal = new Terminal(); | ||
$this->defaultCommand = 'list'; | ||
$this->helperSet = $this->getDefaultHelperSet(); | ||
$this->definition = $this->getDefaultInputDefinition(); | ||
|
@@ -625,7 +624,7 @@ public function renderException(\Exception $e, OutputInterface $output) | |
|
||
$len = $this->stringWidth($title); | ||
|
||
$width = $this->getTerminalWidth() ? $this->getTerminalWidth() - 1 : PHP_INT_MAX; | ||
$width = $this->terminal->getWidth() ? $this->terminal->getWidth() - 1 : PHP_INT_MAX; | ||
// HHVM only accepts 32 bits integer in str_split, even when PHP_INT_MAX is a 64 bit integer: https://github.com/facebook/hhvm/issues/1327 | ||
if (defined('HHVM_VERSION') && $width > 1 << 31) { | ||
$width = 1 << 31; | ||
|
@@ -689,60 +688,42 @@ public function renderException(\Exception $e, OutputInterface $output) | |
* Tries to figure out the terminal width in which this application runs. | ||
* | ||
* @return int|null | ||
* | ||
* @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead. | ||
*/ | ||
protected function getTerminalWidth() | ||
{ | ||
$dimensions = $this->getTerminalDimensions(); | ||
@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); | ||
|
||
return $dimensions[0]; | ||
return $this->terminal->getWidth(); | ||
} | ||
|
||
/** | ||
* Tries to figure out the terminal height in which this application runs. | ||
* | ||
* @return int|null | ||
* | ||
* @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead. | ||
*/ | ||
protected function getTerminalHeight() | ||
{ | ||
$dimensions = $this->getTerminalDimensions(); | ||
@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); | ||
|
||
return $dimensions[1]; | ||
return $this->terminal->getHeight(); | ||
} | ||
|
||
/** | ||
* Tries to figure out the terminal dimensions based on the current environment. | ||
* | ||
* @return array Array containing width and height | ||
* | ||
* @deprecated since version 3.2, to be removed in 4.0. Create a Terminal instance instead. | ||
*/ | ||
public function getTerminalDimensions() | ||
{ | ||
if ($this->terminalDimensions) { | ||
return $this->terminalDimensions; | ||
} | ||
@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); | ||
|
||
if ('\\' === DIRECTORY_SEPARATOR) { | ||
// extract [w, H] from "wxh (WxH)" | ||
if (preg_match('/^(\d+)x\d+ \(\d+x(\d+)\)$/', trim(getenv('ANSICON')), $matches)) { | ||
return array((int) $matches[1], (int) $matches[2]); | ||
} | ||
// extract [w, h] from "wxh" | ||
if (preg_match('/^(\d+)x(\d+)$/', $this->getConsoleMode(), $matches)) { | ||
return array((int) $matches[1], (int) $matches[2]); | ||
} | ||
} | ||
|
||
if ($sttyString = $this->getSttyColumns()) { | ||
// extract [w, h] from "rows h; columns w;" | ||
if (preg_match('/rows.(\d+);.columns.(\d+);/i', $sttyString, $matches)) { | ||
return array((int) $matches[2], (int) $matches[1]); | ||
} | ||
// extract [w, h] from "; h rows; w columns" | ||
if (preg_match('/;.(\d+).rows;.(\d+).columns/i', $sttyString, $matches)) { | ||
return array((int) $matches[2], (int) $matches[1]); | ||
} | ||
} | ||
|
||
return array(null, null); | ||
return array($this->terminal->getWidth(), $this->terminal->getHeight()); | ||
} | ||
|
||
/** | ||
|
@@ -754,10 +735,15 @@ public function getTerminalDimensions() | |
* @param int $height The height | ||
* | ||
* @return Application The current application | ||
* | ||
* @deprecated since version 3.2, to be removed in 4.0. Set the COLUMNS and LINES env vars instead. | ||
*/ | ||
public function setTerminalDimensions($width, $height) | ||
{ | ||
$this->terminalDimensions = array($width, $height); | ||
@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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we usually use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are using both, but this one a bit more than yours :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. never ending PRs to come :) |
||
|
||
putenv('COLUMNS='.$width); | ||
putenv('LINES='.$height); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this does not work in case the dimensions were already retrieved previously, because of the Terminal internal cache There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
||
|
||
return $this; | ||
} | ||
|
@@ -927,54 +913,6 @@ protected function getDefaultHelperSet() | |
)); | ||
} | ||
|
||
/** | ||
* Runs and parses stty -a if it's available, suppressing any error output. | ||
* | ||
* @return string | ||
*/ | ||
private function getSttyColumns() | ||
{ | ||
if (!function_exists('proc_open')) { | ||
return; | ||
} | ||
|
||
$descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')); | ||
$process = proc_open('stty -a | grep columns', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); | ||
if (is_resource($process)) { | ||
$info = stream_get_contents($pipes[1]); | ||
fclose($pipes[1]); | ||
fclose($pipes[2]); | ||
proc_close($process); | ||
|
||
return $info; | ||
} | ||
} | ||
|
||
/** | ||
* Runs and parses mode CON if it's available, suppressing any error output. | ||
* | ||
* @return string <width>x<height> or null if it could not be parsed | ||
*/ | ||
private function getConsoleMode() | ||
{ | ||
if (!function_exists('proc_open')) { | ||
return; | ||
} | ||
|
||
$descriptorspec = array(1 => array('pipe', 'w'), 2 => array('pipe', 'w')); | ||
$process = proc_open('mode CON', $descriptorspec, $pipes, null, null, array('suppress_errors' => true)); | ||
if (is_resource($process)) { | ||
$info = stream_get_contents($pipes[1]); | ||
fclose($pipes[1]); | ||
fclose($pipes[2]); | ||
proc_close($process); | ||
|
||
if (preg_match('/--------+\r?\n.+?(\d+)\r?\n.+?(\d+)\r?\n/', $info, $matches)) { | ||
return $matches[2].'x'.$matches[1]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Returns abbreviated suggestions in string format. | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please add the deprecation warnings too