"""Output formatters using prettytable.
"""

import prettytable

from .base import ListFormatter


class TableLister(ListFormatter):

    ALIGNMENTS = {
        int: 'r',
        str: 'l',
        unicode: 'l',
        float: 'r',
        }

    def add_argument_group(self, parser):
        group = parser.add_argument_group('Table Formatter')
        group.add_argument(
            '-c', '--column',
            action='append',
            default=[],
            dest='columns',
            help='specify the column(s) to include, can be repeated',
            )

    def emit_list(self, column_names, data, stdout, parsed_args):
        x = prettytable.PrettyTable(column_names)
        x.set_padding_width(1)
        # Figure out the types of the columns in the
        # first row and set the alignment of the
        # output accordingly.
        data_iter = iter(data)
        first_row = next(data_iter)
        for value, name in zip(first_row, column_names):
            alignment = self.ALIGNMENTS.get(type(value), 'l')
            x.set_field_align(name, alignment)
        # Now iterate over the data and add the rows.
        x.add_row(first_row)
        for row in data_iter:
            x.add_row(row)
        formatted = x.get_string(fields=(parsed_args.columns or column_names))
        stdout.write(formatted)
        stdout.write('\n')
        return

@@ -872,16 +872,33 @@
 r(data)%0A
+        try:%0A    
         
@@ -925,16 +925,81 @@
 a_iter)%0A
+        except StopIteration:%0A            pass%0A        else:%0A    
         
@@ -1043,24 +1043,28 @@
 umn_names):%0A
+    
             
@@ -1121,24 +1121,28 @@
             
+ 
+   
 x.set_field_
@@ -1172,16 +1172,20 @@
         
+   
+ 
 # Now it
@@ -1222,32 +1222,36 @@
 e rows.%0A        
+    
 x.add_row(first_
@@ -1259,24 +1259,28 @@
 ow)%0A        
+  
+  
 for row in d
@@ -1289,16 +1289,20 @@
 a_iter:%0A
+    
         

handle an empty data set