Skip to content
Permalink
Browse files

Adding style format manipulation methods.

  • Loading branch information...
markstory committed Oct 3, 2010
1 parent 7dea9b0 commit aec1770abc3149a1a82c2817175e98a696348f13
Showing with 129 additions and 2 deletions.
  1. +101 −2 cake/console/console_output.php
  2. +28 −0 cake/tests/cases/console/console_output.test.php
@@ -1,7 +1,6 @@
<?php
/**
* ConsoleOutput an object to provide methods for generating console output.
* Can be connected to any stream resource that can be used with fopen()
* ConsoleOutput file.
*
* PHP 5
*
@@ -18,6 +17,30 @@
* @since CakePHP(tm) v 2.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* Object wrapper for outputing information from a shell application.
* Can be connected to any stream resource that can be used with fopen()
*
* Can generate colourzied output on consoles that support it. There are a few
* built in styles
*
* - `error` Error messages. Bright red text.
* - `warning` Warning messages. Bright orange text
* - `info` Informational messages. Cyan text.
*
* By defining styles with addStyle() you can create custom console styles.
*
* ### Using styles in output
*
* You can format console output using tags with the name of the style to apply. From inside a shell object
*
* `$this->out('<warning>Overwrite:</warning> foo.php was overwritten.');`
*
* This would create orange 'Overwrite:' text, while the rest of the text would remain the normal colour.
* See ConsoleOutput::styles() to learn more about defining your own styles.
*
* @package cake.console
*/
class ConsoleOutput {
/**
* File handle for output.
@@ -31,6 +54,63 @@ class ConsoleOutput {
*/
const LF = PHP_EOL;
/**
* text colors used in coloured output.
*
* @var array
*/
protected static $_foregroundColors = array(
'black' => 30,
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'white' => 37
);
/**
* background colours used in coloured output.
*
* @var array
*/
protected static $_backgroundColors = array(
'black' => 40,
'red' => 41,
'green' => 42,
'yellow' => 43,
'blue' => 44,
'magenta' => 45,
'cyan' => 46,
'white' => 47
);
/**
* formatting options for coloured output
*
* @var string
*/
protected static $_options = array(
'bold' => 1,
'underscore' => 4,
'blink' => 5,
'reverse' => 7,
'conceal' => 8
);
/**
* Styles that are available as tags in console output.
* You can modify these styles with ConsoleOutput::styles()
*
* @var array
*/
protected static $_styles = array(
'error' => array('text' => 'red'),
'warning' => array('text' => 'yellow'),
'info' => array('text' => 'cyan')
);
/**
* Construct the output object.
*
@@ -65,6 +145,25 @@ protected function _write($message) {
return fwrite($this->_output, $message);
}
/**
* Get the current styles offered, or append new ones in.
*
* @param string $style The style to get or create.
* @param mixed $definition The array definition of the style to change or create a style
* or false to remove a style.
* @return mixed
*/
function styles($style = null, $definition = null) {
if (is_string($style) && $definition === null) {
return isset(self::$_styles[$style]) ? self::$_styles[$style] : null;
}
if ($definition === false) {
unset(self::$_styles[$style]);
return true;
}
self::$_styles[$style] = $definition;
}
/**
* clean up and close handles
*
@@ -89,4 +89,32 @@ function testWriteArray() {
$this->output->write(array('Line', 'Line', 'Line'));
}
/**
* test getting a style.
*
* @return void
*/
function testStylesGet() {
$result = $this->output->styles('error');
$expected = array('text' => 'red');
$this->assertEqual($result, $expected);
$this->assertNull($this->output->styles('made_up_goop'));
}
/**
* test adding a style.
*
* @return void
*/
function testStylesAdding() {
$this->output->styles('test', array('text' => 'red', 'background' => 'black'));
$result = $this->output->styles('test');
$expected = array('text' => 'red', 'background' => 'black');
$this->assertEquals($expected, $result);
$this->assertTrue($this->output->styles('test', false), 'Removing a style should return true.');
$this->assertNull($this->output->styles('test'), 'Removed styles should be null.');
}
}

0 comments on commit aec1770

Please sign in to comment.
You can’t perform that action at this time.