Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function createOutput($stream = null)
 {
     $stream = $stream ?: $this->createOutputStream();
     $format = $this->createOutputFormatter();
     // set user-defined styles
     foreach ($this->getOutputStyles() as $name => $options) {
         $style = new OutputFormatterStyle();
         if (isset($options[0])) {
             $style->setForeground($options[0]);
         }
         if (isset($options[1])) {
             $style->setBackground($options[1]);
         }
         if (isset($options[2])) {
             $style->setOptions($options[2]);
         }
         $format->setStyle($name, $style);
     }
     $output = new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, $this->isOutputDecorated(), $format);
     $this->configureOutputStream($output);
     return $output;
 }
Пример #2
0
 /**
  * Returns new output console.
  *
  * @return StreamOutput
  *
  * @uses createOutputStream()
  */
 protected function createOutputConsole()
 {
     $stream = $this->createOutputStream();
     $format = new OutputFormatter();
     // set user-defined styles
     foreach ($this->parameters->get('output_styles') as $name => $options) {
         $style = new OutputFormatterStyle();
         if (isset($options[0])) {
             $style->setForeground($options[0]);
         }
         if (isset($options[1])) {
             $style->setBackground($options[1]);
         }
         if (isset($options[2])) {
             $style->setOptions($options[2]);
         }
         $format->setStyle($name, $style);
     }
     return new StreamOutput($stream, StreamOutput::VERBOSITY_NORMAL, $this->parameters->get('output_decorate'), $format);
 }
Пример #3
0
 /**
  * Tries to create new style instance from string.
  *
  * @param string $string        	
  *
  * @return OutputFormatterStyle|bool false if string is not format string
  */
 private function createStyleFromString($string)
 {
     if (isset($this->styles[$string])) {
         return $this->styles[$string];
     }
     if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', strtolower($string), $matches, PREG_SET_ORDER)) {
         return false;
     }
     $style = new OutputFormatterStyle();
     foreach ($matches as $match) {
         array_shift($match);
         if ('fg' == $match[0]) {
             $style->setForeground($match[1]);
         } elseif ('bg' == $match[0]) {
             $style->setBackground($match[1]);
         } else {
             try {
                 $style->setOption($match[1]);
             } catch (\InvalidArgumentException $e) {
                 return false;
             }
         }
     }
     return $style;
 }
 public function testOptions()
 {
     $style = new OutputFormatterStyle();
     $style->setOptions(array('reverse', 'conceal'));
     $this->assertEquals("foo", $style->apply('foo'));
     $style->setOption('bold');
     $this->assertEquals("foo", $style->apply('foo'));
     $style->unsetOption('reverse');
     $this->assertEquals("foo", $style->apply('foo'));
     $style->setOption('bold');
     $this->assertEquals("foo", $style->apply('foo'));
     $style->setOptions(array('bold'));
     $this->assertEquals("foo", $style->apply('foo'));
     try {
         $style->setOption('foo');
         $this->fail('->setOption() throws an \\InvalidArgumentException when the option does not exist in the available options');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->setOption() throws an \\InvalidArgumentException when the option does not exist in the available options');
         $this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->setOption() throws an \\InvalidArgumentException when the option does not exist in the available options');
     }
     try {
         $style->unsetOption('foo');
         $this->fail('->unsetOption() throws an \\InvalidArgumentException when the option does not exist in the available options');
     } catch (\Exception $e) {
         $this->assertInstanceOf('\\InvalidArgumentException', $e, '->unsetOption() throws an \\InvalidArgumentException when the option does not exist in the available options');
         $this->assertContains('Invalid option specified: "foo"', $e->getMessage(), '->unsetOption() throws an \\InvalidArgumentException when the option does not exist in the available options');
     }
 }
 public function testOptions()
 {
     $style = new OutputFormatterStyle();
     $style->setOptions(array('reverse', 'conceal'));
     $this->assertEquals("foo", $style->apply('foo'));
     $style->setOption('bold');
     $this->assertEquals("foo", $style->apply('foo'));
     $style->unsetOption('reverse');
     $this->assertEquals("foo", $style->apply('foo'));
     $style->setOption('bold');
     $this->assertEquals("foo", $style->apply('foo'));
     $style->setOptions(array('bold'));
     $this->assertEquals("foo", $style->apply('foo'));
 }
Пример #6
0
 /**
  * Tries to create new style instance from string.
  *
  * @param string $string
  *
  * @return OutputFormatterStyle|bool false if string is not format string
  */
 private function createStyleFromString($string)
 {
     if (isset($this->styles[$string])) {
         return $this->styles[$string];
     }
     if (!preg_match_all('/([^=]+)=([^;]+)(;|$)/', $string, $matches, PREG_SET_ORDER)) {
         return false;
     }
     $style = new OutputFormatterStyle();
     foreach ($matches as $match) {
         array_shift($match);
         if ('fg' == $match[0]) {
             $style->setForeground($match[1]);
         } elseif ('bg' == $match[0]) {
             $style->setBackground($match[1]);
         } elseif ('options' === $match[0]) {
             preg_match_all('([^,;]+)', $match[1], $options);
             $options = array_shift($options);
             foreach ($options as $option) {
                 try {
                     $style->setOption($option);
                 } catch (\InvalidArgumentException $e) {
                     @trigger_error(sprintf('Unknown style options are deprecated since version 3.2 and will be removed in 4.0. Exception "%s".', $e->getMessage()), E_USER_DEPRECATED);
                     return false;
                 }
             }
         } else {
             return false;
         }
     }
     return $style;
 }
Пример #7
0
 /**
  * Applies the style to a given text.
  *
  * @param string $text The text to style
  *
  * @return string
  */
 public function apply($text)
 {
     $ret = $text;
     // ##################
     // Padding
     // ##################
     if (!empty($this->padding)) {
         $ret = $this->padding . $ret;
     }
     // ##################
     // Wrap
     // ##################
     if (!empty($this->wrap)) {
         list($width) = $this->application->getTerminalDimensions();
         $length = strlen($text);
         $wrapLength = (int) ($width - $length - 2) / 2 * 0.5;
         if ($wrapLength >= 1) {
             $ret = str_repeat($this->wrap, $wrapLength) . ' ' . $ret . ' ' . str_repeat($this->wrap, $wrapLength);
         }
     }
     $ret = parent::apply($ret);
     // ##################
     // Padding
     // ##################
     if (!empty($this->paddingOutside)) {
         $ret = $this->paddingOutside . $ret;
     }
     return $ret;
 }
Пример #8
0
 /**
  * Applies the style to a given text.
  *
  * @param string $text The text to style
  *
  * @return string
  *
  * @codeCoverageIgnore - nothing to test.
  */
 public function apply($text)
 {
     return $this->symfony->apply($text);
 }
Пример #9
0
 public function __toString()
 {
     if (!$this->getIconHandler()->getRuntimeConfig()->hasUnicodeIconSupport()) {
         return '';
     }
     $colour = null;
     $backgroundColour = null;
     $options = array();
     if (!is_null($this->data['colour'])) {
         $colour = $this->data['colour'];
     }
     if (!is_null($this->data['bgColour'])) {
         $backgroundColour = $this->data['bgColour'];
     }
     if (!empty($this->data['options'])) {
         $options = $this->data['options'];
     }
     $style = new OutputFormatterStyle($colour, $backgroundColour, $options);
     $output = $style->apply($this->data['icon']);
     return $output;
 }