10BC0 [Console] Add title table by maidmaid · Pull Request #26933 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
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
42 changes: 39 additions & 3 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ class Table
private const BORDER_OUTSIDE = 0;
private const BORDER_INSIDE = 1;

private $headerTitle;
private $footerTitle;

/**
* Table headers.
*/
Expand Down Expand Up @@ -290,6 +293,20 @@ public function setRow($column, array $row)
return $this;
}

public function setHeaderTitle(?string $title): self
{
$this->headerTitle = $title;

return $this;
}

public function setFooterTitle(?string $title): self
{
$this->footerTitle = $title;

return $this;
}

/**
* Renders table to output.
*
Expand Down Expand Up @@ -331,15 +348,17 @@ public function render()
}

if ($isHeader || $isFirstRow) {
$this->renderRowSeparator($isFirstRow ? self::SEPARATOR_TOP_BOTTOM : self::SEPARATOR_TOP);
if ($isFirstRow) {
$this->renderRowSeparator(self::SEPARATOR_TOP_BOTTOM);
$isFirstRow = false;
} else {
$this->renderRowSeparator(self::SEPARATOR_TOP, $this->headerTitle, $this->style->getHeaderTitleFormat());
}
}

$this->renderRow($row, $isHeader ? $this->style->getCellHeaderFormat() : $this->style->getCellRowFormat());
}
$this->renderRowSeparator(self::SEPARATOR_BOTTOM);
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());

$this->cleanup();
$this->rendered = true;
Expand All @@ -350,7 +369,7 @@ public function render()
*
* Example: <code>+-----+-----------+-------+</code>
*/
private function renderRowSeparator(int $type = self::SEPARATOR_MID)
private function renderRowSeparator(int $type = self::SEPARATOR_MID, string $title = null, string $titleFormat = null)
{
if (0 === $count = $this->numberOfColumns) {
return;
Expand Down Expand Up @@ -378,6 +397,23 @@ private function renderRowSeparator(int $type = self::SEPARATOR_MID)
$markup .= $column === $count - 1 ? $rightChar : $midChar;
}

if (null !== $title) {
$titleLength = Helper::strlenWithoutDecoration($formatter = $this->output->getFormatter(), $formattedTitle = sprintf($titleFormat, $title));
$markupLength = Helper::strlen($markup);
if ($titleLength > $limit = $markupLength - 4) {
$titleLength = $limit;
$formatLength = Helper::strlenWithoutDecoration($formatter, sprintf($titleFormat, ''));
$formattedTitle = sprintf($titleFormat, Helper::substr($title, 0, $limit - $formatLength - 3).'...');
}

$titleStart = ($markupLength - $titleLength) / 2;
if (false === mb_detect_encoding($markup, null, true)) {
$markup = substr_replace($markup, $formattedTitle, $titleStart, $titleLength);
} else {
$markup = mb_substr($markup, 0, $titleStart).$formattedTitle.mb_substr($markup, $titleStart + $titleLength);
}
}

$this->output->writeln(sprintf($this->style->getBorderFormat(), $markup));
}

Expand Down
26 changes: 26 additions & 0 deletions src/Symfony/Component/Console/Helper/TableStyle.php