|
| 1 | +<?php |
| 2 | +namespace jblond\cli; |
| 3 | + |
| 4 | +/** |
| 5 | + * Class CliColors |
| 6 | + * |
| 7 | + * Works on bash |
| 8 | + * @package jblond\helper |
| 9 | + */ |
| 10 | +class CliColors { |
| 11 | + |
| 12 | + /** |
| 13 | + * @var array |
| 14 | + */ |
| 15 | + private $foregroundColors = array( |
| 16 | + 'black' => '0;30', |
| 17 | + 'dark_gray' => '1;30', |
| 18 | + 'blue' => '0,34', |
| 19 | + 'light_blue' => '1;34', |
| 20 | + 'green' => '0;32', |
| 21 | + 'light_green' => '1;32', |
| 22 | + 'cyan' => '0;36', |
| 23 | + 'light_cyan' => '1;36', |
| 24 | + 'red' => '0;31', |
| 25 | + 'light_red' => '1;31', |
| 26 | + 'purple' => '0;35', |
| 27 | + 'light_purple' => '1;35', |
| 28 | + 'brown' => '0;33', |
| 29 | + 'yellow' => '1;33', |
| 30 | + 'light_gray' => '0;37', |
| 31 | + 'white' => '1;37' |
| 32 | + ); |
| 33 | + |
| 34 | + /** |
| 35 | + * @var array |
| 36 | + */ |
| 37 | + private $backgroundColors = array( |
| 38 | + 'black' => '40', |
| 39 | + 'red' => '41', |
| 40 | + 'green' => '42', |
| 41 | + 'yellow' => '43', |
| 42 | + 'blue' => '44', |
| 43 | + 'magenta' => '45', |
| 44 | + 'cyan' => '46', |
| 45 | + 'light_gray' => '47' |
| 46 | + ); |
| 47 | + |
| 48 | + /** |
| 49 | + * @param string $string |
| 50 | + * @param null|string $foregroundColor |
| 51 | + * @param null|string $backgroundColor |
| 52 | + * @return string |
| 53 | + */ |
| 54 | + public function getColoredString($string, $foregroundColor = null, $backgroundColor = null){ |
| 55 | + $coloredString = ''; |
| 56 | + |
| 57 | + if(isset($this->foregroundColors["$foregroundColor"])){ |
| 58 | + $coloredString .= "\033[" . $this->foregroundColors[$foregroundColor] . "m"; |
| 59 | + } |
| 60 | + |
| 61 | + if(isset($this->backgroundColors["$backgroundColor"])){ |
| 62 | + $coloredString .= "\033[" . $this->backgroundColors[$backgroundColor] . "m"; |
| 63 | + } |
| 64 | + |
| 65 | + $coloredString .= $string . "\033[0m"; |
| 66 | + return $coloredString; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns all foreground color names |
| 71 | + * |
| 72 | + * @return array |
| 73 | + */ |
| 74 | + public function getForegroundColors() { |
| 75 | + return array_keys($this->foregroundColors); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns all background color names |
| 80 | + * |
| 81 | + * @return array |
| 82 | + */ |
| 83 | + public function getBackgroundColors() { |
| 84 | + return array_keys($this->backgroundColors); |
| 85 | + } |
| 86 | +} |
0 commit comments