Skip to content

Commit

Permalink
Adding formatting methods for coloured output.
Browse files Browse the repository at this point in the history
  • Loading branch information
markstory committed Oct 14, 2010
1 parent aec1770 commit d5b5fbe
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 4 deletions.
68 changes: 65 additions & 3 deletions cake/console/console_output.php
Expand Up @@ -37,7 +37,8 @@
* `$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.
* See ConsoleOutput::styles() to learn more about defining your own styles. Nested styles are not supported
* at this time.
*
* @package cake.console
*/
Expand Down Expand Up @@ -132,7 +133,47 @@ public function write($message, $newlines = 1) {
if (is_array($message)) {
$message = implode(self::LF, $message);
}
return $this->_write($message . str_repeat(self::LF, $newlines));
return $this->_write($this->styleText($message . str_repeat(self::LF, $newlines)));
}

/**
* Apply styling to text.
*
* @param string $text Text with styling tags.
* @return string String with color codes added.
*/
public function styleText($text) {
return preg_replace_callback(
'/<(?<tag>[a-z0-9-_]+)>(?<text>.*)<\/(\1)>/i', array($this, '_replaceTags'), $text
);
}

/**
* Replace tags with color codes.
*
* @param array $matches.
* @return string
*/
protected function _replaceTags($matches) {
$style = $this->styles($matches['tag']);
if (empty($style)) {
return $matches['text'];
}

$styleInfo = array();
if (!empty($style['text']) && isset(self::$_foregroundColors[$style['text']])) {
$styleInfo[] = self::$_foregroundColors[$style['text']];
}
if (!empty($style['background']) && isset(self::$_foregroundColors[$style['background']])) {
$styleInfo[] = self::$_foregroundColors[$style['background']];
}
unset($style['text'], $style['background']);
foreach ($style as $option => $value) {
if ($value) {
$styleInfo[] = self::$_options[$option];
}
}
return "\033[" . implode($styleInfo, ';') . 'm' . $matches['text'] . "\033[0m";
}

/**
Expand All @@ -148,12 +189,32 @@ protected function _write($message) {
/**
* Get the current styles offered, or append new ones in.
*
* ### Get a style definition
*
* `$this->output->styles('error');`
*
* ### Get all the style definitions
*
* `$this->output->styles();`
*
* ### Create or modify an existing style
*
* `$this->output->styles('annoy', array('text' => 'purple', 'background' => 'yellow', 'blink' => true));`
*
* ### Remove a style
*
* `$this->output->styles('annoy', false);`
*
* @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
* @return mixed If you are getting styles, the style or null will be returned. If you are creating/modifying
* styles true will be returned.
*/
function styles($style = null, $definition = null) {
if ($style === null && $definition === null) {
return self::$_styles;
}
if (is_string($style) && $definition === null) {
return isset(self::$_styles[$style]) ? self::$_styles[$style] : null;
}
Expand All @@ -162,6 +223,7 @@ function styles($style = null, $definition = null) {
return true;
}
self::$_styles[$style] = $definition;
return true;
}

/**
Expand Down
42 changes: 41 additions & 1 deletion cake/tests/cases/console/console_output.test.php
Expand Up @@ -98,8 +98,12 @@ function testStylesGet() {
$result = $this->output->styles('error');
$expected = array('text' => 'red');
$this->assertEqual($result, $expected);

$this->assertNull($this->output->styles('made_up_goop'));

$result = $this->output->styles();
$this->assertNotEmpty($result, 'error', 'Error is missing');
$this->assertNotEmpty($result, 'warning', 'Warning is missing');
}

/**
Expand All @@ -117,4 +121,40 @@ function testStylesAdding() {
$this->assertNull($this->output->styles('test'), 'Removed styles should be null.');
}

/**
* test formatting text with styles.
*
* @return void
*/
function testFormattingSimple() {
$this->output->expects($this->once())->method('_write')
->with("\033[31mError:\033[0m Something bad");

$this->output->write('<error>Error:</error> Something bad', false);
}

/**
* test formatting text with missing styles.
*
* @return void
*/
function testFormattingMissingStyleName() {
$this->output->expects($this->once())->method('_write')
->with("Error: Something bad");

$this->output->write('<not_there>Error:</not_there> Something bad', false);
}

/**
* test formatting text with multiple styles.
*
* @return void
*/
function testFormattingMultipleStylesName() {
$this->output->expects($this->once())->method('_write')
->with("\033[31mBad\033[0m \033[33mWarning\033[0m Regular");

$this->output->write('<error>Bad</error> <warning>Warning</warning> Regular', false);
}

}

0 comments on commit d5b5fbe

Please sign in to comment.