summaryrefslogtreecommitdiff
path: root/cliff/formatters/table.py
diff options
context:
space:
mode:
authorDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-27 19:56:45 -0400
committerDoug Hellmann <doug.hellmann@dreamhost.com>2012-04-27 19:56:45 -0400
commit556495e530c9cb2dc67300d1f199780e247921dc (patch)
treee7c63773f3001742dce82082efb13a52939326b5 /cliff/formatters/table.py
parentb8f3ad548d02eff5fe1b3c8d8515fab9db888204 (diff)
downloadcliff-556495e530c9cb2dc67300d1f199780e247921dc.tar.gz
add ShowOne base class for commands that need to show properties of an individual object
make the table formatter work as a single object formatter update the docs for the new features
Diffstat (limited to 'cliff/formatters/table.py')
-rw-r--r--cliff/formatters/table.py20
1 files changed, 18 insertions, 2 deletions
diff --git a/cliff/formatters/table.py b/cliff/formatters/table.py
index 43066b4..21960a8 100644
--- a/cliff/formatters/table.py
+++ b/cliff/formatters/table.py
@@ -3,10 +3,10 @@
import prettytable
-from .base import ListFormatter
+from .base import ListFormatter, SingleFormatter
-class TableLister(ListFormatter):
+class TableFormatter(ListFormatter, SingleFormatter):
ALIGNMENTS = {
int: 'r',
@@ -48,3 +48,19 @@ class TableLister(ListFormatter):
stdout.write(formatted)
stdout.write('\n')
return
+
+ def emit_one(self, column_names, data, stdout, parsed_args):
+ x = prettytable.PrettyTable(('Field', 'Value'))
+ x.set_padding_width(1)
+ # Align all columns left because the values are
+ # not all the same type.
+ x.set_field_align('Field', 'l')
+ x.set_field_align('Value', 'l')
+ desired_columns = parsed_args.columns
+ for name, value in zip(column_names, data):
+ if name in desired_columns or not desired_columns:
+ x.add_row((name, value))
+ formatted = x.get_string(fields=('Field', 'Value'))
+ stdout.write(formatted)
+ stdout.write('\n')
+ return