summaryrefslogtreecommitdiff
path: root/cliff
diff options
context:
space:
mode:
authorJenkins <jenkins@review.openstack.org>2017-07-05 19:03:13 +0000
committerGerrit Code Review <review@openstack.org>2017-07-05 19:03:13 +0000
commit0c378ba96fbbf7475a9e440417d018b42734c4d9 (patch)
tree540c43ba9dbfb34b5084d05a773e3ca8cb54c0a8 /cliff
parentccd2e048c6d2b80e129f2295a73b5ad32103e47c (diff)
parent180d7ce3ce7487fc9a071bcfe18d7aff76695750 (diff)
downloadcliff-0c378ba96fbbf7475a9e440417d018b42734c4d9.tar.gz
Merge "add --fit-width option to table formatter"
Diffstat (limited to 'cliff')
-rw-r--r--cliff/formatters/table.py20
-rw-r--r--cliff/tests/test_formatters_table.py32
2 files changed, 40 insertions, 12 deletions
diff --git a/cliff/formatters/table.py b/cliff/formatters/table.py
index df4616b..22f7300 100644
--- a/cliff/formatters/table.py
+++ b/cliff/formatters/table.py
@@ -57,6 +57,15 @@ class TableFormatter(base.ListFormatter, base.SingleFormatter):
'but the parameter takes precedence.'),
)
group.add_argument(
+ '--fit-width',
+ action='store_true',
+ default=bool(int(os.environ.get('CLIFF_FIT_WIDTH', 0))),
+ help=('Fit the table to the display width. '
+ 'Implied if --max-width greater than 0. '
+ 'Set the environment variable CLIFF_FIT_WIDTH=1 '
+ 'to always enable'),
+ )
+ group.add_argument(
'--print-empty',
action='store_true',
help='Print empty table if there is no data to show.',
@@ -96,7 +105,8 @@ class TableFormatter(base.ListFormatter, base.SingleFormatter):
# preference to wrapping columns smaller than 8 characters.
min_width = 8
self._assign_max_widths(
- stdout, x, int(parsed_args.max_width), min_width)
+ stdout, x, int(parsed_args.max_width), min_width,
+ parsed_args.fit_width)
formatted = x.get_string()
stdout.write(formatted)
@@ -120,7 +130,8 @@ class TableFormatter(base.ListFormatter, base.SingleFormatter):
# the Field column readable.
min_width = 16
self._assign_max_widths(
- stdout, x, int(parsed_args.max_width), min_width)
+ stdout, x, int(parsed_args.max_width), min_width,
+ parsed_args.fit_width)
formatted = x.get_string()
stdout.write(formatted)
@@ -164,12 +175,15 @@ class TableFormatter(base.ListFormatter, base.SingleFormatter):
return shrink_fields, shrink_remaining
@staticmethod
- def _assign_max_widths(stdout, x, max_width, min_width=0):
+ def _assign_max_widths(stdout, x, max_width, min_width=0, fit_width=False):
if min_width:
x.min_width = min_width
if max_width > 0:
term_width = max_width
+ elif not fit_width:
+ # Fitting is disabled
+ return
else:
term_width = utils.terminal_width(stdout)
if not term_width:
diff --git a/cliff/tests/test_formatters_table.py b/cliff/tests/test_formatters_table.py
index e1b2f09..d536cac 100644
--- a/cliff/tests/test_formatters_table.py
+++ b/cliff/tests/test_formatters_table.py
@@ -25,7 +25,8 @@ from cliff.tests import test_columns
class args(object):
- def __init__(self, max_width=0, print_empty=False):
+ def __init__(self, max_width=0, print_empty=False, fit_width=False):
+ self.fit_width = fit_width
if max_width > 0:
self.max_width = max_width
else:
@@ -133,7 +134,7 @@ class TestTerminalWidth(base.TestBase):
d = ('A', 'B', 'C', 'd' * 77)
self.assertEqual(
self.expected_ml_80_val,
- _table_tester_helper(c, d, extra_args=args()),
+ _table_tester_helper(c, d, extra_args=args(fit_width=True)),
)
@mock.patch('cliff.utils.terminal_width')
@@ -223,7 +224,10 @@ class TestMaxWidth(base.TestBase):
| | field |
+--------------------------+-----------------------------------------+
''')
- self.assertEqual(expected, _table_tester_helper(c, d))
+ self.assertEqual(
+ expected,
+ _table_tester_helper(c, d, extra_args=['--fit-width']),
+ )
@mock.patch('cliff.utils.terminal_width')
def test_50(self, tw):
@@ -240,7 +244,10 @@ class TestMaxWidth(base.TestBase):
| ame | longer than the field |
+-----------------------+------------------------+
''')
- self.assertEqual(expected, _table_tester_helper(c, d))
+ self.assertEqual(
+ expected,
+ _table_tester_helper(c, d, extra_args=['--fit-width']),
+ )
@mock.patch('cliff.utils.terminal_width')
def test_10(self, tw):
@@ -259,7 +266,10 @@ class TestMaxWidth(base.TestBase):
| | field |
+------------------+------------------+
''')
- self.assertEqual(expected, _table_tester_helper(c, d))
+ self.assertEqual(
+ expected,
+ _table_tester_helper(c, d, extra_args=['--fit-width']),
+ )
class TestListFormatter(base.TestBase):
@@ -390,7 +400,8 @@ class TestListFormatter(base.TestBase):
def test_max_width_50(self, tw):
# resize 1 column
l = tw.return_value = 50
- actual = _table_tester_helper(self._col_names, self._col_data)
+ actual = _table_tester_helper(self._col_names, self._col_data,
+ extra_args=['--fit-width'])
self.assertEqual(self._expected_mv[l], actual)
self.assertEqual(l, len(actual.splitlines()[0]))
@@ -398,7 +409,8 @@ class TestListFormatter(base.TestBase):
def test_max_width_45(self, tw):
# resize 2 columns
l = tw.return_value = 45
- actual = _table_tester_helper(self._col_names, self._col_data)
+ actual = _table_tester_helper(self._col_names, self._col_data,
+ extra_args=['--fit-width'])
self.assertEqual(self._expected_mv[l], actual)
self.assertEqual(l, len(actual.splitlines()[0]))
@@ -406,7 +418,8 @@ class TestListFormatter(base.TestBase):
def test_max_width_40(self, tw):
# resize all columns
l = tw.return_value = 40
- actual = _table_tester_helper(self._col_names, self._col_data)
+ actual = _table_tester_helper(self._col_names, self._col_data,
+ extra_args=['--fit-width'])
self.assertEqual(self._expected_mv[l], actual)
self.assertEqual(l, len(actual.splitlines()[0]))
@@ -414,7 +427,8 @@ class TestListFormatter(base.TestBase):
def test_max_width_10(self, tw):
# resize all columns limited by min_width=8
l = tw.return_value = 10
- actual = _table_tester_helper(self._col_names, self._col_data)
+ actual = _table_tester_helper(self._col_names, self._col_data,
+ extra_args=['--fit-width'])
self.assertEqual(self._expected_mv[l], actual)
# 3 columns each 8 wide, plus table spacing and borders
expected_width = 11 * 3 + 1