Skip to content

Commit

Permalink
Make table headers optional when table is empty (#108)
Browse files Browse the repository at this point in the history
* Add TABLE_EMPTY_MESSAGE formatter option to allow caller to control whether or not table headers should be emitted when the table is empty

* Oh, Windows.

* Update tests/FormattersTest.php

Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>

---------

Co-authored-by: Moshe Weitzman <weitzman@tejasa.com>
  • Loading branch information
greg-1-anderson and weitzman authored Mar 30, 2024
1 parent 0671156 commit 1fc3d75
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Formatters/TableFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public function write(OutputInterface $output, $tableTransformer, FormatterOptio
$defaults = [
FormatterOptions::TABLE_STYLE => 'consolidation',
FormatterOptions::INCLUDE_FIELD_LABELS => true,
FormatterOptions::TABLE_EMPTY_MESSAGE => false,
];

$table = new Table($output);
Expand All @@ -83,10 +84,20 @@ public function write(OutputInterface $output, $tableTransformer, FormatterOptio
$isList = $tableTransformer->isList();
$includeHeaders = $options->get(FormatterOptions::INCLUDE_FIELD_LABELS, $defaults);
$listDelimiter = $options->get(FormatterOptions::LIST_DELIMITER, $defaults);
$emptyMessage = $options->get(FormatterOptions::TABLE_EMPTY_MESSAGE, $defaults);

$headers = $tableTransformer->getHeaders();
$data = $tableTransformer->getTableData($includeHeaders && $isList);

// Do not print table headers for an empty table if the
// TABLE_EMPTY_MESSAGE has been set.
if (empty($data) && ($emptyMessage !== false)) {
if (!empty($emptyMessage)) {
$output->writeln($emptyMessage);
}
return;
}

if ($listDelimiter) {
if (!empty($headers)) {
array_splice($headers, 1, 0, ':');
Expand Down
6 changes: 6 additions & 0 deletions src/Options/FormatterOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class FormatterOptions
const FORMAT = 'format';
const DEFAULT_FORMAT = 'default-format';
const TABLE_STYLE = 'table-style';
const TABLE_EMPTY_MESSAGE = 'table-empty-message';
const LIST_ORIENTATION = 'list-orientation';
const FIELDS = 'fields';
const FIELD = 'field';
Expand Down Expand Up @@ -137,6 +138,11 @@ public function setFieldLabels($fieldLabels)
return $this->setConfigurationValue(self::FIELD_LABELS, $fieldLabels);
}

public function setTableEmptyMessage($emptyMessage)
{
return $this->setConfigurationValue(FormatterOptions::TABLE_EMPTY_MESSAGE, $emptyMessage);
}

public function setDefaultStringField($defaultStringField)
{
return $this->setConfigurationValue(self::DEFAULT_STRING_FIELD, $defaultStringField);
Expand Down
29 changes: 29 additions & 0 deletions tests/FormattersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -934,6 +934,35 @@ function testTableWithMissingCell()
$this->assertFormattedOutputMatches($expectedTsvWithHeaders, 'tsv', $data, new FormatterOptions(), ['include-field-labels' => true]);
}

function testEmptyTable()
{
if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
$this->markTestSkipped("Once again we are having spurious failures on this test under Windows. In theory it should work.");
}

$options = new FormatterOptions();
$options->setWidth(42);
$options->setFieldLabels(['first' => 'First', 'second' => 'Second']);

$data = new RowsOfFields([]);

$expected = <<<EOT
------- --------
First Second
------- --------
EOT;

$this->assertFormattedOutputMatches($expected, 'table', $data, $options);

$expected = <<<EOT
Does not have any headers
EOT;

$options->setTableEmptyMessage("Does not have any headers");

$this->assertFormattedOutputMatches($expected, 'table', $data, $options);
}

/**
* @test
* @return void
Expand Down

0 comments on commit 1fc3d75

Please sign in to comment.