From cfaa122ade9b5bb474a71cefd7dcbf2a713c029a Mon Sep 17 00:00:00 2001 From: johnstevenson Date: Wed, 10 Aug 2016 19:08:31 +0100 Subject: [PATCH] Ensure colored output if restarted from a capable terminal --- src/Composer/XdebugHandler.php | 52 ++++++++++++++++++++++++++++++++-- 1 file changed, 50 insertions(+), 2 deletions(-) diff --git a/src/Composer/XdebugHandler.php b/src/Composer/XdebugHandler.php index ca2887eb2..54fb11447 100644 --- a/src/Composer/XdebugHandler.php +++ b/src/Composer/XdebugHandler.php @@ -112,7 +112,9 @@ class XdebugHandler /** * Writes the temporary ini file, or clears its name if no ini * - * If there are no ini files, the tmp ini name is + * If there are no ini files, the tmp ini name is cleared so that + * an empty value is passed with the -c option. + * * @param array $iniFiles The php.ini locations * @param bool $replace Whether we need to modify the files * @@ -205,11 +207,57 @@ class XdebugHandler } $phpArgs = array(PHP_BINARY, '-c', $this->tmpIni); - $params = array_merge($phpArgs, $this->argv); + $params = array_merge($phpArgs, $this->getScriptArgs($this->argv)); return implode(' ', array_map(array($this, 'escape'), $params)); } + /** + * Returns the restart script arguments, adding --ansi if required + * + * @param array $args The argv array + * + * @return array + */ + private function getScriptArgs(array $args) + { + if (in_array('--no-ansi', $args) || in_array('--ansi', $args)) { + return $args; + } + + if ($this->isColorTerminal()) { + $offset = count($args) > 1 ? 2: 1; + array_splice($args, $offset, 0, '--ansi'); + } + + return $args; + } + + /** + * Returns whether we are a terminal and have colour capabilities + * + * @return bool + */ + private function isColorTerminal() + { + if (function_exists('posix_isatty')) { + $result = posix_isatty(STDOUT); + } else { + // See if STDOUT is a character device (S_IFCHR) + $stat = fstat(STDOUT); + $result = ($stat['mode'] & 0170000) === 0020000; + } + + if ($result && defined('PHP_WINDOWS_VERSION_BUILD')) { + $result = 0 >= version_compare('10.0.10586', PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD) + || false !== getenv('ANSICON') + || 'ON' === getenv('ConEmuANSI') + || 'xterm' === getenv('TERM'); + } + + return $result; + } + /** * Escapes a string to be used as a shell argument. *