public function testForeground() { $style = new OutputFormatterStyle(); $style->setForeground('black'); $this->assertEquals("[30mfoo[0m", $style->apply('foo')); $style->setForeground('blue'); $this->assertEquals("[34mfoo[0m", $style->apply('foo')); $this->setExpectedException('InvalidArgumentException'); $style->setForeground('undefined-color'); }
/** * {@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; }
/** * 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); }
/** * 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; }
/** * 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; }
/** * Sets the foreground colour * * @author Art <*****@*****.**> * * @param string $fg The foreground colour * * @codeCoverageIgnore - nothing to test. */ public function setForeground($fg = null) { $this->symfony->setForeground($fg); }