Completed
Push — master ( f8b2ff...51cde5 )
by Kacper
03:30
created

Console::styled()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 2
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 Console
20
{
21
    private static $_stack   = [];
22
    private static $_current = [];
23
24
    private static $_default = [
25
        'color'      => 'default',
26
        'background' => 'default',
27
        'bold'       => false,
28
        'underlined' => false,
29
        'dim'        => false,
30
        'blink'      => false,
31
    ];
32
33
    public static function styled($style, $text) {
34
        return self::open($style).$text.self::close();
35
    }
36
37
    public static function open($style)
38
    {
39
        if (!empty(self::$_current)) {
40
            self::$_stack[] = self::$_current;
41
            $style = array_diff($style, self::$_current);
42
        }
43
44
        self::$_current = array_merge(self::$_current, $style);
45
        return self::_set(array_diff(self::$_current, self::$_default));
46
    }
47
48
    public static function close()
49
    {
50
        self::$_current = empty(self::$_stack) ? self::$_default : array_pop(self::$_stack);
51
        return "\033[0m".self::_set(array_diff(self::$_current, self::$_default));
52
    }
53
54
    private static function _color($name, $bg = false)
55
    {
56
        $colors = [
57
            'default'       => 39,
58
            'black'         => 30,
59
            'red'           => 31,
60
            'green'         => 32,
61
            'yellow'        => 33,
62
            'blue'          => 34,
63
            'magenta'       => 35,
64
            'cyan'          => 36,
65
            'light gray'    => 37,
66
            'dark gray'     => 90,
67
            'light red'     => 91,
68
            'light green'   => 92,
69
            'light yellow'  => 93,
70
            'light blue'    => 94,
71
            'light magenta' => 95,
72
            'light cyan'    => 96,
73
            'white'         => 97,
74
        ];
75
76
        return $colors[strtolower($name)] + ($bg ? 10 : 0);
77
    }
78
79
    private static function _style($name, $value) {
80
        switch($name) {
81
            case 'color':
82
                return self::_color($value);
83
            case 'background':
84
                return self::_color($value, true);
85
            case 'bold':
86
                return $value ? 1 : 21;
87
            case 'dim':
88
                return $value ? 2 : 22;
89
            case 'underline':
90
                return $value ? 4 : 24;
91
            case 'blink':
92
                return $value ? 5 : 25;
93
            case 'invert':
94
                return $value ? 7 : 27;
95
        }
96
97
        return null;
98
    }
99
100
    private static function _set($style) {
101
        $escape = "\033[".implode(';', array_map(function($style, $name) {
102
                return self::_style($style, $name);
103
            }, array_keys($style), $style)).'m';
104
105
        return $escape;
106
    }
107
108
    public static function reset() {
109
        return "\033[0m";
110
    }
111
}