1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Highlighter |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2016, Some right reserved. |
6
|
|
|
* |
7
|
|
|
* @author Kacper "Kadet" Donat <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Contact with author: |
10
|
|
|
* Xmpp: [email protected] |
11
|
|
|
* E-mail: [email protected] |
12
|
|
|
* |
13
|
|
|
* From Kadet with love. |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace Kadet\Highlighter\Utils; |
17
|
|
|
|
18
|
|
|
class ConsoleHelper |
19
|
|
|
{ |
20
|
|
|
private $_stack = []; |
21
|
|
|
private $_current = []; |
22
|
|
|
|
23
|
|
|
private $_default = [ |
24
|
|
|
'color' => 'default', |
25
|
|
|
'background' => 'default', |
26
|
|
|
'bold' => false, |
27
|
|
|
'underlined' => false, |
28
|
|
|
'dim' => false, |
29
|
|
|
'blink' => false, |
30
|
|
|
'reset' => false, |
31
|
|
|
]; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* ConsoleHelper constructor. |
35
|
|
|
*/ |
36
|
13 |
|
public function __construct() { |
37
|
13 |
|
$this->_current = $this->_default; |
38
|
13 |
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
1 |
|
public function styled($style, $text) |
42
|
|
|
{ |
43
|
1 |
|
return $this->open($style).$text.$this->close(); |
44
|
|
|
} |
45
|
|
|
|
46
|
12 |
|
public function open($style) |
47
|
|
|
{ |
48
|
12 |
|
$this->_stack[] = $this->_current; |
49
|
12 |
|
$style = array_diff_assoc($style, $this->_current); |
50
|
|
|
|
51
|
12 |
|
$this->_current = array_merge($this->_current, $style); |
52
|
|
|
|
53
|
12 |
|
return $this->_set($style); |
54
|
|
|
} |
55
|
|
|
|
56
|
3 |
|
public function close() |
57
|
|
|
{ |
58
|
3 |
|
$this->_current = empty($this->_stack) ? $this->_default : array_pop($this->_stack); |
59
|
|
|
|
60
|
3 |
|
return "\033[0m".$this->_set(array_diff_assoc($this->_current, $this->_default)); |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
private function _color($name, $bg = false) |
64
|
|
|
{ |
65
|
|
|
$colors = [ |
66
|
6 |
|
'default' => 39, |
67
|
6 |
|
'black' => 30, |
68
|
6 |
|
'red' => 31, |
69
|
6 |
|
'green' => 32, |
70
|
6 |
|
'yellow' => 33, |
71
|
6 |
|
'blue' => 34, |
72
|
6 |
|
'magenta' => 35, |
73
|
6 |
|
'cyan' => 36, |
74
|
6 |
|
'light gray' => 37, |
75
|
6 |
|
'dark gray' => 90, |
76
|
6 |
|
'light red' => 91, |
77
|
6 |
|
'light green' => 92, |
78
|
6 |
|
'light yellow' => 93, |
79
|
6 |
|
'light blue' => 94, |
80
|
6 |
|
'light magenta' => 95, |
81
|
6 |
|
'light cyan' => 96, |
82
|
6 |
|
'white' => 97, |
83
|
6 |
|
]; |
84
|
|
|
|
85
|
6 |
|
return $colors[strtolower($name)] + ($bg ? 10 : 0); |
86
|
|
|
} |
87
|
|
|
|
88
|
12 |
|
private function _style($name, $value) |
89
|
|
|
{ |
90
|
|
|
switch ($name) { |
91
|
12 |
|
case 'color': |
92
|
4 |
|
return $this->_color($value); |
93
|
9 |
|
case 'background': |
94
|
3 |
|
return $this->_color($value, true); |
95
|
8 |
|
case 'bold': |
96
|
3 |
|
return $value ? 1 : 21; |
97
|
5 |
|
case 'dim': |
98
|
1 |
|
return $value ? 2 : 22; |
99
|
4 |
|
case 'underline': |
100
|
1 |
|
return $value ? 4 : 24; |
101
|
3 |
|
case 'blink': |
102
|
1 |
|
return $value ? 5 : 25; |
103
|
2 |
|
case 'invert': |
104
|
1 |
|
return $value ? 7 : 27; |
105
|
|
|
} |
106
|
|
|
|
107
|
1 |
|
return null; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
private function _set($style) |
111
|
|
|
{ |
112
|
12 |
|
$escape = "\e[".implode(';', array_map(function ($style, $name) { |
113
|
12 |
|
return $this->_style($style, $name); |
114
|
12 |
|
}, array_keys($style), $style)).'m'; |
115
|
|
|
|
116
|
12 |
|
return $escape === "\e[m" ? null : $escape; |
117
|
|
|
} |
118
|
|
|
|
119
|
2 |
|
public function reset() |
120
|
|
|
{ |
121
|
2 |
|
return "\e[0m"; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|