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
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ TBD

* Remove dependency on terminaltables
* Add psql_unicode table format
* Add minimal table format

Version 2.1.0
-------------
Expand Down
7 changes: 7 additions & 0 deletions cli_helpers/tabular_output/tabulate_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -65,6 +68,7 @@
"ascii",
"plain",
"simple",
"minimal",
"grid",
"fancy_grid",
"pipe",
Expand All @@ -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):
Expand Down Expand Up @@ -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"))
2 changes: 1 addition & 1 deletion docs/source/quickstart.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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::
Expand Down
15 changes: 15 additions & 0 deletions tests/tabular_output/test_output_formatter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down