Completed
Push — master ( 992cd0...c5931e )
by Viacheslav
09:49 queued 06:27
created

Console::set()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 8.8571
c 0
b 0
f 0
cc 5
eloc 10
nc 5
nop 1
1
<?php
2
3
namespace Yaoi\Cli;
4
5
use Yaoi\BaseClass;
6
use Yaoi\String\Expression;
7
8
class Console extends BaseClass
9
{
10
    const RESET = 0;        // reset all attributes to their defaults
11
    const BOLD = 1;         // set bold
12
    //const HALF_BRIGHT = 2;  // set half-bright (simulated with color on a color display)
13
    //const UNDERSCORE = 4;   // set underscore (simulated with color on a color display) (the colors used to simulate dim or underline are set using ESC ] ...)
14
    //const BLINK = 5;        // set blink
15
    //const REVERSE = 7;      // set reverse video
16
    //const PRIMARY_FONT = 10; //      reset selected mapping, display control flag, and toggle meta flag (ECMA-48 says "primary font").
17
    //const ALT_FONT = 11;      // select null mapping, set display control flag, reset  toggle  meta flag (ECMA-48 says "first alternate font").
18
    //const SECOND_ALT_FONT = 12;      // select  null  mapping,  set  display control flag, set toggle meta flag (ECMA-48 says "second alternate font").  The toggle meta flag causes the high bit of a byte to be toggled before the mapping table translation is done.
19
    //const DOUBLY_UNDERLINED = 21;     // set normal intensity (ECMA-48 says "doubly underlined")
20
    //const NORMAL_INTENSITY = 22;      // set normal intensity
21
    //const UNDERLINE_OFF = 24;     // underline off
22
    //const BLINK_OFF = 25;      // blink off
23
    //const REVERSE_OFF = 27;     // reverse video off
24
    const FG_BLACK = 30;      // set black foreground
25
    const FG_RED = 31;      // set red foreground
26
    const FG_GREEN = 32;     // set green foreground
27
    const FG_BROWN = 33;      // set brown foreground
28
    const FG_BLUE = 34;      // set blue foreground
29
    const FG_MAGENTA = 35;    // set magenta foreground
30
    const FG_CYAN = 36;      // set cyan foreground
31
    const FG_WHITE = 37;      // set white foreground
32
    //const UNDERSCORE_ON = 38;      // set underscore on, set default foreground color
33
    //const UNDERSCORE_OFF = 39;      // set underscore off, set default foreground color
34
    const BG_BLACK = 40;      // set black background
35
    const BG_RED = 41;      // set red background
36
    const BG_GREEN = 42;      // set green background
37
    const BG_BROWN = 43;      // set brown background
38
    const BG_BLUE = 44;      // set blue background
39
    const BG_MAGENTA = 45;      // set magenta background
40
    const BG_CYAN = 46;      // set cyan background
41
    const BG_WHITE = 47;      // set white background
42
    const BG_DEFAULT = 49;     // set default background color
43
44
    private $mode = array(self::RESET);
45
46
    public function set($mode = self::RESET) {
47
        if (!($this->forceColors || $this->attached())) {
48
            return $this;
49
        }
50
51
        if (!is_array($mode)) {
52
            $mode = func_get_args();
53
        }
54
        if ($this->mode === $mode) {
55
            return $this;
56
        }
57
        $this->mode = $mode;
58
        echo "\x1B[" . implode(';', $mode) . 'm';
59
        return $this;
60
    }
61
62
    public function returnCaret() {
63
        echo "\r";
64
        return $this;
65
    }
66
67
    public function eol() {
68
        echo PHP_EOL;
69
        $this->lineStarted = false;
70
        return $this;
71
    }
72
73
    /**
74
     * @param string $statement
75
     * @param mixed ...$binds
76
     * @throws \Yaoi\String\Exception
77
     * @return static
78
     */
79
    public function printF($statement) {
80
        if ($this->padding && !$this->lineStarted) {
81
            echo $this->padding;
82
            $this->lineStarted = true;
83
        }
84
        echo $statement;
85
86
        return $this;
87
    }
88
89
    /**
90
     * @param string $statement
91
     * @throws \Yaoi\String\Exception
92
     * @return static
93
     */
94
    public function printLine($statement) {
95
        $this->printF($statement)->eol();
96
        return $this;
97
    }
98
99
    public function printLines($statement) {
100
        $lines = explode("\n", str_replace("\r\n", "\n", $statement));
101
        foreach ($lines as $line) {
102
            $this->printLine($line);
103
        }
104
        return $this;
105
    }
106
107
    private $lineStarted = false;
108
    private $padding = '';
109
    public function setPadding($padding = '   ') {
110
        $this->padding = $padding;
111
        return $this;
112
    }
113
114
115
    /**
116
     * @return static
117
     */
118
    public static function getInstance() {
119
        static $instance;
120
        if (null === $instance) {
121
            $instance = new static;
122
        }
123
        return $instance;
124
    }
125
126
    public $forceColors = false;
127
128
    public function attached()
129
    {
130
        return posix_isatty(STDOUT);
131
    }
132
133
134
}