1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace AlecRabbit\Cli; |
4
|
|
|
|
5
|
|
|
use AlecRabbit\Cli\Tools\Core\Contracts\TerminalInterface; |
6
|
|
|
use AlecRabbit\Cli\Tools\Core\Terminal as StaticTerminal; |
7
|
|
|
use function AlecRabbit\Helpers\inRange; |
8
|
|
|
use const AlecRabbit\ALLOWED_COLOR_TERMINAL; |
9
|
|
|
|
10
|
|
|
class Terminal implements TerminalInterface |
11
|
|
|
{ |
12
|
|
|
protected const MIN_WIDTH = 20; |
13
|
|
|
protected const MAX_WIDTH = 280; |
14
|
|
|
protected const MIN_HEIGHT = 20; |
15
|
|
|
protected const MAX_HEIGHT = 80; |
16
|
|
|
|
17
|
|
|
/** @var int */ |
18
|
|
|
protected $width; |
19
|
|
|
/** @var int */ |
20
|
|
|
protected $height; |
21
|
|
|
/** @var int */ |
22
|
|
|
protected $color; |
23
|
|
|
|
24
|
12 |
|
public function __construct(?int $colorSupport = null, ?int $width = null, ?int $height = null) |
25
|
|
|
{ |
26
|
12 |
|
$this->width = $this->refineWidth($width); |
27
|
11 |
|
$this->height = $this->refineHeight($height); |
28
|
10 |
|
$this->color = $this->refineColorSupport($colorSupport); |
29
|
9 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param int $width |
33
|
|
|
* @return int |
34
|
|
|
*/ |
35
|
12 |
|
protected function refineWidth(?int $width): int |
36
|
|
|
{ |
37
|
12 |
|
$this->assertWidth($width); |
38
|
11 |
|
return $width ?? StaticTerminal::width(); |
39
|
|
|
} |
40
|
|
|
|
41
|
12 |
|
protected function assertWidth(?int $width): void |
42
|
|
|
{ |
43
|
12 |
|
if (null !== $width && !inRange($width, static::MIN_WIDTH, static::MAX_WIDTH)) { |
44
|
1 |
|
throw new \RuntimeException('Terminal size bounds exceeded.'); |
45
|
|
|
} |
46
|
11 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param int $height |
50
|
|
|
* @return int |
51
|
|
|
*/ |
52
|
11 |
|
protected function refineHeight(?int $height): int |
53
|
|
|
{ |
54
|
11 |
|
$this->assertHeight($height); |
55
|
10 |
|
return $height ?? StaticTerminal::height(); |
56
|
|
|
} |
57
|
|
|
|
58
|
11 |
|
protected function assertHeight(?int $height): void |
59
|
|
|
{ |
60
|
11 |
|
if (null !== $height && !inRange($height, static::MIN_HEIGHT, static::MAX_HEIGHT)) { |
61
|
1 |
|
throw new \RuntimeException('Terminal size bounds exceeded.'); |
62
|
|
|
} |
63
|
10 |
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param int $colorSupport |
67
|
|
|
* @return int |
68
|
|
|
*/ |
69
|
10 |
|
protected function refineColorSupport(?int $colorSupport): int |
70
|
|
|
{ |
71
|
10 |
|
$this->assertColorSupport($colorSupport); |
72
|
9 |
|
return $colorSupport ?? StaticTerminal::colorSupport(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @param int $colorSupport |
77
|
|
|
*/ |
78
|
10 |
|
protected function assertColorSupport(?int $colorSupport): void |
79
|
|
|
{ |
80
|
10 |
|
if (null !== $colorSupport && !\in_array($colorSupport, ALLOWED_COLOR_TERMINAL, true)) { |
81
|
1 |
|
throw new \RuntimeException('Unknown color support level.'); |
82
|
|
|
} |
83
|
9 |
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @return int |
87
|
|
|
*/ |
88
|
9 |
|
public function width(): int |
89
|
|
|
{ |
90
|
9 |
|
return $this->width; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
9 |
|
public function height(): int |
97
|
|
|
{ |
98
|
9 |
|
return $this->height; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return int |
103
|
|
|
*/ |
104
|
9 |
|
public function color(): int |
105
|
|
|
{ |
106
|
9 |
|
return $this->color; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|