Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
CliOutput
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
4 / 4
13
100.00% covered (success)
100.00%
1 / 1
 setColor
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
5
 setUseColor
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 write
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
6
 writeLine
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3namespace Dynart\Micro;
4
5class CliOutput {
6    
7    const COLOR_OFF = "\033[0m";
8
9    const BLACK       = 0;
10    const DARK_RED    = 1;
11    const DARK_GREEN  = 2;
12    const DARK_YELLOW = 3;
13    const DARK_BLUE   = 4;
14    const DARK_PURPLE = 5;
15    const DARK_CYAN   = 6;
16    const GRAY        = 7;
17    const DARK_GRAY   = 8;
18    const RED         = 9;
19    const GREEN       = 10;
20    const YELLOW      = 11;
21    const BLUE        = 12;
22    const PURPLE      = 13;
23    const CYAN        = 14;
24    const WHITE       = 15;
25
26    protected ?string $color = null;
27    protected ?string $bgColor = null;
28    protected bool $useColor = true;
29
30    public function setColor($color, $bgColor = null): void {
31        if (is_int($color)) {
32            $this->color = "\033[" . ($color < 8 ? 30 + $color : 90 + $color - 8) . "m";
33        } else {
34            $this->color = null;
35        }
36        if (is_int($bgColor)) {
37            $this->bgColor = "\033[".($bgColor < 8 ? 40 + $bgColor : 100 + $bgColor - 8)."m";
38        } else {
39            $this->bgColor = null;
40        }
41    }
42
43    public function setUseColor(bool $value): void {
44        $this->useColor = $value;
45    }
46
47    public function write(string $text): void {
48        if ($this->useColor) {
49            if ($this->bgColor) {
50                echo $this->bgColor;
51            }
52            if ($this->color) {
53                echo $this->color;
54            }
55            echo $text;
56            if ($this->color || $this->bgColor) {
57                echo self::COLOR_OFF;
58            }
59        } else {
60            echo $text;
61        }
62    }
63
64    public function writeLine(string $text): void {
65        $this->write($text);
66        echo "\n";
67    }
68}