Completed
Pull Request — master (#95)
by
unknown
02:57
created

Console   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 202
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
wmc 24
lcom 1
cbo 2
dl 0
loc 202
ccs 0
cts 67
cp 0
rs 10
c 3
b 2
f 0

8 Methods

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