forked from Siwayll/deuton
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDisplay.php
More file actions
211 lines (191 loc) · 4.72 KB
/
Copy pathDisplay.php
File metadata and controls
211 lines (191 loc) · 4.72 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<?php
/**
* Affichage standardisé avec gestion des couleurs
*
* @package Deuton
* @subpackage Core
* @author Siwaÿll <sanath.labs@gmail.com>
* @license beerware http://wikipedia.org/wiki/Beerware
*/
namespace Siwayll\Deuton;
/**
* Affichage standardisé avec gestion des couleurs
*
* @package Deuton
* @subpackage Core
* @author Siwaÿll <sanath.labs@gmail.com>
* @license beerware http://wikipedia.org/wiki/Beerware
*/
class Display
{
/**
* Raccourcis d'écriture
*
* @var array
*/
protected static $short = [
'c' => 'color',
'b' => 'background',
's' => 'style',
't' => 'template',
];
/**
* Styles pré-formatés
*
* @var array
*/
protected static $templates = [
'focus' => [
'color' => 'blue',
'style' => 'underscore'
],
'alert' => [
'color' => 'white',
'background' => 'red'
],
'warning' => [
'color' => 'red'
],
'success' => [
'color' => 'green'
],
'greatSuccess' => [
'color' => 'white',
'background' => 'green'
],
];
/**
* Couleurs
*
* @var array
*/
static protected $colors = array(
'color' => array(
'black' => 30,
'red' => 31,
'green' => 32,
'yellow' => 33,
'blue' => 34,
'magenta' => 35,
'cyan' => 36,
'white' => 37,
),
'style' => array(
'bright' => 1,
'dim' => 2,
'underscore' => 4,
'blink' => 5,
'reverse' => 7,
'hidden' => 8,
),
'background' => array(
'black' => 40,
'red' => 41,
'green' => 42,
'yellow' => 43,
'blue' => 44,
'magenta' => 45,
'cyan' => 46,
'white' => 47,
)
);
/**
* Affiche un élément après s'être occupé des balises couleur
*
* @param string $content contenu à afficher
*
* @return void
*/
public static function write($content)
{
echo self::parseColor($content);
}
/**
* Affiche un élément sur une ligne après s'être occupé des balises couleur
*
* @param string $content contenu à afficher
*
* @return void
*/
public static function line($content)
{
echo self::parseColor($content) . "\r\n";
}
/**
* Parse une chaine et met les codes couleurs correspondants
*
* @param string $string chaine à parser
*
* @return string
*/
public static function parseColor($string)
{
$pattern = '#{.([a-z0-9 _:]+)}#i';
preg_match_all($pattern, $string, $matchs);
if (!isset($matchs[1])) {
return $string;
}
$max = count($matchs[1]);
for ($i = 0; $i < $max; $i++) {
$color = [];
$foo = explode(' ', $matchs[1][$i]);
foreach ($foo as $order) {
self::parseOrder($color, $order);
}
$string = str_replace($matchs[0][$i], self::color($color), $string);
}
return $string;
}
/**
* Interprête un ordre de formatage
*
* @param array $color Ordre interprété
* @param string $order Ordre de formatage
*
* @return void
*/
protected static function parseOrder(&$color, $order)
{
if ($order === 'reset') {
$color['color'] = 'reset';
return;
}
$bar = explode(':', $order);
if (isset(self::$short[$bar[0]])) {
$bar[0] = self::$short[$bar[0]];
}
if ($bar[0] == 'template') {
$color = self::$templates[$bar[1]];
return;
}
$color[$bar[0]] = $bar[1];
}
/**
* Création du code couleur
*
* @param array $color Paramétrage couleur
*
* @return string
*/
protected static function color($color)
{
$color += array('color' => null, 'style' => null, 'background' => null);
if ($color['color'] == 'reset') {
return "\033[0m";
}
$colors = array();
foreach (array('color', 'style', 'background') as $type) {
if (!isset($color[$type])) {
continue;
}
$code = $color[$type];
if (isset(self::$colors[$type][$code])) {
$colors[] = self::$colors[$type][$code];
}
}
if (empty($colors)) {
$colors[] = 0;
}
return "\033[" . join(';', $colors) . 'm';
}
}