From 1fc3d7538ec4a39d03736369323ac41c710bdbfb Mon Sep 17 00:00:00 2001 From: Greg Anderson Date: Sat, 30 Mar 2024 06:35:19 -0700 Subject: [PATCH] Make table headers optional when table is empty (#108) * 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 --------- Co-authored-by: Moshe Weitzman --- src/Formatters/TableFormatter.php | 11 +++++++++++ src/Options/FormatterOptions.php | 6 ++++++ tests/FormattersTest.php | 29 +++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/Formatters/TableFormatter.php b/src/Formatters/TableFormatter.php index b808e92..bdc29c3 100644 --- a/src/Formatters/TableFormatter.php +++ b/src/Formatters/TableFormatter.php @@ -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); @@ -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, ':'); diff --git a/src/Options/FormatterOptions.php b/src/Options/FormatterOptions.php index d47408b..a1c85a4 100644 --- a/src/Options/FormatterOptions.php +++ b/src/Options/FormatterOptions.php @@ -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'; @@ -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); diff --git a/tests/FormattersTest.php b/tests/FormattersTest.php index cff3ee5..5f365ab 100644 --- a/tests/FormattersTest.php +++ b/tests/FormattersTest.php @@ -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 = <<assertFormattedOutputMatches($expected, 'table', $data, $options); + + $expected = <<setTableEmptyMessage("Does not have any headers"); + + $this->assertFormattedOutputMatches($expected, 'table', $data, $options); + } + /** * @test * @return void