1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82:
<?php
namespace Webmozart\Console\Formatter;
use Symfony\Component\Console\Formatter\OutputFormatter;
use Webmozart\Console\Adapter\StyleConverter;
use Webmozart\Console\Api\Formatter\Formatter;
use Webmozart\Console\Api\Formatter\Style;
use Webmozart\Console\Api\Formatter\StyleSet;
class AnsiFormatter implements Formatter
{
private $innerFormatter;
public function __construct(StyleSet $styleSet = null)
{
$this->innerFormatter = new OutputFormatter(true);
if (!$styleSet) {
$styleSet = new DefaultStyleSet();
}
foreach ($styleSet->toArray() as $tag => $style) {
$this->innerFormatter->setStyle($tag, StyleConverter::convert($style));
}
}
public function format($string, Style $style = null)
{
if (null !== $style) {
$this->innerFormatter->getStyleStack()->push(StyleConverter::convert($style));
}
$formatted = $this->innerFormatter->format($string);
if (null !== $style) {
$this->innerFormatter->getStyleStack()->pop();
}
return $formatted;
}
public function removeFormat($string)
{
$this->innerFormatter->setDecorated(false);
$formatted = $this->innerFormatter->format($string);
$this->innerFormatter->setDecorated(true);
return $formatted;
}
}