Repository navigation
Expand file tree
/
Copy pathConsoleStyle.php
More file actions
156 lines (127 loc) · 4.06 KB
/
Copy pathConsoleStyle.php
File metadata and controls
156 lines (127 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
<?php
declare(strict_types=1);
/*
* This file is part of the PHPRegex package.
*
* (c) Younes ENNAJI <younes.ennaji.pro@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPRegex\Cli;
use PHPRegex\Toolkit\Regex;
/**
* @internal
*/
final readonly class ConsoleStyle
{
private const INDENT = ' ';
private const PATTERN_INDENT = ' ';
private const ARROW = "\xE2\x86\x92";
public function __construct(private Output $output, private bool $visuals = true) {}
public function visualsEnabled(): bool
{
return $this->visuals;
}
/**
* @param array<string, string> $meta
*/
public function renderBanner(string $command, array $meta = [], ?string $tagline = null): void
{
if (!$this->visuals) {
return;
}
$this->writeTitleBlock($tagline);
$this->writeRuntimeInfo($command, $meta);
$this->output->write("\n");
}
public function renderSection(string $title, ?int $step = null, ?int $total = null): void
{
if (!$this->visuals) {
return;
}
$prefix = $this->formatStepPrefix($step, $total);
$this->output->write(self::INDENT.$this->output->dim($prefix.$title)."\n");
}
public function renderPattern(string $pattern, string $label = 'Pattern'): void
{
if (!$this->visuals) {
$this->output->write(self::INDENT.$label.': '.$pattern."\n");
return;
}
$this->output->write(self::INDENT.$this->output->color($label, Output::CYAN.Output::BOLD)."\n");
$this->output->write(
self::PATTERN_INDENT
.$this->output->color(self::ARROW.' ', Output::CYAN.Output::BOLD)
.$pattern
."\n",
);
}
/**
* @param array<string, string> $rows
*/
public function renderKeyValueBlock(array $rows, int $indent = 2): void
{
if ([] === $rows) {
return;
}
$maxLabelLength = max(array_map(strlen(...), array_keys($rows)));
$prefix = str_repeat(' ', max(0, $indent));
foreach ($rows as $label => $value) {
$this->output->write($this->formatKeyValueLine($prefix, $maxLabelLength, $label, $value)."\n");
}
}
private function writeTitleBlock(?string $tagline): void
{
$version = Regex::VERSION;
$this->output->write(
$this->output->color('PHPRegex', Output::CYAN.Output::BOLD)
.' '
.$this->output->warning($version)
." by Younes ENNAJI\n",
);
if (null !== $tagline && '' !== $tagline) {
$this->output->write($this->output->dim($tagline)."\n");
}
$this->output->write("\n");
}
/**
* @param array<string, string> $meta
*/
private function writeRuntimeInfo(string $command, array $meta): void
{
$lines = [
'Runtime' => 'PHP '.$this->output->warning(\PHP_VERSION),
'Command' => $this->output->warning($command),
];
foreach ($meta as $label => $value) {
$lines[$label] = $value;
}
$maxLabelLength = max(array_map(strlen(...), array_keys($lines)));
foreach ($lines as $label => $value) {
$this->output->write(
$this->output->bold(str_pad($label, $maxLabelLength))
.' : '
.$value
."\n",
);
}
}
private function formatStepPrefix(?int $step, ?int $total): string
{
if (null === $step || null === $total) {
return '';
}
return '['.$step.'/'.$total.'] ';
}
private function formatKeyValueLine(string $prefix, int $maxLabelLength, string $label, string $value): string
{
if ($this->visuals) {
return $prefix
.$this->output->dim(str_pad($label, $maxLabelLength))
.' : '
.$value;
}
return $prefix.$label.': '.$value;
}
}