Line::setShowResult()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace WebServCo\Framework\Cli\Progress;
6
7
final class Line
8
{
9
    protected int $padding;
10
11
    protected string $outPad;
12
    protected string $outMessage;
13
14
    protected bool $showResult;
15
16
    public function __construct()
17
    {
18
        $this->showResult = false;
19
        $this->padding = 0;
20
    }
21
22
    public function setShowResult(bool $showResult): bool
23
    {
24
        $this->showResult = $showResult;
25
        return true;
26
    }
27
28
    public function prefix(string $message = ''): string
29
    {
30
        $this->outMessage = $message;
31
32
        $this->outPad = 0 < $this->padding
33
            ? \str_repeat(' ', (int) $this->padding)
34
            : '';
35
        return $this->outPad . $this->outMessage;
36
    }
37
38
    public function suffix(bool $result = true): string
39
    {
40
        $totalLen = \strlen($this->outPad . $this->outMessage);
41
        $output = null;
42
43
        //overwrite current line
44
        $output .= "\033[" . $totalLen . 'D';
45
        $output .= \str_repeat(' ', $this->padding);
46
        $output .= $this->outMessage;
47
48
        $padLen = 74 - $totalLen;
49
        if (0 < $padLen) {
50
            $output .= \str_repeat(' ', $padLen);
51
        }
52
        if ($this->showResult) {
53
            $output .= '[';
54
            $output .= $result
55
                ? "\e[32mOK"
56
                : "\e[31mKO";
57
            $output .= "\e[0m" . ']';
58
        }
59
60
        $output .= "\r";
61
62
        return $output;
63
    }
64
65
    public function finish(): string
66
    {
67
        return "\033[" . 0 . 'D' . \str_repeat(' ', 74) . "\r";
68
    }
69
}
70