Skip to content

[Console] Add markdown format to Table #59657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 14, 2025
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
[Console] Add markdown format to Table
  • Loading branch information
amenk committed Feb 6, 2025
commit 034b574253b748fa6f6629d63e1027d50a7f11d1
1 change: 1 addition & 0 deletions src/Symfony/Component/Console/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ CHANGELOG
* Deprecate not declaring the parameter type in callable commands defined through `setCode` method
* Add support for help definition via `AsCommand` attribute
* Deprecate methods `Command::getDefaultName()` and `Command::getDefaultDescription()` in favor of the `#[AsCommand]` attribute
* Add support for Markdown format in `Table`

7.2
---
Expand Down
14 changes: 12 additions & 2 deletions src/Symfony/Component/Console/Helper/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ public function render(): void
continue;
}

if ($isHeader && !$isHeaderSeparatorRendered) {
if ($isHeader && !$isHeaderSeparatorRendered && $this->style->displayOutsideBorder()) {
$this->renderRowSeparator(
self::SEPARATOR_TOP,
$hasTitle ? $this->headerTitle : null,
Expand Down Expand Up @@ -449,7 +449,10 @@ public function render(): void
}
}
}
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());

if ($this->getStyle()->displayOutsideBorder()) {
$this->renderRowSeparator(self::SEPARATOR_BOTTOM, $this->footerTitle, $this->style->getFooterTitleFormat());
}

$this->cleanup();
$this->rendered = true;
Expand Down Expand Up @@ -868,6 +871,12 @@ private function cleanup(): void
*/
private static function initStyles(): array
{
$markdown = new TableStyle();
$markdown
->setDefaultCrossingChar('|')
->setDisplayOutsideBorder(false)
;

$borderless = new TableStyle();
$borderless
->setHorizontalBorderChars('=')
Expand Down Expand Up @@ -905,6 +914,7 @@ private static function initStyles(): array

return [
'default' => new TableStyle(),
'markdown' => $markdown,
'borderless' => $borderless,
'compact' => $compact,
'symfony-style-guide' => $styleGuide,
Expand Down
13 changes: 13 additions & 0 deletions src/Symfony/Component/Console/Helper/TableStyle.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TableStyle
private string $cellRowFormat = '%s';
private string $cellRowContentFormat = ' %s ';
private string $borderFormat = '%s';
private bool $displayOutsideBorder = true;
private int $padType = \STR_PAD_RIGHT;

/**
Expand Down Expand Up @@ -359,4 +360,16 @@ public function setFooterTitleFormat(string $format): static

return $this;
}

public function setDisplayOutsideBorder($displayOutSideBorder): static
{
$this->displayOutsideBorder = $displayOutSideBorder;

return $this;
}

public function displayOutsideBorder(): bool
{
return $this->displayOutsideBorder;
}
}
14 changes: 14 additions & 0 deletions src/Symfony/Component/Console/Tests/Helper/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,20 @@ public static function renderProvider()
| 80-902734-1-6 | And Then There Were None | Agatha Christie |
+---------------+--------------------------+------------------+

TABLE,
],
[
['ISBN', 'Title', 'Author'],
$books,
'markdown',
<<<'TABLE'
| ISBN | Title | Author |
|---------------|--------------------------|------------------|
| 99921-58-10-7 | Divine Comedy | Dante Alighieri |
| 9971-5-0210-0 | A Tale of Two Cities | Charles Dickens |
| 960-425-059-0 | The Lord of the Rings | J. R. R. Tolkien |
| 80-902734-1-6 | And Then There Were None | Agatha Christie |

TABLE,
],
[
Expand Down
Loading