Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/View/TableView.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public function show(array $data): void
$headers = $this->headers($data);
$widths = $this->columnWidths($headers, $data);

$this->writeLine($this->separator($widths));
$this->writeHeaderLine($widths);
$this->writeLine($this->formatRow($headers, $widths));
$this->writeLine($this->separator($widths));

Expand All @@ -33,7 +33,7 @@ public function show(array $data): void
$this->writeLine($this->formatRow(array_values($row), $widths));
}

$this->writeLine($this->separator($widths));
$this->writeFooterLine($widths);
}

protected function headers(array $data): array
Expand All @@ -54,11 +54,25 @@ protected function columnWidths(array $headers, array $data): array
return $widths;
}

protected function separator(array $widths): string
protected function writeHeaderLine(array $widths): void
{
$this->writeLine(
$this->separator($widths, '┌', '┬', '┐')
);
}

protected function writeFooterLine(array $widths): void
{
$this->writeLine(
$this->separator($widths, '└', '┴', '┘')
);
}

protected function separator(array $widths, string $start = '├', string $divider = '┼', string $end = '┤'): string
{
$parts = array_map(fn (int $w) => str_repeat('-', $w + 2), $widths);
$parts = array_map(static fn (int $w) => str_repeat('', $w + 2), $widths);

return '+' . implode('+', $parts) . '+';
return $start . implode($divider, $parts) . $end;
}

protected function formatRow(array $values, array $widths): string
Expand All @@ -69,6 +83,6 @@ protected function formatRow(array $values, array $widths): string
$cells[] = ' ' . mb_str_pad((string) $value, $widths[$i]) . ' ';
}

return '|' . implode('|', $cells) . '|';
return '' . implode('', $cells) . '';
}
}
Loading