From 115a6e9dd106ac391934e9a57bb81dfcb8227c00 Mon Sep 17 00:00:00 2001 From: Jarrod Johnson Date: Tue, 30 Oct 2018 16:23:34 -0400 Subject: [PATCH] Add helper for tabular output Provide a quick class for auto-sizing tabular output. --- confluent_client/confluent/client.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/confluent_client/confluent/client.py b/confluent_client/confluent/client.py index bc5cd4fe..d5ed1035 100644 --- a/confluent_client/confluent/client.py +++ b/confluent_client/confluent/client.py @@ -35,6 +35,30 @@ _attraliases = { } +class Tabulator(object): + def __init__(self, headers): + self.headers = headers + self.rows = [] + + def add_row(self, row): + self.rows.append(row) + + def get_table(self): + i = 0 + fmtstr = '' + for head in self.headers: + neededlen = len(head) + for row in self.rows: + if len(row[i]) > neededlen: + neededlen = len(row[i]) + fmtstr += '{{{0}:>{1}}} |'.format(i, neededlen + 1) + i = i + 1 + fmtstr = fmtstr[:-2] + yield fmtstr.format(*self.headers) + for row in self.rows: + yield fmtstr.format(*row) + + def printerror(res, node=None): exitcode = 0 if 'errorcode' in res: