OutputFormatterStyle.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 = [
  20. 'black' => ['set' => 30, 'unset' => 39],
  21. 'red' => ['set' => 31, 'unset' => 39],
  22. 'green' => ['set' => 32, 'unset' => 39],
  23. 'yellow' => ['set' => 33, 'unset' => 39],
  24. 'blue' => ['set' => 34, 'unset' => 39],
  25. 'magenta' => ['set' => 35, 'unset' => 39],
  26. 'cyan' => ['set' => 36, 'unset' => 39],
  27. 'white' => ['set' => 37, 'unset' => 39],
  28. 'default' => ['set' => 39, 'unset' => 39],
  29. ];
  30. private static $availableBackgroundColors = [
  31. 'black' => ['set' => 40, 'unset' => 49],
  32. 'red' => ['set' => 41, 'unset' => 49],
  33. 'green' => ['set' => 42, 'unset' => 49],
  34. 'yellow' => ['set' => 43, 'unset' => 49],
  35. 'blue' => ['set' => 44, 'unset' => 49],
  36. 'magenta' => ['set' => 45, 'unset' => 49],
  37. 'cyan' => ['set' => 46, 'unset' => 49],
  38. 'white' => ['set' => 47, 'unset' => 49],
  39. 'default' => ['set' => 49, 'unset' => 49],
  40. ];
  41. private static $availableOptions = [
  42. 'bold' => ['set' => 1, 'unset' => 22],
  43. 'underscore' => ['set' => 4, 'unset' => 24],
  44. 'blink' => ['set' => 5, 'unset' => 25],
  45. 'reverse' => ['set' => 7, 'unset' => 27],
  46. 'conceal' => ['set' => 8, 'unset' => 28],
  47. ];
  48. private $foreground;
  49. private $background;
  50. private $options = [];
  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(string $foreground = null, string $background = null, array $options = [])
  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. * {@inheritdoc}
  72. */
  73. public function setForeground($color = null)
  74. {
  75. if (null === $color) {
  76. $this->foreground = null;
  77. return;
  78. }
  79. if (!isset(static::$availableForegroundColors[$color])) {
  80. throw new InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
  81. }
  82. $this->foreground = static::$availableForegroundColors[$color];
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function setBackground($color = null)
  88. {
  89. if (null === $color) {
  90. $this->background = null;
  91. return;
  92. }
  93. if (!isset(static::$availableBackgroundColors[$color])) {
  94. throw new InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
  95. }
  96. $this->background = static::$availableBackgroundColors[$color];
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function setOption($option)
  102. {
  103. if (!isset(static::$availableOptions[$option])) {
  104. throw new InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
  105. }
  106. if (!\in_array(static::$availableOptions[$option], $this->options)) {
  107. $this->options[] = static::$availableOptions[$option];
  108. }
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function unsetOption($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. $pos = array_search(static::$availableOptions[$option], $this->options);
  119. if (false !== $pos) {
  120. unset($this->options[$pos]);
  121. }
  122. }
  123. /**
  124. * {@inheritdoc}
  125. */
  126. public function setOptions(array $options)
  127. {
  128. $this->options = [];
  129. foreach ($options as $option) {
  130. $this->setOption($option);
  131. }
  132. }
  133. /**
  134. * {@inheritdoc}
  135. */
  136. public function apply($text)
  137. {
  138. $setCodes = [];
  139. $unsetCodes = [];
  140. if (null !== $this->foreground) {
  141. $setCodes[] = $this->foreground['set'];
  142. $unsetCodes[] = $this->foreground['unset'];
  143. }
  144. if (null !== $this->background) {
  145. $setCodes[] = $this->background['set'];
  146. $unsetCodes[] = $this->background['unset'];
  147. }
  148. if (\count($this->options)) {
  149. foreach ($this->options as $option) {
  150. $setCodes[] = $option['set'];
  151. $unsetCodes[] = $option['unset'];
  152. }
  153. }
  154. if (0 === \count($setCodes)) {
  155. return $text;
  156. }
  157. return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
  158. }
  159. }