|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace kalanis\kw_clipr\Output; |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Class AOutput |
|
8
|
|
|
* @package kalanis\kw_clipr\Output |
|
9
|
|
|
* Abstraction to set different output codes |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AOutput |
|
12
|
|
|
{ |
|
13
|
|
|
protected string $closeSequence = ''; |
|
14
|
|
|
protected string $formatBackSequence = ''; |
|
15
|
|
|
protected string $eolSequence = PHP_EOL; |
|
16
|
|
|
/** @var string[] */ |
|
17
|
|
|
protected array $tags = []; |
|
18
|
|
|
|
|
19
|
38 |
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
38 |
|
$this->setTags(); |
|
22
|
38 |
|
} |
|
23
|
|
|
|
|
24
|
38 |
|
protected function setTags(): void |
|
25
|
|
|
{ |
|
26
|
|
|
# format style |
|
27
|
38 |
|
$this->addTranslation('normal', ''); |
|
28
|
38 |
|
$this->addTranslation('bold', ''); |
|
29
|
38 |
|
$this->addTranslation('under', ''); |
|
30
|
38 |
|
$this->addTranslation('blink', ''); |
|
31
|
|
|
|
|
32
|
|
|
# default ones |
|
33
|
|
|
# foreground colors |
|
34
|
38 |
|
$this->addTranslation('black', ''); |
|
35
|
38 |
|
$this->addTranslation('red', ''); |
|
36
|
38 |
|
$this->addTranslation('green', ''); |
|
37
|
38 |
|
$this->addTranslation('brown', ''); |
|
38
|
38 |
|
$this->addTranslation('blue', ''); |
|
39
|
38 |
|
$this->addTranslation('purple', ''); |
|
40
|
38 |
|
$this->addTranslation('cyan', ''); |
|
41
|
38 |
|
$this->addTranslation('gray', ''); |
|
42
|
|
|
|
|
43
|
|
|
# background colors |
|
44
|
38 |
|
$this->addTranslation('blackbg', ''); |
|
45
|
38 |
|
$this->addTranslation('redbg', ''); |
|
46
|
38 |
|
$this->addTranslation('greenbg', ''); |
|
47
|
38 |
|
$this->addTranslation('brownbg', ''); |
|
48
|
38 |
|
$this->addTranslation('bluebg', ''); |
|
49
|
38 |
|
$this->addTranslation('purplebg', ''); |
|
50
|
38 |
|
$this->addTranslation('cyanbg', ''); |
|
51
|
38 |
|
$this->addTranslation('graybg', ''); |
|
52
|
|
|
|
|
53
|
|
|
# stronger ones |
|
54
|
|
|
# foreground colors |
|
55
|
38 |
|
$this->addTranslation('dgray', ''); |
|
56
|
38 |
|
$this->addTranslation('lred', ''); |
|
57
|
38 |
|
$this->addTranslation('lgreen', ''); |
|
58
|
38 |
|
$this->addTranslation('yellow', ''); |
|
59
|
38 |
|
$this->addTranslation('lblue', ''); |
|
60
|
38 |
|
$this->addTranslation('magenta', ''); |
|
61
|
38 |
|
$this->addTranslation('lcyan', ''); |
|
62
|
38 |
|
$this->addTranslation('white', ''); |
|
63
|
|
|
|
|
64
|
|
|
# background colors |
|
65
|
38 |
|
$this->addTranslation('dgraybg', ''); |
|
66
|
38 |
|
$this->addTranslation('lredbg', ''); |
|
67
|
38 |
|
$this->addTranslation('lgreenbg', ''); |
|
68
|
38 |
|
$this->addTranslation('yellowbg', ''); |
|
69
|
38 |
|
$this->addTranslation('lbluebg', ''); |
|
70
|
38 |
|
$this->addTranslation('magentabg', ''); |
|
71
|
38 |
|
$this->addTranslation('lcyanbg', ''); |
|
72
|
38 |
|
$this->addTranslation('whitebg', ''); |
|
73
|
38 |
|
} |
|
74
|
|
|
|
|
75
|
38 |
|
protected function addTranslation(string $tag, string $translation): void |
|
76
|
|
|
{ |
|
77
|
38 |
|
$this->tags[$tag] = $translation; |
|
78
|
38 |
|
} |
|
79
|
|
|
|
|
80
|
13 |
|
public function translate(string $message): string |
|
81
|
|
|
{ |
|
82
|
13 |
|
foreach ($this->tags as $color => $translation) { |
|
83
|
13 |
|
$endSequence = empty($translation) ? '' : $this->closeSequence; |
|
84
|
13 |
|
$message = preg_replace('/\<' . $color . '\>(.*?)\<\/' . $color . '\>/i', $translation . '\1' . $endSequence, strval($message)); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
13 |
|
return strval($message); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function getStepsBack(int $len = 1): string |
|
91
|
|
|
{ |
|
92
|
1 |
|
return sprintf($this->formatBackSequence, $len); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
18 |
|
public function getEol(): string |
|
96
|
|
|
{ |
|
97
|
18 |
|
return $this->eolSequence; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|