-
Notifications
You must be signed in to change notification settings - Fork 3.4k
/
TableHelper.php
186 lines (167 loc) · 5.34 KB
/
TableHelper.php
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @since 3.1.0
* @license https://opensource.org/licenses/mit-license.php MIT License
*/
namespace Cake\Command\Helper;
use Cake\Console\Helper;
use UnexpectedValueException;
/**
* Create a visually pleasing ASCII art table
* from 2 dimensional array data.
*/
class TableHelper extends Helper
{
/**
* Default config for this helper.
*
* @var array<string, mixed>
*/
protected array $_defaultConfig = [
'headers' => true,
'rowSeparator' => false,
'headerStyle' => 'info',
];
/**
* Calculate the column widths
*
* @param array $rows The rows on which the columns width will be calculated on.
* @return array<int>
*/
protected function _calculateWidths(array $rows): array
{
$widths = [];
foreach ($rows as $line) {
foreach (array_values($line) as $k => $v) {
/** @psalm-suppress InvalidCast */
$columnLength = $this->_cellWidth((string)$v);
if ($columnLength >= ($widths[$k] ?? 0)) {
$widths[$k] = $columnLength;
}
}
}
return $widths;
}
/**
* Get the width of a cell exclusive of style tags.
*
* @param string $text The text to calculate a width for.
* @return int The width of the textual content in visible characters.
*/
protected function _cellWidth(string $text): int
{
if ($text === '') {
return 0;
}
if (!str_contains($text, '<') && !str_contains($text, '>')) {
return mb_strwidth($text);
}
$styles = $this->_io->styles();
$tags = implode('|', array_keys($styles));
$text = (string)preg_replace('#</?(?:' . $tags . ')>#', '', $text);
return mb_strwidth($text);
}
/**
* Output a row separator.
*
* @param array<int> $widths The widths of each column to output.
* @return void
*/
protected function _rowSeparator(array $widths): void
{
$out = '';
foreach ($widths as $column) {
$out .= '+' . str_repeat('-', $column + 2);
}
$out .= '+';
$this->_io->out($out);
}
/**
* Output a row.
*
* @param array $row The row to output.
* @param array<int> $widths The widths of each column to output.
* @param array<string, mixed> $options Options to be passed.
* @return void
*/
protected function _render(array $row, array $widths, array $options = []): void
{
if (count($row) === 0) {
return;
}
$out = '';
foreach (array_values($row) as $i => $column) {
$column = (string)$column;
$pad = $widths[$i] - $this->_cellWidth($column);
if (!empty($options['style'])) {
$column = $this->_addStyle($column, $options['style']);
}
if ($column !== '' && preg_match('#(.*)<text-right>.+</text-right>(.*)#', $column, $matches)) {
if ($matches[1] !== '' || $matches[2] !== '') {
throw new UnexpectedValueException('You cannot include text before or after the text-right tag.');
}
$column = str_replace(['<text-right>', '</text-right>'], '', $column);
$out .= '| ' . str_repeat(' ', $pad) . $column . ' ';
} else {
$out .= '| ' . $column . str_repeat(' ', $pad) . ' ';
}
}
$out .= '|';
$this->_io->out($out);
}
/**
* Output a table.
*
* Data will be output based on the order of the values
* in the array. The keys will not be used to align data.
*
* @param array $args The data to render out.
* @return void
*/
public function output(array $args): void
{
if (!$args) {
return;
}
$this->_io->setStyle('text-right', ['text' => null]);
$config = $this->getConfig();
$widths = $this->_calculateWidths($args);
$this->_rowSeparator($widths);
if ($config['headers'] === true) {
$this->_render(array_shift($args), $widths, ['style' => $config['headerStyle']]);
$this->_rowSeparator($widths);
}
if (!$args) {
return;
}
foreach ($args as $line) {
$this->_render($line, $widths);
if ($config['rowSeparator'] === true) {
$this->_rowSeparator($widths);
}
}
if ($config['rowSeparator'] !== true) {
$this->_rowSeparator($widths);
}
}
/**
* Add style tags
*
* @param string $text The text to be surrounded
* @param string $style The style to be applied
* @return string
*/
protected function _addStyle(string $text, string $style): string
{
return '<' . $style . '>' . $text . '</' . $style . '>';
}
}