Style.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkPHP [ WE CAN DO IT JUST THINK ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2006~2015 http://thinkphp.cn All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: yunwuxin <448901948@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace think\console\output\formatter;
  12. class Style
  13. {
  14. private static $availableForegroundColors = [
  15. 'black' => ['set' => 30, 'unset' => 39],
  16. 'red' => ['set' => 31, 'unset' => 39],
  17. 'green' => ['set' => 32, 'unset' => 39],
  18. 'yellow' => ['set' => 33, 'unset' => 39],
  19. 'blue' => ['set' => 34, 'unset' => 39],
  20. 'magenta' => ['set' => 35, 'unset' => 39],
  21. 'cyan' => ['set' => 36, 'unset' => 39],
  22. 'white' => ['set' => 37, 'unset' => 39],
  23. ];
  24. private static $availableBackgroundColors = [
  25. 'black' => ['set' => 40, 'unset' => 49],
  26. 'red' => ['set' => 41, 'unset' => 49],
  27. 'green' => ['set' => 42, 'unset' => 49],
  28. 'yellow' => ['set' => 43, 'unset' => 49],
  29. 'blue' => ['set' => 44, 'unset' => 49],
  30. 'magenta' => ['set' => 45, 'unset' => 49],
  31. 'cyan' => ['set' => 46, 'unset' => 49],
  32. 'white' => ['set' => 47, 'unset' => 49],
  33. ];
  34. private static $availableOptions = [
  35. 'bold' => ['set' => 1, 'unset' => 22],
  36. 'underscore' => ['set' => 4, 'unset' => 24],
  37. 'blink' => ['set' => 5, 'unset' => 25],
  38. 'reverse' => ['set' => 7, 'unset' => 27],
  39. 'conceal' => ['set' => 8, 'unset' => 28],
  40. ];
  41. private $foreground;
  42. private $background;
  43. private $options = [];
  44. /**
  45. * 初始化输出的样式
  46. * @param string|null $foreground 字体颜色
  47. * @param string|null $background 背景色
  48. * @param array $options 格式
  49. * @api
  50. */
  51. public function __construct($foreground = null, $background = null, array $options = [])
  52. {
  53. if (null !== $foreground) {
  54. $this->setForeground($foreground);
  55. }
  56. if (null !== $background) {
  57. $this->setBackground($background);
  58. }
  59. if (count($options)) {
  60. $this->setOptions($options);
  61. }
  62. }
  63. /**
  64. * 设置字体颜色
  65. * @param string|null $color 颜色名
  66. * @throws \InvalidArgumentException
  67. * @api
  68. */
  69. public function setForeground($color = null)
  70. {
  71. if (null === $color) {
  72. $this->foreground = null;
  73. return;
  74. }
  75. if (!isset(static::$availableForegroundColors[$color])) {
  76. throw new \InvalidArgumentException(sprintf('Invalid foreground color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableForegroundColors))));
  77. }
  78. $this->foreground = static::$availableForegroundColors[$color];
  79. }
  80. /**
  81. * 设置背景色
  82. * @param string|null $color 颜色名
  83. * @throws \InvalidArgumentException
  84. * @api
  85. */
  86. public function setBackground($color = null)
  87. {
  88. if (null === $color) {
  89. $this->background = null;
  90. return;
  91. }
  92. if (!isset(static::$availableBackgroundColors[$color])) {
  93. throw new \InvalidArgumentException(sprintf('Invalid background color specified: "%s". Expected one of (%s)', $color, implode(', ', array_keys(static::$availableBackgroundColors))));
  94. }
  95. $this->background = static::$availableBackgroundColors[$color];
  96. }
  97. /**
  98. * 设置字体格式
  99. * @param string $option 格式名
  100. * @throws \InvalidArgumentException When the option name isn't defined
  101. * @api
  102. */
  103. public function setOption($option)
  104. {
  105. if (!isset(static::$availableOptions[$option])) {
  106. throw new \InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
  107. }
  108. if (!in_array(static::$availableOptions[$option], $this->options)) {
  109. $this->options[] = static::$availableOptions[$option];
  110. }
  111. }
  112. /**
  113. * 重置字体格式
  114. * @param string $option 格式名
  115. * @throws \InvalidArgumentException
  116. */
  117. public function unsetOption($option)
  118. {
  119. if (!isset(static::$availableOptions[$option])) {
  120. throw new \InvalidArgumentException(sprintf('Invalid option specified: "%s". Expected one of (%s)', $option, implode(', ', array_keys(static::$availableOptions))));
  121. }
  122. $pos = array_search(static::$availableOptions[$option], $this->options);
  123. if (false !== $pos) {
  124. unset($this->options[$pos]);
  125. }
  126. }
  127. /**
  128. * 批量设置字体格式
  129. * @param array $options
  130. */
  131. public function setOptions(array $options)
  132. {
  133. $this->options = [];
  134. foreach ($options as $option) {
  135. $this->setOption($option);
  136. }
  137. }
  138. /**
  139. * 应用样式到文字
  140. * @param string $text 文字
  141. * @return string
  142. */
  143. public function apply($text)
  144. {
  145. $setCodes = [];
  146. $unsetCodes = [];
  147. if (null !== $this->foreground) {
  148. $setCodes[] = $this->foreground['set'];
  149. $unsetCodes[] = $this->foreground['unset'];
  150. }
  151. if (null !== $this->background) {
  152. $setCodes[] = $this->background['set'];
  153. $unsetCodes[] = $this->background['unset'];
  154. }
  155. if (count($this->options)) {
  156. foreach ($this->options as $option) {
  157. $setCodes[] = $option['set'];
  158. $unsetCodes[] = $option['unset'];
  159. }
  160. }
  161. if (0 === count($setCodes)) {
  162. return $text;
  163. }
  164. return sprintf("\033[%sm%s\033[%sm", implode(';', $setCodes), $text, implode(';', $unsetCodes));
  165. }
  166. }