Passed
Push — master ( d93186...fdbeea )
by Alec
01:52
created

Driver::showCursor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace AlecRabbit\Snake\Core;
6
7
use AlecRabbit\Snake\Contracts\Color;
8
9
class Driver
10
{
11
    public const HIDE_CURSOR_SEQ = "\033[?25l";
12
    public const SHOW_CURSOR_SEQ = "\033[?25h";
13
14
    /** @var false|resource */
15
    private $stream = STDERR;
16
17
    /** @var int */
18
    private $colorLevel;
19
20 1
    public function __construct(int $colorLevel)
21
    {
22 1
        $this->setColorLevel($colorLevel);
23 1
    }
24
25
    /**
26
     * @param int $colorLevel
27
     */
28 1
    public function setColorLevel(int $colorLevel): void
29
    {
30 1
        if (!in_array($colorLevel, Color::ALLOWED, true)) {
31
            throw new \InvalidArgumentException('Unknown color level.');
32
        }
33 1
        $this->colorLevel = $colorLevel;
34 1
    }
35
36
//    public function erase(): void
37
//    {
38
//        $this->write("\033[1X");
39
//    }
40
//
41
42 1
    public function moveBackSequence(): string
43
    {
44 1
        return "\033[1D";
45
    }
46
47 1
    public function eraseSequence(): string
48
    {
49 1
        return "\033[1X";
50
    }
51
52 1
    public function frameSequence(int $fg, string $char): string
53
    {
54 1
        if (Color::COLOR_256 === $this->colorLevel) {
55
            return "\033[38;5;{$fg}m{$char}\033[0m";
56
        }
57 1
        if (Color::COLOR_16 === $this->colorLevel) {
58
            return "\033[96m{$char}\033[0m";
59
        }
60 1
        return $char;
61
    }
62
63 1
    public function hideCursor(): void
64
    {
65 1
        $this->write(self::HIDE_CURSOR_SEQ);
66 1
    }
67
68
    /**
69
     * @codeCoverageIgnore
70
     *
71
     * @param string ...$text
72
     */
73
    public function write(string ...$text): void
74
    {
75
        foreach ($text as $s) {
76
            if (false === $this->stream) {
77
                echo $s;
78
            } elseif (false === @fwrite($this->stream, $s)) {
0 ignored issues
show
Bug introduced by
It seems like $this->stream can also be of type true; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

78
            } elseif (false === @fwrite(/** @scrutinizer ignore-type */ $this->stream, $s)) {
Loading history...
79
                // should never happen
80
                throw new \RuntimeException('Unable to write stream.');
81
            }
82
        }
83
        if (false !== $this->stream) {
84
            fflush($this->stream);
0 ignored issues
show
Bug introduced by
It seems like $this->stream can also be of type true; however, parameter $handle of fflush() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

84
            fflush(/** @scrutinizer ignore-type */ $this->stream);
Loading history...
85
        }
86
    }
87
88 1
    public function showCursor(): void
89
    {
90 1
        $this->write(self::SHOW_CURSOR_SEQ);
91 1
    }
92
93
    /**
94
     * @codeCoverageIgnore
95
     */
96
    public function disableStdErr(): void
97
    {
98
        $this->stream = false;
99
    }
100
}
101