Passed
Push — master ( d2ad69...686350 )
by Kacper
02:44
created

ConsoleHelper::styled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 2
crap 1
1
<?php
2
/**
3
 * Highlighter
4
 *
5
 * Copyright (C) 2015, Some right reserved.
6
 *
7
 * @author Kacper "Kadet" Donat <[email protected]>
8
 *
9
 * Contact with author:
10
 * Xmpp: [email protected]
11
 * E-mail: [email protected]
12
 *
13
 * From Kadet with love.
14
 */
15
16
namespace Kadet\Highlighter\Utils;
17
18
19
class ConsoleHelper
20
{
21
    private $_stack   = [];
22
    private $_current = [];
23
24
    private $_default = [
25
        'color'      => 'default',
26
        'background' => 'default',
27
        'bold'       => false,
28
        'underlined' => false,
29
        'dim'        => false,
30
        'blink'      => false,
31
    ];
32
33 1
    public function styled($style, $text) {
34 1
        return $this->open($style).$text.$this->close();
35
    }
36
37 12
    public function open($style)
38
    {
39 12
        if (!empty($this->_current)) {
40 3
            $this->_stack[] = $this->_current;
41 3
            $style = array_diff($style, $this->_current);
42 3
        }
43
44 12
        $this->_current = array_merge($this->_current, $style);
45 12
        return $this->_set(array_diff($this->_current, $this->_default));
46
    }
47
48 3
    public function close()
49
    {
50 3
        $this->_current = empty($this->_stack) ? $this->_default : array_pop($this->_stack);
51 3
        return "\033[0m".$this->_set(array_diff($this->_current, $this->_default));
52
    }
53
54 6
    private function _color($name, $bg = false)
55
    {
56
        $colors = [
57 6
            'default'       => 39,
58 6
            'black'         => 30,
59 6
            'red'           => 31,
60 6
            'green'         => 32,
61 6
            'yellow'        => 33,
62 6
            'blue'          => 34,
63 6
            'magenta'       => 35,
64 6
            'cyan'          => 36,
65 6
            'light gray'    => 37,
66 6
            'dark gray'     => 90,
67 6
            'light red'     => 91,
68 6
            'light green'   => 92,
69 6
            'light yellow'  => 93,
70 6
            'light blue'    => 94,
71 6
            'light magenta' => 95,
72 6
            'light cyan'    => 96,
73 6
            'white'         => 97,
74 6
        ];
75
76 6
        return $colors[strtolower($name)] + ($bg ? 10 : 0);
77
    }
78
79 12
    private function _style($name, $value) {
80
        switch($name) {
81 12
            case 'color':
82 4
                return $this->_color($value);
83 9
            case 'background':
84 3
                return $this->_color($value, true);
85 8
            case 'bold':
86 3
                return $value ? 1 : 21;
87 5
            case 'dim':
88 1
                return $value ? 2 : 22;
89 4
            case 'underline':
90 1
                return $value ? 4 : 24;
91 3
            case 'blink':
92 1
                return $value ? 5 : 25;
93 2
            case 'invert':
94 1
                return $value ? 7 : 27;
95
        }
96
97 1
        return null;
98
    }
99
100
    private function _set($style) {
101 12
        $escape = "\e[".implode(';', array_map(function($style, $name) {
102 12
            return $this->_style($style, $name);
103 12
        }, array_keys($style), $style)).'m';
104
105 12
        return $escape === "\e[m" ? null : $escape;
106
    }
107
108 2
    public function reset() {
109 2
        return "\e[0m";
110
    }
111
}