CliEcho.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. namespace app\admin\service\console;
  3. class CliEcho
  4. {
  5. private array $foreground_colors = [];
  6. private array $background_colors = [];
  7. private static array $foregroundColors = [
  8. 'black' => '0;30',
  9. 'dark_gray' => '1;30',
  10. 'blue' => '0;34',
  11. 'light_blue' => '1;34',
  12. 'green' => '0;32',
  13. 'light_green' => '1;32',
  14. 'cyan' => '0;36',
  15. 'light_cyan' => '1;36',
  16. 'red' => '0;31',
  17. 'light_red' => '1;31',
  18. 'purple' => '0;35',
  19. 'light_purple' => '1;35',
  20. 'brown' => '0;33',
  21. 'yellow' => '1;33',
  22. 'light_gray' => '0;37',
  23. 'white' => '1;37',
  24. ];
  25. private static $backgroundColors = [
  26. 'black' => '40',
  27. 'red' => '41',
  28. 'green' => '42',
  29. 'yellow' => '43',
  30. 'blue' => '44',
  31. 'magenta' => '45',
  32. 'cyan' => '46',
  33. 'light_gray' => '47',
  34. ];
  35. public function __construct()
  36. {
  37. // Set up shell colors
  38. $this->foreground_colors['black'] = '0;30';
  39. $this->foreground_colors['dark_gray'] = '1;30';
  40. $this->foreground_colors['blue'] = '0;34';
  41. $this->foreground_colors['light_blue'] = '1;34';
  42. $this->foreground_colors['green'] = '0;32';
  43. $this->foreground_colors['light_green'] = '1;32';
  44. $this->foreground_colors['cyan'] = '0;36';
  45. $this->foreground_colors['light_cyan'] = '1;36';
  46. $this->foreground_colors['red'] = '0;31';
  47. $this->foreground_colors['light_red'] = '1;31';
  48. $this->foreground_colors['purple'] = '0;35';
  49. $this->foreground_colors['light_purple'] = '1;35';
  50. $this->foreground_colors['brown'] = '0;33';
  51. $this->foreground_colors['yellow'] = '1;33';
  52. $this->foreground_colors['light_gray'] = '0;37';
  53. $this->foreground_colors['white'] = '1;37';
  54. $this->background_colors['black'] = '40';
  55. $this->background_colors['red'] = '41';
  56. $this->background_colors['green'] = '42';
  57. $this->background_colors['yellow'] = '43';
  58. $this->background_colors['blue'] = '44';
  59. $this->background_colors['magenta'] = '45';
  60. $this->background_colors['cyan'] = '46';
  61. $this->background_colors['light_gray'] = '47';
  62. }
  63. // Returns colored string
  64. public function getColoredString($string, $foreground_color = null, $background_color = null, $new_line = false): string
  65. {
  66. $colored_string = '';
  67. // Check if given foreground color found
  68. if (isset($this->foreground_colors[$foreground_color])) {
  69. $colored_string .= "\033[" . $this->foreground_colors[$foreground_color] . 'm';
  70. }
  71. // Check if given background color found
  72. if (isset($this->background_colors[$background_color])) {
  73. $colored_string .= "\033[" . $this->background_colors[$background_color] . 'm';
  74. }
  75. // Add string and end coloring
  76. $colored_string .= $string . "\033[0m";
  77. return $new_line ? $colored_string . PHP_EOL : $colored_string;
  78. }
  79. // Returns all foreground color names
  80. public function getForegroundColors(): array
  81. {
  82. return array_keys($this->foreground_colors);
  83. }
  84. // Returns all background color names
  85. public function getBackgroundColors(): array
  86. {
  87. return array_keys($this->background_colors);
  88. }
  89. /**
  90. * 获取带颜色的文字.
  91. *
  92. * @param string $string black|dark_gray|blue|light_blue|green|light_green|cyan|light_cyan|red|light_red|purple|brown|yellow|light_gray|white
  93. * @param string|null $foregroundColor 前景颜色 black|red|green|yellow|blue|magenta|cyan|light_gray
  94. * @param string|null $backgroundColor 背景颜色 同$foregroundColor
  95. *
  96. * @return string
  97. */
  98. public static function initColoredString(
  99. string $string,
  100. ?string $foregroundColor = null,
  101. ?string $backgroundColor = null
  102. ): string
  103. {
  104. $coloredString = '';
  105. if (isset(static::$foregroundColors[$foregroundColor])) {
  106. $coloredString .= "\033[" . static::$foregroundColors[$foregroundColor] . 'm';
  107. }
  108. if (isset(static::$backgroundColors[$backgroundColor])) {
  109. $coloredString .= "\033[" . static::$backgroundColors[$backgroundColor] . 'm';
  110. }
  111. $coloredString .= $string . "\033[0m";
  112. return $coloredString;
  113. }
  114. /**
  115. * 输出提示信息.
  116. *
  117. * @param $msg
  118. */
  119. public static function notice($msg): void
  120. {
  121. fwrite(STDOUT, self::initColoredString($msg, 'light_gray') . PHP_EOL);
  122. }
  123. /**
  124. * 输出错误信息.
  125. *
  126. * @param $msg
  127. */
  128. public static function error($msg): void
  129. {
  130. fwrite(STDERR, self::initColoredString($msg, 'white', 'red') . PHP_EOL);
  131. }
  132. /**
  133. * 输出警告信息.
  134. *
  135. * @param $msg
  136. */
  137. public static function warn($msg): void
  138. {
  139. fwrite(STDOUT, self::initColoredString($msg, 'red', 'yellow') . PHP_EOL);
  140. }
  141. /**
  142. * 输出成功信息.
  143. *
  144. * @param $msg
  145. */
  146. public static function success($msg): void
  147. {
  148. fwrite(STDOUT, self::initColoredString($msg, 'light_cyan') . PHP_EOL);
  149. }
  150. }