Completed
Push — master ( ff8f47...dbda64 )
by De Cramer
02:41 queued 39s
created

Console::stripStyles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace eXpansion\Core\Services;
4
5
use Symfony\Component\Console\Output\ConsoleOutputInterface;
6
7
/**
8
 * Class Console to print in the console.
9
 *
10
 * @package eXpansion\Core\Services
11
 * @author Reaby
12
 */
13
class Console
14
{
15
16
    const black = "\e[0;30m";
17
    const b_black = "\e[30;1m";
18
19
    const red = "\e[0;31m";
20
    const b_red = "\e[31;1m";
21
22
    const green = "\e[0;32m";
23
    const b_green = "\e[32;1m";
24
25
    const yellow = "\e[0;33m";
26
    const b_yellow = "\e[33;1m";
27
28
    const blue = "\e[0;34m";
29
    const b_blue = "\e[34;1m";
30
31
    const magenta = "\e[0;35m";
32
    const b_magenta = "\e[35;1m";
33
34
    const cyan = "\e[0;36m";
35
    const b_cyan = "\e[36;1m";
36
37
    const white = "\e[0;37m";
38
    const b_white = "\e[37;1m";
39
40
    // define aliases for colors
41
    const error = "\e[37;1m\e[41m";
42
    const success = self::b_green;
43
    const normal = self::white;
44
    const bold = self::b_white;
45
46
47
    /** @var ConsoleOutputInterface */
48
    protected $consoleOutput;
49
50
    /** @var boolean Color console enabled */
51
    protected $colorEnabled;
52
53
    /**
54
     * Console constructor.
55
     *
56
     * @param bool $colorEnabled
57
     */
58
    public function __construct($colorEnabled)
59
    {
60
        $this->colorEnabled = $colorEnabled;
61
    }
62
63
64
    /**
65
     * Initialize service with the console output.
66
     *
67
     * @param ConsoleOutputInterface $consoleOutput
68
     */
69
    public function init(ConsoleOutputInterface $consoleOutput)
70
    {
71
        $this->consoleOutput = $consoleOutput;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function writeln($string)
78
    {
79
        $this->write($string, true);
80
    }
81
82
    /**
83
     * @inheritdoc
84
     */
85
    public function write($string, $newline = false)
86
    {
87
        if ($this->colorEnabled && $this->consoleOutput->isDecorated()) {
88
            $array = array("000" => self::b_black,
89
                "100" => self::red,
90
                "010" => self::green,
91
                "110" => self::yellow,
92
                "001" => self::blue,
93
                "011" => self::magenta,
94
                "101" => self::cyan,
95
                "111" => self::white,
96
                "200" => self::b_red,
97
                "211" => self::red,
98
                "121" => self::green,
99
                "020" => self::b_green,
100
                "021" => self::green,
101
                "012" => self::cyan,
102
                "221" => self::b_yellow,
103
                "220" => self::b_yellow,
104
                "120" => self::green,
105
                "210" => self::yellow,
106
                "112" => self::b_blue,
107
                "002" => self::b_blue,
108
                "122" => self::b_cyan,
109
                "022" => self::b_cyan,
110
                "202" => self::b_magenta,
111
                "212" => self::b_magenta,
112
                "102" => self::magenta,
113
                "201" => self::b_red,
114
                "222" => self::b_white,
115
            );
116
            $matches = array();
117
            preg_match_all("/\\$[A-Fa-f0-9]{3}/", $string, $matches);
118
            $split = preg_split("/\\$[A-Fa-f0-9]{3}/", $string);
119
120
            $out = "";
121
            foreach ($matches[0] as $i => $rgb) {
122
                $code = $this->fixColors(hexdec($rgb[1]), hexdec($rgb[2]), hexdec($rgb[3]));
123
                if (array_key_exists($code, $array)) {
124
                    $out .= $array[$code] . $this->stripStyles($split[$i + 1]);
125
                } else {
126
                    $out .= self::white . $this->stripStyles($split[$i + 1]);
127
                }
128
                $end = $this->stripStyles($split[$i + 1]);
129
            }
130
131
132
            if (!empty($end)) {
133
                if ($end == $this->stripStyles(end($split))) {
134
                    $end = "";
135
                }
136
            } else {
137
                $end = "";
138
            }
139
140
            $out = self::white . $this->stripStyles(reset($split)) . $out . $end;
141
        } else {
142
            $out = $this->stripStyles($string);
143
        }
144
145
        $this->ansiOut($out, $newline);
146
    }
147
148
    /**
149
     * Outoyt brute text.
150
     *
151
     * @param string $msg
152
     * @param boolean $newline
153
     *
154
     */
155
    protected function ansiOut($msg, $newline)
156
    {
157
        $this->consoleOutput->write($msg, $newline, ConsoleOutputInterface::OUTPUT_RAW);
158
    }
159
160
    /**
161
     * Strip styles from a string.
162
     *
163
     * @param $string
164
     *
165
     * @return mixed
166
     */
167
    protected function stripStyles($string)
168
    {
169
        $string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string);
170
        return preg_replace('/(?<!\$)((?:\$\$)*)\$(?:g|[0-9a-f][^\$]{0,2})/iu', '$1', $string);
171
    }
172
173
    /**
174
     * Fix the color codes from MP standard to world standard
175
     *
176
     * @param string $r
177
     * @param string $g
178
     * @param string $b
179
     *
180
     * @return string
181
     */
182
    public function fixColors($r, $g, $b)
183
    {
184
        $out = "111";
185
        // black/gray/white
186
        if ($r == $g && $g == $b && $b == $r) {
187
            if ($r >= 0 && $r < 5) {
188
                $out = "000";
189
            }
190
            if ($r >= 5 && $r < 13) {
191
                $out = "111";
192
            }
193
            if ($r >= 13 && $r <= 16) {
194
                $out = "222";
195
            }
196
        } else {
197
            $out = $this->convert($r) . $this->convert($g) . $this->convert($b);
198
        }
199
        return $out;
200
    }
201
202
    /**
203
     * Convert.
204
     *
205
     * @param int $number
206
     *
207
     * @return string
208
     */
209
    public function convert($number)
210
    {
211
        $out = "0";
212
213
        if ($number >= 9 && $number <= 16) {
214
            $out = "2";
215
        }
216
        if ($number >= 3 && $number < 9) {
217
            $out = "1";
218
        }
219
        if ($number >= 0 && $number < 3) {
220
            $out = "0";
221
        }
222
        return $out;
223
    }
224
}
225