diff --git a/CHANGELOG b/CHANGELOG index f3d0c71..37705e3 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -6,6 +6,7 @@ TBD * Remove dependency on terminaltables * Add psql_unicode table format +* Add minimal table format Version 2.1.0 ------------- diff --git a/cli_helpers/tabular_output/tabulate_adapter.py b/cli_helpers/tabular_output/tabulate_adapter.py index 565b5a9..92e6f1d 100644 --- a/cli_helpers/tabular_output/tabulate_adapter.py +++ b/cli_helpers/tabular_output/tabulate_adapter.py @@ -52,6 +52,9 @@ with_header_hide=None, ) +# "minimal" is the same as "plain", but without headers +tabulate._table_formats["minimal"] = tabulate._table_formats["plain"] + supported_markup_formats = ( "mediawiki", "html", @@ -65,6 +68,7 @@ "ascii", "plain", "simple", + "minimal", "grid", "fancy_grid", "pipe", @@ -79,6 +83,7 @@ supported_formats = supported_markup_formats + supported_table_formats default_kwargs = {"ascii": {"numalign": "left"}} +headless_formats = ("minimal",) def get_preprocessors(format_name): @@ -182,4 +187,6 @@ def adapter(data, headers, table_format=None, preserve_whitespace=False, **kwarg tabulate.PRESERVE_WHITESPACE = preserve_whitespace tkwargs.update(default_kwargs.get(table_format, {})) + if table_format in headless_formats: + headers = [] return iter(tabulate.tabulate(data, headers, **tkwargs).split("\n")) diff --git a/docs/source/quickstart.rst b/docs/source/quickstart.rst index 77827df..319655d 100644 --- a/docs/source/quickstart.rst +++ b/docs/source/quickstart.rst @@ -50,7 +50,7 @@ Let's get a list of all the supported format names:: >>> from cli_helpers.tabular_output import TabularOutputFormatter >>> formatter = TabularOutputFormatter() >>> formatter.supported_formats - ('vertical', 'csv', 'tsv', 'mediawiki', 'html', 'latex', 'latex_booktabs', 'textile', 'moinmoin', 'jira', 'plain', 'simple', 'grid', 'fancy_grid', 'pipe', 'orgtbl', 'psql', 'psql_unicode', 'rst', 'ascii', 'double', 'github') + ('vertical', 'csv', 'tsv', 'mediawiki', 'html', 'latex', 'latex_booktabs', 'textile', 'moinmoin', 'jira', 'plain', 'minimal', 'simple', 'grid', 'fancy_grid', 'pipe', 'orgtbl', 'psql', 'psql_unicode', 'rst', 'ascii', 'double', 'github') You can format your data in any of those supported formats. Let's take the same data from our first example and put it in the ``fancy_grid`` format:: diff --git a/tests/tabular_output/test_output_formatter.py b/tests/tabular_output/test_output_formatter.py index 77d0afb..d064427 100644 --- a/tests/tabular_output/test_output_formatter.py +++ b/tests/tabular_output/test_output_formatter.py @@ -114,6 +114,21 @@ def test_format_name_attribute(): formatter.format_name = "foobar" +def test_headless_tabulate_format(): + """Test that a headless formatter doesn't display headers""" + formatter = TabularOutputFormatter(format_name="minimal") + headers = ["text", "numeric"] + data = [["a"], ["b"], ["c"]] + expected = "a\nb\nc" + assert expected == "\n".join( + TabularOutputFormatter().format_output( + iter(data), + headers, + format_name="minimal", + ) + ) + + def test_unsupported_format(): """Test that TabularOutputFormatter rejects unknown formats.""" formatter = TabularOutputFormatter()