Cli::colorForShell()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 27
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 16
nc 3
nop 2
dl 0
loc 27
rs 9.7333
c 0
b 0
f 0
1
<?php
2
3
namespace BfwCli\Helpers;
4
5
use \Exception;
6
7
/**
8
 * Helpers for cli applications
9
 */
10
class Cli
11
{
12
    /**
13
     * @const ERR_COLOR_NOT_AVAILABLE Exception code if the color is not
14
     * available.
15
     */
16
    const ERR_COLOR_NOT_AVAILABLE = 1601001;
17
    
18
    /**
19
     * @const ERR_STYLE_NOT_AVAILABLE Exception code if the style is not
20
     * available.
21
     */
22
    const ERR_STYLE_NOT_AVAILABLE = 1601002;
23
    
24
    /**
25
     * @const FLUSH_AUTO Value to use on $flushMethod property to automaticaly
26
     * call the method ob_flush into function displayMsg and displayMsgNoNL
27
     */
28
    const FLUSH_AUTO = 'auto';
29
    
30
    /**
31
     * @const FLUSH_MANUAL Value to use on $flushMethod property to NOT
32
     * automaticaly call the method ob_flush into function displayMsg
33
     * and displayMsgNL
34
     */
35
    const FLUSH_MANUAL = 'manual';
36
    
37
    /**
38
     * @var string $callObFlush (default: self::FLUSH_AUTO) Define if the
39
     * method ob_flush is called or not into the method displayMsg
40
     * and displayMsgNL
41
     */
42
    public static $callObFlush = self::FLUSH_AUTO;
43
    
44
    /**
45
     * Display a message in the console without a line break
46
     * If only the first parameter is passed, the colors will be those
47
     * currently used in the console
48
     * 
49
     * @param string $msg Message to display
50
     * @param string $colorTxt (default "white") Text color
51
     * @param string $style (default "normal") Style for the message (bold etc)
52
     * @param string $colorBg (default "black") Background color
53
     * 
54
     * @return void
55
     */
56
    public static function displayMsg(
57
        string $msg,
58
        string $colorTxt = 'white',
59
        string $style = 'normal',
60
        string $colorBg = 'black'
61
    ) {
62
        $nbArgs = func_num_args();
63
        
64
        if ($nbArgs === 1) {
65
            echo $msg;
66
            
67
            if (self::$callObFlush === self::FLUSH_AUTO) {
68
                ob_flush();
69
            }
70
            
71
            return;
72
        }
73
        
74
        //Get colors values
75
        $currentClass = get_called_class();
76
        $styleNum     = $currentClass::styleForShell($style);
77
        $colorTxtNum  = $currentClass::colorForShell($colorTxt, 'txt');
78
        $colorBgNum   = $currentClass::colorForShell($colorBg, 'bg');
79
80
        if ($nbArgs > 3) {
81
            echo "\033[".$styleNum.";".$colorTxtNum.";".$colorBgNum."m";
82
        } else {
83
            echo "\033[".$styleNum.";".$colorTxtNum."m";
84
        }
85
        
86
        echo $msg."\033[0m";
87
        
88
        if (self::$callObFlush === self::FLUSH_AUTO) {
89
            ob_flush();
90
        }
91
    }
92
    
93
    /**
94
     * Display a message in the console with a line break
95
     * If only the first parameter is passed, the colors will be those
96
     * currently used in the console
97
     * 
98
     * @param string $msg Message to display
99
     * @param string $colorTxt (default "white") Text color
100
     * @param string $style (default "normal") Style for the message (bold etc)
101
     * @param string $colorBg (default "black") Background color
102
     * 
103
     * @return void
104
     */
105
    public static function displayMsgNL(
106
        string $msg,
107
        string $colorTxt = 'white',
108
        string $style = 'normal',
109
        string $colorBg = 'black'
110
    ) {
111
        $nbArgs       = func_num_args();
112
        $currentClass = get_called_class();
113
        
114
        //Because on displayMsg, we can just echo, or echo with color change.
115
        //Not forget custom user shell color :)
116
        if ($nbArgs === 1) {
117
            $currentClass::displayMsg($msg."\n");
118
        } elseif ($nbArgs > 3) {
119
            $currentClass::displayMsg($msg."\n", $colorTxt, $style, $colorBg);
120
        } else {
121
            $currentClass::displayMsg($msg."\n", $colorTxt, $style);
122
        }
123
    }
124
125
    /**
126
     * Convert text color to shell value
127
     * 
128
     * @param string $color The human color text
129
     * @param string $type ("txt"|"bg") If the color is for text or background
130
     * 
131
     * @return integer
132
     * 
133
     * @throws \Exception If the color is not available
134
     */
135
    protected static function colorForShell(string $color, string $type): int
136
    {
137
        $colorList = [
138
            'black'   => 0,
139
            'red'     => 1,
140
            'green'   => 2,
141
            'yellow'  => 3,
142
            'blue'    => 4,
143
            'magenta' => 5,
144
            'cyan'    => 6,
145
            'white'   => 7
146
        ];
147
148
        if (!isset($colorList[$color])) {
149
            throw new Exception(
150
                'Color '.$color.' is not available.',
151
                self::ERR_COLOR_NOT_AVAILABLE
152
            );
153
        }
154
155
        //Text color
156
        if ($type === 'txt') {
157
            return $colorList[$color] + 30;
158
        }
159
        
160
        //Background color
161
        return $colorList[$color] + 40;
162
    }
163
164
    /**
165
     * Convert a human style text to shell value
166
     * 
167
     * @param string $style The style value
168
     * 
169
     * @return integer
170
     * 
171
     * @throws \Exception If the style is not available
172
     */
173
    protected static function styleForShell(string $style): int
174
    {
175
        $styleList = [
176
            'normal'        => 0,
177
            'bold'          => 1,
178
            'not-bold'      => 21,
179
            'underline'     => 4,
180
            'not-underline' => 24,
181
            'blink'         => 5,
182
            'not-blink'     => 25,
183
            'reverse'       => 7,
184
            'not-reverse'   => 27
185
        ];
186
187
        if (!isset($styleList[$style])) {
188
            throw new Exception(
189
                'Style '.$style.' is not available.',
190
                self::ERR_STYLE_NOT_AVAILABLE
191
            );
192
        }
193
194
        return $styleList[$style];
195
    }
196
}
197