|
2 | 2 |
|
3 | 3 | namespace Ahc\Cli\Output; |
4 | 4 |
|
| 5 | +use Ahc\Cli\Exception\InvalidArgumentException; |
| 6 | +use Ahc\Cli\Helper\InflectsString; |
| 7 | + |
5 | 8 | /** |
6 | 9 | * Cli Writer. |
7 | 10 | * |
|
149 | 152 | */ |
150 | 153 | class Writer |
151 | 154 | { |
| 155 | + use InflectsString; |
| 156 | + |
152 | 157 | /** @var resource Output file handle */ |
153 | 158 | protected $stream; |
154 | 159 |
|
@@ -267,6 +272,98 @@ public function raw($text, bool $error = false): self |
267 | 272 | return $this->doWrite((string) $text, $error); |
268 | 273 | } |
269 | 274 |
|
| 275 | + /** |
| 276 | + * Generate table for the console. Keys of first row are taken as header. |
| 277 | + * |
| 278 | + * @param array[] $rows Array of assoc arrays. |
| 279 | + * @param array $styles Eg: ['head' => 'bold', 'odd' => 'comment', 'even' => 'green'] |
| 280 | + * |
| 281 | + * @return self |
| 282 | + */ |
| 283 | + public function table(array $rows, array $styles = []): self |
| 284 | + { |
| 285 | + if ([] === $table = $this->normalizeTable($rows)) { |
| 286 | + return $this; |
| 287 | + } |
| 288 | + |
| 289 | + list($head, $rows) = $table; |
| 290 | + |
| 291 | + $styles = $this->normalizeStyles($styles); |
| 292 | + $title = $body = $dash = []; |
| 293 | + |
| 294 | + list($start, $end) = $styles['head']; |
| 295 | + foreach ($head as $col => $size) { |
| 296 | + $dash[] = \str_repeat('-', $size + 2); |
| 297 | + $title[] = \str_pad($this->toWords($col), $size, ' '); |
| 298 | + } |
| 299 | + |
| 300 | + $title = "|$start " . \implode(" $end|$start ", $title) . " $end|" . \PHP_EOL; |
| 301 | + |
| 302 | + $odd = true; |
| 303 | + foreach ($rows as $row) { |
| 304 | + $parts = []; |
| 305 | + |
| 306 | + list($start, $end) = $styles[['even', 'odd'][(int) $odd]]; |
| 307 | + foreach ($head as $col => $size) { |
| 308 | + $parts[] = \str_pad($row[$col] ?? '', $size, ' '); |
| 309 | + } |
| 310 | + |
| 311 | + $odd = !$odd; |
| 312 | + $body[] = "|$start " . \implode(" $end|$start ", $parts) . " $end|"; |
| 313 | + } |
| 314 | + |
| 315 | + $dash = '+' . \implode('+', $dash) . '+' . \PHP_EOL; |
| 316 | + $body = \implode(\PHP_EOL, $body) . \PHP_EOL; |
| 317 | + |
| 318 | + return $this->colors("$dash$title$dash$body$dash"); |
| 319 | + } |
| 320 | + |
| 321 | + protected function normalizeTable(array $rows): array |
| 322 | + { |
| 323 | + $head = \reset($rows); |
| 324 | + if (empty($head)) { |
| 325 | + return []; |
| 326 | + } |
| 327 | + |
| 328 | + if (!\is_array($head)) { |
| 329 | + throw new InvalidArgumentException( |
| 330 | + \sprintf('Rows must be array of assoc arrays, %s given', \gettype($head)) |
| 331 | + ); |
| 332 | + } |
| 333 | + |
| 334 | + $head = \array_fill_keys(\array_keys($head), null); |
| 335 | + foreach ($rows as $i => &$row) { |
| 336 | + $row = \array_merge($head, $row); |
| 337 | + } |
| 338 | + |
| 339 | + foreach ($head as $col => &$value) { |
| 340 | + $cols = \array_column($rows, $col); |
| 341 | + $span = \array_map('strlen', $cols); |
| 342 | + $span[] = \strlen($col); |
| 343 | + $value = \max($span); |
| 344 | + } |
| 345 | + |
| 346 | + return [$head, $rows]; |
| 347 | + } |
| 348 | + |
| 349 | + protected function normalizeStyles(array $styles) |
| 350 | + { |
| 351 | + $default = [ |
| 352 | + // styleFor => ['styleStartFn', 'end'] |
| 353 | + 'head' => ['', ''], |
| 354 | + 'odd' => ['', ''], |
| 355 | + 'even' => ['', ''], |
| 356 | + ]; |
| 357 | + |
| 358 | + foreach ($styles as $for => $style) { |
| 359 | + if (isset($default[$for])) { |
| 360 | + $default[$for] = ['<' . \trim($style, '<> ') . '>', '</end>']; |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + return $default; |
| 365 | + } |
| 366 | + |
270 | 367 | /** |
271 | 368 | * Write to stdout or stderr magically. |
272 | 369 | * |
|
0 commit comments