Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
| tao_helpers_Cli | |
0.00% |
0 / 8 |
|
0.00% |
0 / 2 |
42 | |
0.00% |
0 / 1 |
| getBgColor | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| getFgColor | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | /* |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License |
| 6 | * as published by the Free Software Foundation; under version 2 |
| 7 | * of the License (non-upgradable). |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 17 | * |
| 18 | * Copyright (c) 2008-2010 (original work) Deutsche Institut für Internationale Pädagogische Forschung |
| 19 | * (under the project TAO-TRANSFER); |
| 20 | * 2009-2012 (update and modification) Public Research Centre Henri Tudor |
| 21 | * (under the project TAO-SUSTAIN & TAO-DEV); |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | |
| 26 | /** |
| 27 | * Utility class focusing on the PHP CLI. |
| 28 | * |
| 29 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 30 | * @package tao |
| 31 | |
| 32 | */ |
| 33 | class tao_helpers_Cli |
| 34 | { |
| 35 | /** |
| 36 | * A set of color codes that can be used to highlight texts in a CLI context. |
| 37 | * Keys of these associative array are color names in english and values are color |
| 38 | * codes. Available colors are: |
| 39 | * |
| 40 | * $colors['background']['black'] |
| 41 | * $colors['background']['red'] |
| 42 | * $colors['background']['green'] |
| 43 | * $colors['background']['yellow'] |
| 44 | * $colors['background']['blue'] |
| 45 | * $colors['background']['magenta'] |
| 46 | * $colors['background']['cyan'] |
| 47 | * $colors['background']['light_gray'] |
| 48 | * $colors['foreground']['black'] |
| 49 | * $colors['foreground']['dark_gray'] |
| 50 | * $colors['foreground']['blue'] |
| 51 | * $colors['foreground']['light_blue'] |
| 52 | * $colors['foreground']['green'] |
| 53 | * $colors['foreground']['light_green'] |
| 54 | * $colors['foreground']['light_cyan'] |
| 55 | * $colors['foreground']['red'] |
| 56 | * $colors['foreground']['light_red'] |
| 57 | * $colors['foreground']['purple'] |
| 58 | * $colors['foreground']['brown'] |
| 59 | * $colors['foreground']['yellow'] |
| 60 | * $colors['foreground']['light_gray'] |
| 61 | * $colors['foreground']['white'] |
| 62 | * |
| 63 | * @var array |
| 64 | */ |
| 65 | private static $colors = [ |
| 66 | 'background' => [ |
| 67 | 'black' => '40', |
| 68 | 'red' => '41', |
| 69 | 'green' => '42', |
| 70 | 'yellow' => '43', |
| 71 | 'blue' => '44', |
| 72 | 'magenta' => '45', |
| 73 | 'cyan' => '46', |
| 74 | 'light_gray' => '47' |
| 75 | ], |
| 76 | 'foreground' => [ |
| 77 | 'black' => '0;30', |
| 78 | 'dark_gray' => '1;30', |
| 79 | 'blue' => '0;34', |
| 80 | 'light_blue' => '1;34', |
| 81 | 'green' => '0;32', |
| 82 | 'light_green' => '1;32', |
| 83 | 'cyan' => '0;36', |
| 84 | 'light_cyan' => '1;36', |
| 85 | 'red' => '0;31', |
| 86 | 'light_red' => '1;31', |
| 87 | 'purple' => '0;35', |
| 88 | 'light_purple' => '1;35', |
| 89 | 'brown' => '0;33', |
| 90 | 'yellow' => '1;33', |
| 91 | 'light_gray' => '0;37', |
| 92 | 'white' => '1;37' |
| 93 | ]]; |
| 94 | |
| 95 | /** |
| 96 | * Get a background color compliant with the CLI. Available color names are: |
| 97 | * black, red, green, yellow, blue, magenta, cyan, light_gray. |
| 98 | * |
| 99 | * If the color name does not exist, an empty string is returned. |
| 100 | * |
| 101 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 102 | * @param string name The name of the color. |
| 103 | * @return string The corresponding color code. |
| 104 | */ |
| 105 | public static function getBgColor($name) |
| 106 | { |
| 107 | $returnValue = (string) ''; |
| 108 | |
| 109 | if (!empty($name) && array_key_exists($name, self::$colors['background'])) { |
| 110 | $returnValue = self::$colors['background'][$name]; |
| 111 | } |
| 112 | |
| 113 | return (string) $returnValue; |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Get a foreground color compliant with the CLI. Available color names are: |
| 118 | * black, dark_gray, blue, light_blue, green, light_green, light_cyan, |
| 119 | * red, light_red, purple, brown, yellow, light_gray, white. |
| 120 | * |
| 121 | * If the provided color names is not supported, an empty string is returned. Otherwise, |
| 122 | * the corresponding color code is returned. |
| 123 | * |
| 124 | * @author Bertrand Chevrier, <bertrand.chevrier@tudor.lu> |
| 125 | * @param string name The color name. |
| 126 | * @return string A color code. |
| 127 | */ |
| 128 | public static function getFgColor($name) |
| 129 | { |
| 130 | $returnValue = (string) ''; |
| 131 | |
| 132 | if (!empty($name) && array_key_exists($name, self::$colors['foreground'])) { |
| 133 | $returnValue = self::$colors['foreground'][$name]; |
| 134 | } |
| 135 | |
| 136 | return (string) $returnValue; |
| 137 | } |
| 138 | } |