1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace VPA\Console\Glyphs; |
4
|
|
|
|
5
|
|
|
use VPA\Console\FrameConfigInterface; |
6
|
|
|
use VPA\Console\Symbol; |
7
|
|
|
|
8
|
|
|
class Text extends GlyphInline |
9
|
|
|
{ |
10
|
|
|
private string $text = ''; |
11
|
|
|
|
12
|
31 |
|
public function setValue(string $text): GlyphInline |
13
|
|
|
{ |
14
|
31 |
|
$this->text = $text; |
15
|
31 |
|
$batches = $this->splitText(); |
16
|
31 |
|
$lengths = array_map(function ($it) { |
17
|
31 |
|
return strlen($it); |
18
|
|
|
}, $batches); |
19
|
31 |
|
$this->width = max($lengths); |
20
|
31 |
|
$this->height = count($batches); |
21
|
31 |
|
$this->render(); |
22
|
31 |
|
return $this; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function getValue(): string |
26
|
|
|
{ |
27
|
|
|
return $this->text; |
28
|
|
|
} |
29
|
|
|
|
30
|
31 |
|
public function render(): GlyphInline |
31
|
|
|
{ |
32
|
31 |
|
$batches = $this->splitText(); |
33
|
31 |
|
$lengths = array_map(function ($it) { |
34
|
31 |
|
return strlen($it); |
35
|
|
|
}, $batches); |
36
|
31 |
|
$this->height = count($batches); |
37
|
31 |
|
$maxLength = max($lengths); |
38
|
31 |
|
$parent = $this->getParent(); |
39
|
31 |
|
$contentWidth = $parent && $parent instanceof GlyphBlock ? $parent->getContentWidth() : 0; |
40
|
31 |
|
$this->width = $contentWidth > 0 ? $contentWidth : min($maxLength, $this->__get('maxWidth')); |
41
|
31 |
|
$this->__set('maxWidth', max($this->width, $this->__get('maxWidth'))); |
42
|
31 |
|
$padding = match ($this->__get('textAlign')) { |
43
|
29 |
|
default => STR_PAD_RIGHT, |
44
|
1 |
|
'right' => STR_PAD_LEFT, |
45
|
1 |
|
'center' => STR_PAD_BOTH, |
46
|
|
|
}; |
47
|
31 |
|
$resultString = ''; |
48
|
31 |
|
foreach ($batches as $batch) { |
49
|
31 |
|
$resultString .= str_pad(ltrim($batch), $this->getWidth(), ' ', $padding); |
50
|
|
|
} |
51
|
31 |
|
$symbols = str_split($resultString) ?? []; |
52
|
31 |
|
$symbolConfig = [ |
53
|
31 |
|
'mode' => $this->__get('mode'), |
54
|
31 |
|
'color' => $this->__get('color'), |
55
|
31 |
|
'backgroundColor' => $this->__get('backgroundColor'), |
56
|
|
|
]; |
57
|
31 |
|
$this->renderMap = $this->getWidth() ? array_chunk(array_map(function ($value) use ($symbolConfig) { |
58
|
31 |
|
return (new Symbol($value))->setConfig($symbolConfig); |
59
|
31 |
|
}, $symbols), $this->getWidth()) : []; |
|
|
|
|
60
|
31 |
|
$this->height = count($this->renderMap); |
61
|
31 |
|
return $this; |
62
|
|
|
} |
63
|
|
|
|
64
|
31 |
|
private function splitText(): array |
65
|
|
|
{ |
66
|
31 |
|
$parent = $this->getParent(); |
67
|
31 |
|
$deltaWidth = $parent && $parent instanceof GlyphBlock ? $parent->getDeltaWidth() : 0; |
68
|
31 |
|
$result = []; |
69
|
31 |
|
$batches = explode("\n", $this->text); |
70
|
31 |
|
foreach ($batches as $batch) { |
71
|
31 |
|
$chunks = str_split(ltrim($batch), $this->__get('maxWidth') - $deltaWidth); |
72
|
31 |
|
foreach ($chunks as $chunk) { |
73
|
31 |
|
$result[] = $chunk; |
74
|
|
|
} |
75
|
|
|
} |
76
|
31 |
|
return $result; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|