Completed
Push — master ( f3bed7...d2e601 )
by De Cramer
9s
created

Console::writeln()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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