From 725d81008514c7eec1285d749c4941454fac6146 Mon Sep 17 00:00:00 2001 From: Qiang Xue Date: Fri, 14 Jun 2013 11:16:56 -0400 Subject: [PATCH] Refactored console controller color option. --- framework/yii/console/Controller.php | 43 +++++++++------------- .../yii/console/controllers/HelpController.php | 4 ++ .../yii/console/controllers/MigrateController.php | 4 +- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/framework/yii/console/Controller.php b/framework/yii/console/Controller.php index 79e9f38..84d9d11 100644 --- a/framework/yii/console/Controller.php +++ b/framework/yii/console/Controller.php @@ -35,34 +35,23 @@ class Controller extends \yii\base\Controller public $interactive = true; /** - * @var boolean whether to enable ANSI style in output. - * Defaults to null meaning auto-detect. + * @var boolean whether to enable ANSI color in the output. + * If not set, ANSI color will be enabled for terminals that support it. */ - private $_colors; + public $color; /** - * Whether to enable ANSI style in output. + * Returns a value indicating whether ANSI color is enabled. * - * Setting this will affect [[ansiFormat()]], [[stdout()]] and [[stderr()]]. - * If not set it will be auto detected using [[yii\helpers\Console::streamSupportsAnsiColors()]] with STDOUT - * for [[ansiFormat()]] and [[stdout()]] and STDERR for [[stderr()]]. - * @param resource $stream + * ANSI color is enabled only if [[color]] is not set or is set true, + * and the terminal must support ANSI color. + * + * @param resource $stream the stream to check. * @return boolean Whether to enable ANSI style in output. */ - public function getColors($stream = STDOUT) + public function isColorEnabled($stream = STDOUT) { - if ($this->_colors === null) { - return Console::streamSupportsAnsiColors($stream); - } - return $this->_colors; - } - - /** - * Whether to enable ANSI style in output. - */ - public function setColors($value) - { - $this->_colors = (bool) $value; + return ($this->color === null || $this->color) && Console::streamSupportsAnsiColors($stream); } /** @@ -151,6 +140,7 @@ class Controller extends \yii\base\Controller * You may pass additional parameters using the constants defined in [[yii\helpers\base\Console]]. * * Example: + * * ~~~ * $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); * ~~~ @@ -160,7 +150,7 @@ class Controller extends \yii\base\Controller */ public function ansiFormat($string) { - if ($this->getColors()) { + if ($this->isColorEnabled()) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); @@ -175,6 +165,7 @@ class Controller extends \yii\base\Controller * passing additional parameters using the constants defined in [[yii\helpers\base\Console]]. * * Example: + * * ~~~ * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); * ~~~ @@ -184,7 +175,7 @@ class Controller extends \yii\base\Controller */ public function stdout($string) { - if ($this->getColors()) { + if ($this->isColorEnabled()) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); @@ -199,6 +190,7 @@ class Controller extends \yii\base\Controller * passing additional parameters using the constants defined in [[yii\helpers\base\Console]]. * * Example: + * * ~~~ * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); * ~~~ @@ -208,7 +200,7 @@ class Controller extends \yii\base\Controller */ public function stderr($string) { - if ($this->getColors(STDERR)) { + if ($this->isColorEnabled(STDERR)) { $args = func_get_args(); array_shift($args); $string = Console::ansiFormat($string, $args); @@ -221,6 +213,7 @@ class Controller extends \yii\base\Controller * * @param string $text prompt string * @param array $options the options to validate the input: + * * - required: whether it is required or not * - default: default value if no input is inserted by the user * - pattern: regular expression pattern to validate user input @@ -281,6 +274,6 @@ class Controller extends \yii\base\Controller */ public function globalOptions() { - return array('colors', 'interactive'); + return array('color', 'interactive'); } } diff --git a/framework/yii/console/controllers/HelpController.php b/framework/yii/console/controllers/HelpController.php index 9319163..5afb543 100644 --- a/framework/yii/console/controllers/HelpController.php +++ b/framework/yii/console/controllers/HelpController.php @@ -406,6 +406,10 @@ class HelpController extends Controller if ($type === null) { $type = gettype($defaultValue); } + if (is_bool($defaultValue)) { + // show as integer to avoid confusion + $defaultValue = (int)$defaultValue; + } $doc = "$type (defaults to " . var_export($defaultValue, true) . ")"; } elseif (trim($type) !== '') { $doc = $type; diff --git a/framework/yii/console/controllers/MigrateController.php b/framework/yii/console/controllers/MigrateController.php index eca7787..7cb333a 100644 --- a/framework/yii/console/controllers/MigrateController.php +++ b/framework/yii/console/controllers/MigrateController.php @@ -96,7 +96,9 @@ class MigrateController extends Controller */ public function globalOptions() { - return array('migrationPath', 'migrationTable', 'db', 'templateFile', 'interactive'); + return array_merge(parent::globalOptions(), array( + 'migrationPath', 'migrationTable', 'db', 'templateFile', 'interactive' + )); } /**