[Console] Updated output formatter to use style stack.

This commit is contained in:
Jean-François Simon 2012-03-16 09:44:39 +01:00
parent 4f298dd7c7
commit a1add4b8d5

View File

@ -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.
@ -48,6 +49,8 @@ class OutputFormatter implements OutputFormatterInterface
foreach ($styles as $name => $style) {
$this->setStyle($name, $style);
}
$this->styleStack = new OutputFormatterStyleStack();
}
/**
@ -145,20 +148,33 @@ class OutputFormatter implements OutputFormatterInterface
private function replaceStyle($match)
{
if (!$this->isDecorated()) {
return $match[2];
return '';
}
if (isset($this->styles[strtolower($match[1])])) {
$style = $this->styles[strtolower($match[1])];
// Special "</>" tag resets all styles.
if (!isset($match[2])) {
$this->styleStack->reset();
return "\033[0m";
}
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();
}
/**