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
|
14 |
|
public function __construct() { |
37
|
14 |
|
$this->_current = $this->_default; |
38
|
14 |
|
} |
39
|
|
|
|
40
|
|
|
|
41
|
1 |
|
public function styled($style, $text) |
42
|
|
|
{ |
43
|
1 |
|
return $this->open($style).$text.$this->close(); |
44
|
|
|
} |
45
|
|
|
|
46
|
14 |
|
public function open($style) |
47
|
|
|
{ |
48
|
14 |
|
$this->_stack[] = $this->_current; |
49
|
14 |
|
$style = array_diff_assoc($style, $this->_current); |
50
|
|
|
|
51
|
14 |
|
$this->_current = array_merge($this->_current, $style); |
52
|
|
|
|
53
|
14 |
|
return $this->set($style); |
54
|
|
|
} |
55
|
|
|
|
56
|
4 |
|
public function close() |
57
|
|
|
{ |
58
|
4 |
|
$this->_current = empty($this->_stack) ? $this->_default : array_pop($this->_stack); |
59
|
|
|
|
60
|
4 |
|
return "\033[0m".$this->set(array_diff_assoc($this->_current, $this->_default)); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function current() |
64
|
|
|
{ |
65
|
|
|
return $this->_current; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function set($style) |
69
|
|
|
{ |
70
|
14 |
|
$escape = "\e[".implode(';', array_map(function ($style, $name) { |
71
|
14 |
|
return $this->_style($style, $name); |
72
|
14 |
|
}, array_keys($style), $style)).'m'; |
73
|
|
|
|
74
|
14 |
|
return $escape === "\e[m" ? null : $escape; |
75
|
|
|
} |
76
|
|
|
|
77
|
3 |
|
public function reset() |
78
|
|
|
{ |
79
|
3 |
|
return "\e[0m"; |
80
|
|
|
} |
81
|
|
|
|
82
|
7 |
|
private function _color($name, $bg = false) |
83
|
|
|
{ |
84
|
|
|
$colors = [ |
85
|
7 |
|
'default' => 39, |
86
|
7 |
|
'black' => 30, |
87
|
7 |
|
'red' => 31, |
88
|
7 |
|
'green' => 32, |
89
|
7 |
|
'yellow' => 33, |
90
|
7 |
|
'blue' => 34, |
91
|
7 |
|
'magenta' => 35, |
92
|
7 |
|
'cyan' => 36, |
93
|
7 |
|
'light gray' => 37, |
94
|
7 |
|
'dark gray' => 90, |
95
|
7 |
|
'light red' => 91, |
96
|
7 |
|
'light green' => 92, |
97
|
7 |
|
'light yellow' => 93, |
98
|
7 |
|
'light blue' => 94, |
99
|
7 |
|
'light magenta' => 95, |
100
|
7 |
|
'light cyan' => 96, |
101
|
7 |
|
'white' => 97, |
102
|
7 |
|
]; |
103
|
|
|
|
104
|
7 |
|
return $colors[strtolower($name)] + ($bg ? 10 : 0); |
105
|
|
|
} |
106
|
|
|
|
107
|
14 |
|
private function _style($name, $value) |
108
|
|
|
{ |
109
|
|
|
switch ($name) { |
110
|
14 |
|
case 'color': |
111
|
5 |
|
return $this->_color($value); |
112
|
10 |
|
case 'background': |
113
|
3 |
|
return $this->_color($value, true); |
114
|
9 |
|
case 'bold': |
115
|
3 |
|
return $value ? 1 : 21; |
116
|
6 |
|
case 'dim': |
117
|
1 |
|
return $value ? 2 : 22; |
118
|
5 |
|
case 'italic': |
119
|
1 |
|
return $value ? 3 : 23; |
120
|
4 |
|
case 'underline': |
121
|
1 |
|
return $value ? 4 : 24; |
122
|
3 |
|
case 'blink': |
123
|
1 |
|
return $value ? 5 : 25; |
124
|
2 |
|
case 'invert': |
125
|
1 |
|
return $value ? 7 : 27; |
126
|
|
|
} |
127
|
|
|
|
128
|
1 |
|
return null; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|