|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Matks\Vivian\Output; |
|
4
|
|
|
|
|
5
|
|
|
use Matks\Vivian\Color\BackgroundColor; |
|
6
|
|
|
use Matks\Vivian\Color\TextColor; |
|
7
|
|
|
use Matks\Vivian\Style\Style; |
|
8
|
|
|
use Exception; |
|
9
|
|
|
|
|
10
|
|
|
/** |
|
11
|
|
|
* Element |
|
12
|
|
|
* |
|
13
|
|
|
* Manageable element |
|
14
|
|
|
*/ |
|
15
|
|
|
abstract class Element |
|
16
|
|
|
{ |
|
17
|
|
|
const ANSI_ESCAPE_CODE_RESET = "\033[0m"; |
|
18
|
|
|
const ANSI_ESCAPE_CODE_REGEX = '\\033\[[\d]+m'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* @var TextColor |
|
22
|
|
|
*/ |
|
23
|
|
|
private $textColor; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var BackgroundColor |
|
27
|
|
|
*/ |
|
28
|
|
|
private $backgroundColor; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $styles; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param BackgroundColor $backgroundColor |
|
37
|
|
|
* |
|
38
|
|
|
* @return static |
|
39
|
|
|
*/ |
|
40
|
|
|
public function setBackgroundColor(BackgroundColor $backgroundColor) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->backgroundColor = $backgroundColor; |
|
43
|
|
|
|
|
44
|
1 |
|
return $this; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return BackgroundColor |
|
49
|
|
|
*/ |
|
50
|
|
|
public function getBackgroundColor() |
|
51
|
|
|
{ |
|
52
|
1 |
|
return $this->backgroundColor; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @param TextColor $textColor |
|
57
|
|
|
* |
|
58
|
|
|
* @return static |
|
59
|
|
|
*/ |
|
60
|
|
|
public function setTextColor(TextColor $textColor) |
|
61
|
1 |
|
{ |
|
62
|
1 |
|
$this->textColor = $textColor; |
|
63
|
|
|
|
|
64
|
1 |
|
return $this; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return TextColor |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getTextColor() |
|
71
|
|
|
{ |
|
72
|
1 |
|
return $this->textColor; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param Style $style |
|
77
|
|
|
* |
|
78
|
|
|
* @return static |
|
79
|
|
|
*/ |
|
80
|
|
|
public function addStyle(Style $style) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$this->styles[] = $style; |
|
83
|
|
|
|
|
84
|
1 |
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param Style $style |
|
89
|
|
|
* |
|
90
|
|
|
* @return static |
|
91
|
|
|
*/ |
|
92
|
|
|
public function removeStyle(Style $style) |
|
93
|
|
|
{ |
|
94
|
1 |
|
$styleKey = null; |
|
95
|
1 |
|
foreach ($this->styles as $key => $currentStyle) { |
|
96
|
1 |
|
if ($currentStyle->equals($style)) { |
|
97
|
1 |
|
$styleKey = $key; |
|
98
|
1 |
|
break; |
|
99
|
|
|
} |
|
100
|
1 |
|
} |
|
101
|
|
|
|
|
102
|
1 |
|
if (null === $styleKey) { |
|
103
|
|
|
throw new Exception("Style $style is not associated with this element"); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
1 |
|
array_splice($this->styles, $styleKey, 1); |
|
107
|
|
|
|
|
108
|
1 |
|
return $this; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* @return Style[] |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getStyles() |
|
115
|
|
|
{ |
|
116
|
1 |
|
return $this->styles; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @return string |
|
121
|
|
|
*/ |
|
122
|
|
|
abstract public function render(); |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* Apply escape code to string and close section |
|
126
|
|
|
* |
|
127
|
|
|
* @param string $string |
|
128
|
|
|
* @param string $escapeCharacter |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
protected function frame($string, $escapeCharacter) |
|
133
|
|
|
{ |
|
134
|
1 |
|
$result = $escapeCharacter . $string . static::ANSI_ESCAPE_CODE_RESET; |
|
135
|
|
|
|
|
136
|
1 |
|
return $result; |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|