OutputFormatterStyle.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Console\Formatter;
  11. use Symfony\Component\Console\Exception\InvalidArgumentException;
  12. /**
  13. * Formatter style class for defining styles.
  14. *
  15. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  16. */
  17. class OutputFormatterStyle implements OutputFormatterStyleInterface
  18. {
  19. private static $availableForegroundColors = array(
  20. 'black' => array('set' => 30, 'unset' => 39),
  21. 'red' => array('set' => 31, 'unset' => 39),
  22. 'green' => array('set' => 32, 'unset' => 39),
  23. 'yellow' => array('set' => 33, 'unset' => 39),
  24. 'blue' => array('set' => 34, 'unset' => 39),
  25. 'magenta' => array('set' => 35, 'unset' => 39),
  26. 'cyan' => array('set' => 36, 'unset' => 39),
  27. 'white' => array('set' => 37, 'unset' => 39),
  28. 'default' => array('set' => 39, 'unset' => 39),
  29. );
  30. private static $availableBackgroundColors = array(
  31. 'black' => array('set' => 40, 'unset' => 49),
  32. 'red' => array('set' => 41, 'unset' => 49),
  33. 'green' => array('set' => 42, 'unset' => 49),
  34. 'yellow' => array('set' => 43, 'unset' => 49),
  35. 'blue' => array('set' => 44, 'unset' => 49),
  36. 'magenta' => array('set' => 45, 'unset' => 49),
  37. 'cyan' => array('set' => 46, 'unset' => 49),
  38. 'white' => array('set' => 47, 'unset' => 49),
  39. 'default' => array('set' => 49, 'unset' => 49),
  40. );
  41. private static $availableOptions = array(
  42. 'bold' => array('set' => 1, 'unset' => 22),
  43. 'underscore' => array('set' => 4, 'unset' => 24),
  44. 'blink' => array('set' => 5, 'unset' => 25),
  45. 'reverse' => array('set' => 7, 'unset' => 27),
  46. 'conceal' => array('set' => 8, 'unset' => 28),
  47. );
  48. private $foreground;
  49. private $background;
  50. private $options = array();
  51. /**
  52. * Initializes output formatter style.
  53. *
  54. * @param string|null $foreground The style foreground color name
  55. * @param string|null $background The style background color name
  56. * @param array $options The style options
  57. */
  58. public function __construct($foreground = null, $background = null, array $options = array())
  59. {
  60. if (null !== $foreground) {
  61. $this->setForeground($foreground);
  62. }
  63. if (null !== $background) {
  64. $this->setBackground($background);
  65. }
  66. if (\count($options)) {
  67. $this->setOptions($options);
  68. }
  69. }
  70. /**
  71. * Sets style foreground color.
  72. *
  73. * @param string|null $color The color name
  74. *
  75. * @throws InvalidArgumentException When the color name isn't defined
  76. */
  77. public function setForeground($color = null)
  78. {
  79. if (null === $color) {
  80. $this->foreground = null;
  81. return;
  82. }
  83. if (!isset(static::$availableForegroundColors[$color])) {
  84. throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
  85. }
  86. $this->foreground = static::$availableForegroundColors[$color];
  87. }
  88. /**
  89. * Sets style background color.
  90. *
  91. * @param string|null $color The color name
  92. *
  93. * @throws InvalidArgumentException When the color name isn't defined
  94. */
  95. public function setBackground($color = null)
  96. {
  97. if (null === $color) {
  98. $this->background = null;
  99. return;
  100. }
  101. if (!isset(static::$availableBackgroundColors[$color])) {
  102. throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
  103. }
  104. $this->background = static::$availableBackgroundColors[$color];
  105. }
  106. /**
  107. * Sets some specific style option.
  108. *
  109. * @param string $option The option name
  110. *
  111. * @throws InvalidArgumentException When the option name isn't defined
  112. */
  113. public function setOption($option)
  114. {
  115. if (!isset(static::$availableOptions[$option])) {
  116. throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
  117. }
  118. if (!\in_array(static::$availableOptions[$option], $this->options)) {
  119. $this->options[] = static::$availableOptions[$option];
  120. }
  121. }
  122. /**
  123. * Unsets some specific style option.
  124. *
  125. * @param string $option The option name
  126. *
  127. * @throws InvalidArgumentException When the option name isn't defined
  128. */
  129. public function unsetOption($option)
  130. {
  131. if (!isset(static::$availableOptions[$option])) {
  132. throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
  133. }
  134. $pos = array_search(static::$availableOptions[$option], $this->options);
  135. if (false !== $pos) {
  136. unset($this->options[$pos]);
  137. }
  138. }
  139. /**
  140. * {@inheritdoc}
  141. */
  142. public function setOptions(array $options)
  143. {
  144. $this->options = array();
  145. foreach ($options as $option) {
  146. $this->setOption($option);
  147. }
  148. }
  149. /**
  150. * Applies the style to a given text.
  151. *
  152. * @param string $text The text to style
  153. *
  154. * @return string
  155. */
  156. public function apply($text)
  157. {
  158. $setCodes = array();
  159. $unsetCodes = array();
  160. if (null !== $this->foreground) {
  161. $setCodes[] = $this->foreground['set'];
  162. $unsetCodes[] = $this->foreground['unset'];
  163. }
  164. if (null !== $this->background) {
  165. $setCodes[] = $this->background['set'];
  166. $unsetCodes[] = $this->background['unset'];
  167. }
  168. if (\count($this->options)) {
  169. foreach ($this->options as $option) {
  170. $setCodes[] = $option['set'];
  171. $unsetCodes[] = $option['unset'];
  172. }
  173. }
  174. if (0 === \count($setCodes)) {
  175. return $text;
  176. }
  177. return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
  178. }
  179. }