Adding a few new constants and a new method to control colouring of output. Added raw mode for outputting things like XML.

This commit is contained in:
mark_story 2010-10-22 23:30:08 -04:00
parent c3b70dccb3
commit 0be2208429
2 changed files with 55 additions and 11 deletions

View file

@ -44,6 +44,26 @@
* @package cake.console
*/
class ConsoleOutput {
/**
* Raw output constant - no modification of output text.
*/
const RAW = 0;
/**
* Plain output - tags will be stripped.
*/
const PLAIN = 1;
/**
* Colour output - Convert known tags in to ANSI color escape codes.
*/
const COLOR = 2;
/**
* Constant for a newline.
*/
const LF = PHP_EOL;
/**
* File handle for output.
*
@ -52,16 +72,11 @@ class ConsoleOutput {
protected $_output;
/**
* Is set to true for consoles that can take pretty output. (Not windows).
* The current output type. Manipulated with ConsoleOutput::outputAs();
*
* @var boolean
* @var integer.
*/
protected $_prettyOutput = true;
/**
* Constant for a newline.
*/
const LF = PHP_EOL;
protected $_outputAs = self::COLOR;
/**
* text colors used in coloured output.
@ -132,8 +147,8 @@ class ConsoleOutput {
public function __construct($stream = 'php://stdout') {
$this->_output = fopen($stream, 'w');
if (DS == '\\') {
$this->_prettyOutput = (bool)env('ANSICON');
if (DS == '\\' && !(bool)env('ANSICON')) {
$this->_outputAs = self::PLAIN;
}
}
@ -159,7 +174,10 @@ class ConsoleOutput {
* @return string String with color codes added.
*/
public function styleText($text) {
if (!$this->_prettyOutput) {
if ($this->_outputAs == self::RAW) {
return $text;
}
if ($this->_outputAs == self::PLAIN) {
return strip_tags($text);
}
return preg_replace_callback(
@ -245,6 +263,19 @@ class ConsoleOutput {
return true;
}
/**
* Get/Set the output type to use. The output type how formatting tags are treated.
*
* @param int $type The output type to use. Should be one of the class contstants.
* @return mixed Either null or the value if getting.
*/
public function outputAs($type = null) {
if ($type === null) {
return $this->_outputAs;
}
$this->_outputAs = $type;
}
/**
* clean up and close handles
*

View file

@ -200,4 +200,17 @@ class ConsoleOutputTest extends CakeTestCase {
$this->output->write('<error>Bad</error> <error>Warning</error> Regular', false);
}
/**
* test raw output not getting tags replaced.
*
* @return void
*/
function testOutputAsRaw() {
$this->output->outputAs(ConsoleOutput::RAW);
$this->output->expects($this->once())->method('_write')
->with('<error>Bad</error> Regular');
$this->output->write('<error>Bad</error> Regular', false);
}
}