Completed
Pull Request — master (#22)
by De Cramer
02:13
created

Console::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 2
b 0
f 0
ccs 3
cts 3
cp 1
cc 1
eloc 2
nc 1
nop 1
crap 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 = "\e[0m";
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 7
    public function __construct($colorEnabled)
59
    {
60 7
        $this->colorEnabled = $colorEnabled;
61 7
    }
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 . self::normal, $newline, ConsoleOutputInterface::OUTPUT_RAW);
0 ignored issues
show
Unused Code Comprehensibility introduced by
55% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
158
        $nl = "";
159
        if ($newline) $nl = "\n";
160
161
        echo $msg . self::normal . $nl;
162
    }
163
164
    /**
165
     * Strip styles from a string.
166
     *
167
     * @param $string
168
     *
169
     * @return mixed
170
     */
171
    protected function stripStyles($string)
172
    {
173
        $string = preg_replace('/(?<!\$)((?:\$\$)*)\$[^$0-9a-hlp]/iu', '$1', $string);
174
        return preg_replace('/(?<!\$)((?:\$\$)*)\$(?:g|[0-9a-f][^\$]{0,2})/iu', '$1', $string);
175
    }
176
177
    /**
178
     * Fix the color codes from MP standard to world standard
179
     *
180
     * @param string $r
181
     * @param string $g
182
     * @param string $b
183
     *
184
     * @return string
185
     */
186
    public function fixColors($r, $g, $b)
187
    {
188
        $out = "111";
189
        // black/gray/white
190
        if ($r == $g && $g == $b && $b == $r) {
191
            if ($r >= 0 && $r < 5) {
192
                $out = "000";
193
            }
194
            if ($r >= 5 && $r < 13) {
195
                $out = "111";
196
            }
197
            if ($r >= 13 && $r <= 16) {
198
                $out = "222";
199
            }
200
        } else {
201
            $out = $this->convert($r) . $this->convert($g) . $this->convert($b);
202
        }
203
        return $out;
204
    }
205
206
    /**
207
     * Convert.
208
     *
209
     * @param int $number
210
     *
211
     * @return string
212
     */
213
    public function convert($number)
214
    {
215
        $out = "0";
216
217
        if ($number >= 9 && $number <= 16) {
218
            $out = "2";
219
        }
220
        if ($number >= 3 && $number < 9) {
221
            $out = "1";
222
        }
223
        if ($number >= 0 && $number < 3) {
224
            $out = "0";
225
        }
226
        return $out;
227
    }
228
}
229