From e5d6b02d37c8301389feec4169b50a296a37afc3 Mon Sep 17 00:00:00 2001 From: Qiu Yu Date: Wed, 15 Jan 2014 16:51:55 +0800 Subject: Add max-width support for table formatter Change-Id: If6198ede935907396454e72fc76f8ec9c1265eb6 Closes-Bug: #1269299 --- cliff/formatters/table.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/cliff/formatters/table.py b/cliff/formatters/table.py index e625a25..12e1ac6 100644 --- a/cliff/formatters/table.py +++ b/cliff/formatters/table.py @@ -19,7 +19,14 @@ class TableFormatter(ListFormatter, SingleFormatter): pass def add_argument_group(self, parser): - pass + group = parser.add_argument_group('table formatter') + group.add_argument( + '--max-width', + metavar='', + default=0, + type=int, + help='Maximum display width, 0 to disable', + ) def emit_list(self, column_names, data, stdout, parsed_args): x = prettytable.PrettyTable( @@ -27,6 +34,8 @@ class TableFormatter(ListFormatter, SingleFormatter): print_empty=False, ) x.padding_width = 1 + if parsed_args.max_width > 0: + x.max_width = int(parsed_args.max_width) # Figure out the types of the columns in the # first row and set the alignment of the # output accordingly. @@ -52,6 +61,8 @@ class TableFormatter(ListFormatter, SingleFormatter): x = prettytable.PrettyTable(field_names=('Field', 'Value'), print_empty=False) x.padding_width = 1 + if parsed_args.max_width > 0: + x.max_width = int(parsed_args.max_width) # Align all columns left because the values are # not all the same type. x.align['Field'] = 'l' -- cgit v1.2.1