Commit 725d8100 by Qiang Xue

Refactored console controller color option.

parent f3cc5d1e
...@@ -35,34 +35,23 @@ class Controller extends \yii\base\Controller ...@@ -35,34 +35,23 @@ class Controller extends \yii\base\Controller
public $interactive = true; public $interactive = true;
/** /**
* @var boolean whether to enable ANSI style in output. * @var boolean whether to enable ANSI color in the output.
* Defaults to null meaning auto-detect. * 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()]]. * ANSI color is enabled only if [[color]] is not set or is set true,
* If not set it will be auto detected using [[yii\helpers\Console::streamSupportsAnsiColors()]] with STDOUT * and the terminal must support ANSI color.
* for [[ansiFormat()]] and [[stdout()]] and STDERR for [[stderr()]]. *
* @param resource $stream * @param resource $stream the stream to check.
* @return boolean Whether to enable ANSI style in output. * @return boolean Whether to enable ANSI style in output.
*/ */
public function getColors($stream = STDOUT) public function isColorEnabled($stream = STDOUT)
{ {
if ($this->_colors === null) { return ($this->color === null || $this->color) && Console::streamSupportsAnsiColors($stream);
return Console::streamSupportsAnsiColors($stream);
}
return $this->_colors;
}
/**
* Whether to enable ANSI style in output.
*/
public function setColors($value)
{
$this->_colors = (bool) $value;
} }
/** /**
...@@ -151,6 +140,7 @@ class Controller extends \yii\base\Controller ...@@ -151,6 +140,7 @@ class Controller extends \yii\base\Controller
* You may pass additional parameters using the constants defined in [[yii\helpers\base\Console]]. * You may pass additional parameters using the constants defined in [[yii\helpers\base\Console]].
* *
* Example: * Example:
*
* ~~~ * ~~~
* $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); * $this->ansiFormat('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
* ~~~ * ~~~
...@@ -160,7 +150,7 @@ class Controller extends \yii\base\Controller ...@@ -160,7 +150,7 @@ class Controller extends \yii\base\Controller
*/ */
public function ansiFormat($string) public function ansiFormat($string)
{ {
if ($this->getColors()) { if ($this->isColorEnabled()) {
$args = func_get_args(); $args = func_get_args();
array_shift($args); array_shift($args);
$string = Console::ansiFormat($string, $args); $string = Console::ansiFormat($string, $args);
...@@ -175,6 +165,7 @@ class Controller extends \yii\base\Controller ...@@ -175,6 +165,7 @@ class Controller extends \yii\base\Controller
* passing additional parameters using the constants defined in [[yii\helpers\base\Console]]. * passing additional parameters using the constants defined in [[yii\helpers\base\Console]].
* *
* Example: * Example:
*
* ~~~ * ~~~
* $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); * $this->stdout('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
* ~~~ * ~~~
...@@ -184,7 +175,7 @@ class Controller extends \yii\base\Controller ...@@ -184,7 +175,7 @@ class Controller extends \yii\base\Controller
*/ */
public function stdout($string) public function stdout($string)
{ {
if ($this->getColors()) { if ($this->isColorEnabled()) {
$args = func_get_args(); $args = func_get_args();
array_shift($args); array_shift($args);
$string = Console::ansiFormat($string, $args); $string = Console::ansiFormat($string, $args);
...@@ -199,6 +190,7 @@ class Controller extends \yii\base\Controller ...@@ -199,6 +190,7 @@ class Controller extends \yii\base\Controller
* passing additional parameters using the constants defined in [[yii\helpers\base\Console]]. * passing additional parameters using the constants defined in [[yii\helpers\base\Console]].
* *
* Example: * Example:
*
* ~~~ * ~~~
* $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE); * $this->stderr('This will be red and underlined.', Console::FG_RED, Console::UNDERLINE);
* ~~~ * ~~~
...@@ -208,7 +200,7 @@ class Controller extends \yii\base\Controller ...@@ -208,7 +200,7 @@ class Controller extends \yii\base\Controller
*/ */
public function stderr($string) public function stderr($string)
{ {
if ($this->getColors(STDERR)) { if ($this->isColorEnabled(STDERR)) {
$args = func_get_args(); $args = func_get_args();
array_shift($args); array_shift($args);
$string = Console::ansiFormat($string, $args); $string = Console::ansiFormat($string, $args);
...@@ -221,6 +213,7 @@ class Controller extends \yii\base\Controller ...@@ -221,6 +213,7 @@ class Controller extends \yii\base\Controller
* *
* @param string $text prompt string * @param string $text prompt string
* @param array $options the options to validate the input: * @param array $options the options to validate the input:
*
* - required: whether it is required or not * - required: whether it is required or not
* - default: default value if no input is inserted by the user * - default: default value if no input is inserted by the user
* - pattern: regular expression pattern to validate user input * - pattern: regular expression pattern to validate user input
...@@ -281,6 +274,6 @@ class Controller extends \yii\base\Controller ...@@ -281,6 +274,6 @@ class Controller extends \yii\base\Controller
*/ */
public function globalOptions() public function globalOptions()
{ {
return array('colors', 'interactive'); return array('color', 'interactive');
} }
} }
...@@ -406,6 +406,10 @@ class HelpController extends Controller ...@@ -406,6 +406,10 @@ class HelpController extends Controller
if ($type === null) { if ($type === null) {
$type = gettype($defaultValue); $type = gettype($defaultValue);
} }
if (is_bool($defaultValue)) {
// show as integer to avoid confusion
$defaultValue = (int)$defaultValue;
}
$doc = "$type (defaults to " . var_export($defaultValue, true) . ")"; $doc = "$type (defaults to " . var_export($defaultValue, true) . ")";
} elseif (trim($type) !== '') { } elseif (trim($type) !== '') {
$doc = $type; $doc = $type;
......
...@@ -96,7 +96,9 @@ class MigrateController extends Controller ...@@ -96,7 +96,9 @@ class MigrateController extends Controller
*/ */
public function globalOptions() public function globalOptions()
{ {
return array('migrationPath', 'migrationTable', 'db', 'templateFile', 'interactive'); return array_merge(parent::globalOptions(), array(
'migrationPath', 'migrationTable', 'db', 'templateFile', 'interactive'
));
} }
/** /**
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment