/
Colorize.php
84 lines (73 loc) · 1.76 KB
/
Colorize.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
* Bluz Framework Component
*
* @copyright Bluz PHP Team
* @link https://github.com/bluzphp/framework
*/
declare(strict_types=1);
namespace Bluz\Cli;
/**
* Colorize text for CLI
*
* @package Bluz\Cli
* @author Anton Shevchuk
*/
class Colorize
{
/**
* @var array list of text colors
*/
protected static $colors = [
'black' => '30',
'red' => '31',
'green' => '32',
'yellow' => '33',
'blue' => '34',
'magenta' => '35',
'cyan' => '36',
'gray' => '37',
];
/**
* @var array list of background colors
*/
protected static $background = [
'black' => '40',
'red' => '41',
'green' => '42',
'yellow' => '43',
'blue' => '44',
'magenta' => '45',
'cyan' => '46',
'gray' => '47'
];
/**
* Return string colorized for Linux console
*
* @param string $text
* @param string $foreground
* @param string $background
* @param bool $bold
* @param bool $underline
* @return string
*/
public static function text($text, $foreground = null, $background = null, $bold = false, $underline = false)
{
$colored = "\033[";
if ($bold) {
$colored .= '1;';
}
if ($underline) {
$colored .= '4;';
}
if ($foreground !== null && isset(self::$colors[$foreground])) {
$colored .= self::$colors[$foreground] . ";";
}
if ($background !== null && isset(self::$background[$background])) {
$colored .= self::$background[$background] . ";";
}
$colored = rtrim($colored, ';');
$colored .= "m" . $text . "\033[0m";
return $colored;
}
}