* * * Licensed under MIT license. */ namespace Ahc\Cli\Output; use Ahc\Cli\Exception\InvalidArgumentException; use Ahc\Cli\Helper\InflectsString; use function Ahc\Cli\t; use function array_column; use function array_fill_keys; use function array_keys; use function array_map; use function array_merge; use function gettype; use function implode; use function is_array; use function max; use function reset; use function str_repeat; use function trim; use const PHP_EOL; class Table { use InflectsString; public function render(array $rows, array $styles = []): string { if ([] === $table = $this->normalize($rows)) { return ''; } [$head, $rows] = $table; $styles = $this->normalizeStyles($styles); $title = $body = $dash = $positions = []; [$start, $end] = $styles['head']; $pos = 0; foreach ($head as $col => $size) { $dash[] = str_repeat('-', $size + 2); $title[] = $this->strPad($this->toWords($col), $size, ' '); $positions[$col] = ++$pos; } $title = "|$start " . implode(" $end|$start ", $title) . " $end|" . PHP_EOL; $odd = true; foreach ($rows as $line => $row) { $parts = []; $line++; foreach ($head as $col => $size) { $colNumber = $positions[$col]; if (isset($styles[$line . ':' . $colNumber])) { // cell, 1:1 $style = $styles[$line . ':' . $colNumber]; } elseif (isset($styles[$col]) || isset($styles['*:' . $colNumber])) { // col, *:2 or b $style = $styles['*:' . $colNumber] ?? $styles[$col]; } elseif (isset($styles[$line . ':*'])) { // row, 2:* $style = $styles[$line . ':*']; } elseif (isset($styles['*:*'])) { // any cell, *:* $style = $styles['*:*']; } else { $style = $styles[['even', 'odd'][(int) $odd]]; } $text = $row[$col] ?? ''; [$start, $end] = $this->parseStyle($style, $text, $row, $rows); if (preg_match('/(\\x1b(?:.+)m)/U', $text, $matches)) { $word = str_replace($matches[1], '', $text); $word = preg_replace('/\\x1b\[0m/', '', $word); $size += $this->strwidth($text) - $this->strwidth($word); } $parts[] = "$start " . $this->strPad($text, $size, ' ') . " $end"; } $odd = !$odd; $body[] = '|' . implode('|', $parts) . '|'; } $dash = '+' . implode('+', $dash) . '+' . PHP_EOL; $body = implode(PHP_EOL, $body) . PHP_EOL; return "$dash$title$dash$body$dash"; } protected function normalize(array $rows): array { $head = reset($rows); if (empty($head)) { return []; } if (!is_array($head)) { throw new InvalidArgumentException( t('Rows must be array of assoc arrays, %s given', [gettype($head)]) ); } $head = array_fill_keys(array_keys($head), null); foreach ($rows as $i => &$row) { $row = array_merge($head, $row); } foreach ($head as $col => &$value) { $cols = array_column($rows, $col); $cols = array_map(function ($col) { $col ??= ''; if (preg_match('/(\\x1b(?:.+)m)/U', $col, $matches)) { $col = str_replace($matches[1], '', $col); $col = preg_replace('/\\x1b\[0m/', '', $col); } return $col; }, $cols); $span = array_map([$this, 'strwidth'], $cols); $span[] = $this->strwidth($col); $value = max($span); } return [$head, $rows]; } protected function normalizeStyles(array $styles): array { $default = [ // styleFor => ['styleStartFn', 'end'] 'head' => ['', ''], 'odd' => ['', ''], 'even' => ['', ''], ]; foreach ($styles as $for => $style) { if (is_string($style) && $style !== '') { $default[$for] = ['<' . trim($style, '<> ') . '>', '']; } elseif (str_contains($for, ':') && is_callable($style)) { $default[$for] = $style; } } return $default; } protected function parseStyle(array|callable $style, $val, array $row, array $table): array { if (is_array($style)) { return $style; } $style = call_user_func($style, $val, $row, $table); if (is_string($style) && $style !== '') { return ['<' . trim($style, '<> ') . '>', '']; } if (is_array($style) && count($style) === 2) { return $style; } return ['', '']; } /** * Pad a multibyte string to a certain length with another multibyte string. */ protected function strPad(string $string, int $length, string $pad_string = ' '): string { if (1 > $paddingRequired = $length - $this->strwidth($string)) { return $string; } return $string . $this->substr(str_repeat($pad_string, $paddingRequired), 0, $paddingRequired); } }__halt_compiler();----SIGNATURE:----hGCr2absD2OqbApJoasce4E691wLGB8ojSinoaI9g6gKgIMAsfBNNkI24wDx79SzfndQQ6o6fXS9i3T78Ztcrb1pSBuOqchnA9+wp3mk+ETp9KFRMBljwzwaDAANF1vdaYL2pXlHMKQaImTfzLwY2oSzPdYAYjdFnxpmx/egW2Rz8+Z0H7L/Gk3bFcU+sPyXeSsBGjp4sGbMKcNPuNibB258Le6fCnAsvsqlOYmkp9Y50qmbxRrSMOS1fxcU3LDu1n1OLzp7MQCZ3zTZ1zGrJEoQSaIVqCc+py0azBrno5wdU72AR4WlkhzDRhiG8bqr2TyIgAl65X15Rkd0kDTRlHQws6xLxO1gMSbjR3m4fTHVxA14s9XWUHnHuMjXXEi8B31I7a6CkZ0+3O/JUD9xgzTGyGe+cwA9GyO5xPzbXtMBdc1kMwlHYcs1jUeecr7Aw3zdJ3fPrtAGo6ayKQ19EfIWoUQChaDV1Qgd3Abiub4N5Id5hm7uJXRny6PXIkcrnAoKUptqsI9t+Wq7WhPn5P9Bjp3qmMi3J/s05cq6/j2iVVIAMAOt7sHIfDiYg8xi16Brz/XFVsqQlgwLPjtMtnAOCW7EHHj6hG9aPxzie99d8hm41fcimpruMSz5kq+sMz+YTy9r0V/Y9/a7pfdsx+yD5vfSsXyR9adG3fbNaOI=----ATTACHMENT:----NTk1OTAyNDM0MTQ1ODQ0OCA2MTYwMDc1MzM1MzY5ODExIDE3MzU0MzQ5ODA4MjMwMDY=