1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Spinner\Core\Jugglers; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Accessories\Circular; |
6
|
|
|
use AlecRabbit\Spinner\Core\Coloring\Style; |
7
|
|
|
use AlecRabbit\Spinner\Core\Jugglers\Contracts\JugglerInterface; |
8
|
|
|
use AlecRabbit\Spinner\Settings\Contracts\Defaults; |
9
|
|
|
use AlecRabbit\Spinner\Settings\Settings; |
10
|
|
|
use function AlecRabbit\Helpers\wcswidth; |
11
|
|
|
|
12
|
|
|
abstract class AbstractJuggler implements JugglerInterface |
13
|
|
|
{ |
14
|
|
|
/** @var string */ |
15
|
|
|
protected $spacer = Defaults::ONE_SPACE_SYMBOL; |
16
|
|
|
/** |
17
|
|
|
* @var int |
18
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
19
|
|
|
*/ |
20
|
|
|
protected $currentFrameErasingWidth; |
21
|
|
|
/** |
22
|
|
|
* @var Circular |
23
|
|
|
* @psalm-suppress PropertyNotSetInConstructor |
24
|
|
|
*/ |
25
|
|
|
protected $style; |
26
|
|
|
/** @var int */ |
27
|
|
|
protected $formatErasingWidthShift = 0; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param Settings $settings |
31
|
|
|
* @param Style $style |
32
|
|
|
*/ |
33
|
|
|
abstract public function __construct(Settings $settings, Style $style); |
34
|
|
|
|
35
|
|
|
/** {@inheritDoc} */ |
36
|
25 |
|
public function getFrameErasingWidth(): int |
37
|
|
|
{ |
38
|
25 |
|
return $this->currentFrameErasingWidth; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** {@inheritDoc} */ |
42
|
25 |
|
public function getStyledFrame(): string |
43
|
|
|
{ |
44
|
|
|
return |
45
|
25 |
|
sprintf((string)$this->style->value(), $this->getCurrentFrame()) . $this->spacer; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
abstract protected function getCurrentFrame(): string; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param Style $style |
55
|
|
|
*/ |
56
|
27 |
|
protected function init(Style $style): void |
57
|
|
|
{ |
58
|
27 |
|
$this->style = $style->getStyle(); |
59
|
27 |
|
$this->formatErasingWidthShift = $this->computeFormatErasingWidthShift($style->getFormat()); |
60
|
27 |
|
$this->spacer = $style->getSpacer(); |
61
|
27 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $format |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
27 |
|
protected function computeFormatErasingWidthShift(string $format): int |
68
|
|
|
{ |
69
|
27 |
|
return wcswidth(sprintf($format, '')); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|