-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConsoleTable.php
379 lines (322 loc) · 10 KB
/
ConsoleTable.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
<?php
/**
* This file is part of the PHPLucidFrame library.
* The class makes you easy to build console style tables
*
* @package PHPLucidFrame\Console
* @since PHPLucidFrame v 1.12.0
* @copyright Copyright (c), PHPLucidFrame.
* @author Sithu K. <[email protected]>
* @link http://phplucidframe.com
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE
*/
namespace LucidFrame\Console;
/**
* The class makes you easy to build console style tables
*/
class ConsoleTable
{
const HEADER_INDEX = -1;
const HR = 'HR';
/** @var array Array of table data */
protected $data = array();
/** @var boolean Border shown or not */
protected $border = true;
/** @var boolean All borders shown or not */
protected $allBorders = false;
/** @var integer Table padding */
protected $padding = 1;
/** @var integer Table left margin */
protected $indent = 0;
/** @var integer */
private $rowIndex = -1;
/** @var array */
private $columnWidths = array();
/** @var int */
private $maxColumnCount = 0;
/**
* Adds a column to the table header
* @param mixed Header cell content
* @return object LucidFrame\Console\ConsoleTable
*/
public function addHeader($content = '')
{
$this->data[self::HEADER_INDEX][] = $content;
return $this;
}
/**
* Set headers for the columns in one-line
* @param array Array of header cell content
* @return object LucidFrame\Console\ConsoleTable
*/
public function setHeaders(array $content)
{
$this->data[self::HEADER_INDEX] = $content;
return $this;
}
/**
* Get the row of header
*/
public function getHeaders()
{
return isset($this->data[self::HEADER_INDEX]) ? $this->data[self::HEADER_INDEX] : null;
}
/**
* Adds a row to the table
* @param array $data The row data to add
* @return object LucidFrame\Console\ConsoleTable
*/
public function addRow(array $data = null)
{
$this->rowIndex++;
if (is_array($data)) {
foreach ($data as $col => $content) {
$this->data[$this->rowIndex][$col] = $content;
}
$this->setMaxColumnCount(count($this->data[$this->rowIndex]));
}
return $this;
}
/**
* Adds a column to the table
* @param mixed $content The data of the column
* @param integer $col The column index to populate
* @param integer $row If starting row is not zero, specify it here
* @return object LucidFrame\Console\ConsoleTable
*/
public function addColumn($content, $col = null, $row = null)
{
$row = $row === null ? $this->rowIndex : $row;
if ($col === null) {
$col = isset($this->data[$row]) ? count($this->data[$row]) : 0;
}
$this->data[$row][$col] = $content;
$this->setMaxColumnCount(count($this->data[$row]));
return $this;
}
/**
* Show table border
* @return object LucidFrame\Console\ConsoleTable
*/
public function showBorder()
{
$this->border = true;
return $this;
}
/**
* Hide table border
* @return object LucidFrame\Console\ConsoleTable
*/
public function hideBorder()
{
$this->border = false;
return $this;
}
/**
* Show all table borders
* @return object LucidFrame\Console\ConsoleTable
*/
public function showAllBorders()
{
$this->showBorder();
$this->allBorders = true;
return $this;
}
/**
* Set padding for each cell
* @param integer $value The integer value, defaults to 1
* @return object LucidFrame\Console\ConsoleTable
*/
public function setPadding($value = 1)
{
$this->padding = $value;
return $this;
}
/**
* Set left indentation for the table
* @param integer $value The integer value, defaults to 1
* @return object LucidFrame\Console\ConsoleTable
*/
public function setIndent($value = 0)
{
$this->indent = $value;
return $this;
}
/**
* Add horizontal border line
* @return object LucidFrame\Console\ConsoleTable
*/
public function addBorderLine()
{
$this->rowIndex++;
$this->data[$this->rowIndex] = self::HR;
return $this;
}
/**
* Print the table
* @return void
*/
public function display()
{
echo $this->getTable();
}
/**
* Get the printable table content
* @return string
*/
public function getTable()
{
$this->calculateColumnWidth();
$output = $this->border ? $this->getBorderLine() : '';
foreach ($this->data as $y => $row) {
if ($row === self::HR) {
if (!$this->allBorders) {
$output .= $this->getBorderLine();
unset($this->data[$y]);
}
continue;
}
if ($y === self::HEADER_INDEX && count($row) < $this->maxColumnCount) {
$row = $row + array_fill(count($row), $this->maxColumnCount - count($row), ' ');
}
foreach ($row as $x => $cell) {
$output .= $this->getCellOutput($x, $row);
}
$output .= PHP_EOL;
if ($y === self::HEADER_INDEX) {
$output .= $this->getBorderLine();
} else {
if ($this->allBorders) {
$output .= $this->getBorderLine();
}
}
}
if (!$this->allBorders) {
$output .= $this->border ? $this->getBorderLine() : '';
}
if (PHP_SAPI !== 'cli') {
$output = '<pre>'.$output.'</pre>';
}
return $output;
}
/**
* Get the printable border line
* @return string
*/
private function getBorderLine()
{
$output = '';
if (isset($this->data[0])) {
$columnCount = count($this->data[0]);
} elseif (isset($this->data[self::HEADER_INDEX])) {
$columnCount = count($this->data[self::HEADER_INDEX]);
} else {
return $output;
}
for ($col = 0; $col < $columnCount; $col++) {
$output .= $this->getCellOutput($col);
}
if ($this->border) {
$output .= '+';
}
$output .= PHP_EOL;
return $output;
}
/**
* Get the printable cell content
*
* @param integer $index The column index
* @param array $row The table row
* @return string
*/
private function getCellOutput($index, $row = null)
{
$cell = $row ? $row[$index] : '-';
$width = $this->columnWidths[$index];
$padding = str_repeat($row ? ' ' : '-', $this->padding);
$output = '';
if ($index === 0) {
$output .= str_repeat(' ', $this->indent);
}
if ($this->border) {
$output .= $row ? '|' : '+';
}
$output .= $padding; # left padding
if (in_array(trim($cell), ["\x1b[32m✔\x1b[0m", "\x1b[31m✘\x1b[0m", "\x1b[33m?\x1b[0m"])) {
$output .= ' ' . trim($cell) . ' ';
} else {
$output .= $this->strPadUnicode($cell, $width, $row ? ' ' : '-'); # cell content
}
$output .= $padding; # right padding
if ($row && $index == count($row) - 1 && $this->border) {
$output .= $row ? '|' : '+';
}
return $output;
}
/**
* Calculate maximum width of each column
* @return array
*/
private function calculateColumnWidth()
{
foreach ($this->data as $row) {
if (is_array($row)) {
foreach ($row as $x => $col) {
$content = preg_replace('#\x1b[[][^A-Za-z]*[A-Za-z]#', '', $col);
if (!isset($this->columnWidths[$x])) {
$this->columnWidths[$x] = mb_strlen($content, 'UTF-8');
} else {
if (mb_strlen($content, 'UTF-8') > $this->columnWidths[$x]) {
$this->columnWidths[$x] = mb_strlen($content, 'UTF-8');
}
}
}
}
}
return $this->columnWidths;
}
/**
* Multibyte version of str_pad() function
* @source http://php.net/manual/en/function.str-pad.php
*/
private function strPadUnicode($str, $padLength, $padString = ' ', $dir = STR_PAD_RIGHT)
{
$strLen = mb_strlen($str, 'UTF-8');
$padStrLen = mb_strlen($padString, 'UTF-8');
if (!$strLen && ($dir == STR_PAD_RIGHT || $dir == STR_PAD_LEFT)) {
$strLen = 1;
}
if (!$padLength || !$padStrLen || $padLength <= $strLen) {
return $str;
}
$result = null;
$repeat = ceil($strLen - $padStrLen + $padLength);
if ($dir == STR_PAD_RIGHT) {
$result = $str . str_repeat($padString, $repeat);
$result = mb_substr($result, 0, $padLength, 'UTF-8');
} elseif ($dir == STR_PAD_LEFT) {
$result = str_repeat($padString, $repeat) . $str;
$result = mb_substr($result, -$padLength, null, 'UTF-8');
} elseif ($dir == STR_PAD_BOTH) {
$length = ($padLength - $strLen) / 2;
$repeat = ceil($length / $padStrLen);
$result = mb_substr(str_repeat($padString, $repeat), 0, floor($length), 'UTF-8')
. $str
. mb_substr(str_repeat($padString, $repeat), 0, ceil($length), 'UTF-8');
}
return $result;
}
/**
* Set max column count
* @param int $count The column count
*/
private function setMaxColumnCount($count)
{
if ($count > $this->maxColumnCount) {
$this->maxColumnCount = $count;
}
}
}