Skip to content

Commit

Permalink
[Console] Updated output formatter to use style stack.
Browse files Browse the repository at this point in the history
  • Loading branch information
jfsimon committed Mar 16, 2012
1 parent 4f298dd commit a1add4b
Showing 1 changed file with 23 additions and 7 deletions.
30 changes: 23 additions & 7 deletions src/Symfony/Component/Console/Formatter/OutputFormatter.php
Expand Up @@ -23,10 +23,11 @@ class OutputFormatter implements OutputFormatterInterface
/**
* The pattern to phrase the format.
*/
const FORMAT_PATTERN = '#<([a-z][a-z0-9_=;-]+)>(.*?)</\\1?>#is';
const FORMAT_PATTERN = '#<(/?)([a-z][a-z0-9_=;-]+)?>#is';

private $decorated;
private $styles = array();
private $styleStack;

/**
* Initializes console output formatter.
Expand All @@ -48,6 +49,8 @@ public function __construct($decorated = null, array $styles = array())
foreach ($styles as $name => $style) {
$this->setStyle($name, $style);
}

$this->styleStack = new OutputFormatterStyleStack();
}

/**
Expand Down Expand Up @@ -145,20 +148,33 @@ public function format($message)
private function replaceStyle($match)
{
if (!$this->isDecorated()) {
return $match[2];
return '';
}

// Special "</>" tag resets all styles.
if (!isset($match[2])) {
$this->styleStack->reset();

return "\033[0m";
}

if (isset($this->styles[strtolower($match[1])])) {
$style = $this->styles[strtolower($match[1])];
if (isset($this->styles[strtolower($match[2])])) {
$style = $this->styles[strtolower($match[2])];
} else {
$style = $this->createStyleFromString($match[1]);
$style = $this->createStyleFromString($match[2]);

if (false === $style) {
return $match[0];
return '';
}
}

return $style->apply($this->format($match[2]));
if ('/' === $match[1]) {
$this->styleStack->popStyle($style);
} else {
$this->styleStack->pushStyle($style);
}

return $this->styleStack->getCurrentStyle()->getTerminalSequence();
}

/**
Expand Down

0 comments on commit a1add4b

Please sign in to comment.