Completed
Push — master ( 7f33bf...9b09f6 )
by Adam
04:00
created

Cli::getColoredString()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 6.105

Importance

Changes 4
Bugs 1 Features 1
Metric Value
c 4
b 1
f 1
dl 0
loc 19
ccs 12
cts 14
cp 0.8571
rs 8.8571
cc 6
eloc 12
nc 6
nop 3
crap 6.105
1
<?php
2
3
4
namespace Genesis;
5
6
7
/**
8
 * @author Adam Bisek <[email protected]>
9
 *
10
 * Cli
11
 */
12
class Cli
13
{
14
15
	public static $enableColors = TRUE;
16
17
	private static $foregroundColors = array(
18
		'black' => '0;30', 'dark_gray' => '1;30', 'blue' => '0;34', 'light_blue' => '1;34', 'green' => '0;32', 'light_green' => '1;32', 'cyan' => '0;36', 'light_cyan' => '1;36', 'red' => '0;31', 'light_red' => '1;31', 'purple' => '0;35', 'light_purple' => '1;35', 'brown' => '0;33', 'yellow' => '1;33', 'light_gray' => '0;37', 'white' => '1;37',
19
	);
20
21
	private static $backgroundColors = array(
22
		'black' => '40', 'red' => '41', 'green' => '42', 'yellow' => '43', 'blue' => '44', 'magenta' => '45', 'cyan' => '46', 'light_gray' => '47',
23
	);
24
25
26 28
	public static function getColoredString($string, $foreground = NULL, $background = NULL)
27
	{
28 28
		if (!self::$enableColors) {
29 14
			return $string;
30
		}
31 14
		if ($foreground === NULL && $background === NULL) {
32 6
			return $string;
33
		}
34
35 12
		$colored = "";
36 12
		if (isset(self::$foregroundColors[$foreground])) {
37 12
			$colored .= "\033[" . self::$foregroundColors[$foreground] . "m";
38 12
		}
39 12
		if (isset(self::$backgroundColors[$background])) {
40
			$colored .= "\033[" . self::$backgroundColors[$background] . "m";
41
		}
42 12
		$colored .= $string . "\033[0m";
43 12
		return $colored;
44
	}
45
46
}