Skip to content

Commit 2a7d379

Browse files
authored
Merge pull request #74 from dbcli/pasenor/minimal-format
Add 'minimal' format: the same as 'plain', but without column headers.
2 parents 42159f4 + d109e5b commit 2a7d379

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ TBD
66

77
* Remove dependency on terminaltables
88
* Add psql_unicode table format
9+
* Add minimal table format
910

1011
Version 2.1.0
1112
-------------

cli_helpers/tabular_output/tabulate_adapter.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@
5252
with_header_hide=None,
5353
)
5454

55+
# "minimal" is the same as "plain", but without headers
56+
tabulate._table_formats["minimal"] = tabulate._table_formats["plain"]
57+
5558
supported_markup_formats = (
5659
"mediawiki",
5760
"html",
@@ -65,6 +68,7 @@
6568
"ascii",
6669
"plain",
6770
"simple",
71+
"minimal",
6872
"grid",
6973
"fancy_grid",
7074
"pipe",
@@ -79,6 +83,7 @@
7983
supported_formats = supported_markup_formats + supported_table_formats
8084

8185
default_kwargs = {"ascii": {"numalign": "left"}}
86+
headless_formats = ("minimal",)
8287

8388

8489
def get_preprocessors(format_name):
@@ -182,4 +187,6 @@ def adapter(data, headers, table_format=None, preserve_whitespace=False, **kwarg
182187
tabulate.PRESERVE_WHITESPACE = preserve_whitespace
183188

184189
tkwargs.update(default_kwargs.get(table_format, {}))
190+
if table_format in headless_formats:
191+
headers = []
185192
return iter(tabulate.tabulate(data, headers, **tkwargs).split("\n"))

docs/source/quickstart.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ Let's get a list of all the supported format names::
5050
>>> from cli_helpers.tabular_output import TabularOutputFormatter
5151
>>> formatter = TabularOutputFormatter()
5252
>>> formatter.supported_formats
53-
('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')
53+
('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')
5454

5555
You can format your data in any of those supported formats. Let's take the
5656
same data from our first example and put it in the ``fancy_grid`` format::

tests/tabular_output/test_output_formatter.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,21 @@ def test_format_name_attribute():
114114
formatter.format_name = "foobar"
115115

116116

117+
def test_headless_tabulate_format():
118+
"""Test that a headless formatter doesn't display headers"""
119+
formatter = TabularOutputFormatter(format_name="minimal")
120+
headers = ["text", "numeric"]
121+
data = [["a"], ["b"], ["c"]]
122+
expected = "a\nb\nc"
123+
assert expected == "\n".join(
124+
TabularOutputFormatter().format_output(
125+
iter(data),
126+
headers,
127+
format_name="minimal",
128+
)
129+
)
130+
131+
117132
def test_unsupported_format():
118133
"""Test that TabularOutputFormatter rejects unknown formats."""
119134
formatter = TabularOutputFormatter()

0 commit comments

Comments
 (0)