Skip to content

Commit

Permalink
[Console] added a way to use style without defining a name (<fg=blue;…
Browse files Browse the repository at this point in the history
…bg=red>...</>)
  • Loading branch information
fabpot committed Jul 1, 2010
1 parent b31c3e0 commit aaa6aba
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/Symfony/Components/Console/Output/Output.php
Expand Up @@ -175,9 +175,9 @@ abstract public function doWrite($message, $newline);
*/
protected function format($message)
{
$message = preg_replace_callback('#<([a-z][a-z0-9\-_]+)>#i', array($this, 'replaceStartStyle'), $message);
$message = preg_replace_callback('#<([a-z][a-z0-9\-_=;]+)>#i', array($this, 'replaceStartStyle'), $message);

return preg_replace_callback('#</([a-z][a-z0-9\-_]+)>#i', array($this, 'replaceEndStyle'), $message);
return preg_replace_callback('#</([a-z][a-z0-9\-_]*)?>#i', array($this, 'replaceEndStyle'), $message);
}

/**
Expand All @@ -189,11 +189,20 @@ protected function replaceStartStyle($match)
return '';
}

if (!isset(static::$styles[strtolower($match[1])])) {
throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
if (isset(static::$styles[strtolower($match[1])])) {
$parameters = static::$styles[strtolower($match[1])];
} else {
// bg=blue;fg=red
if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($match[1]), $matches, PREG_SET_ORDER)) {
throw new \InvalidArgumentException(sprintf('Unknown style "%s".', $match[1]));
}

$parameters = array();
foreach ($matches as $match) {
$parameters[$match[1]] = $match[2];
}
}

$parameters = static::$styles[strtolower($match[1])];
$codes = array();

if (isset($parameters['fg'])) {
Expand Down

0 comments on commit aaa6aba

Please sign in to comment.