Module: check_mk
Branch: master
Commit: e40cd5b84d7bc684ad76dde0ac1dc92b077a065f
URL:
http://git.mathias-kettner.de/git/?p=check_mk.git;a=commit;h=e40cd5b84d7bc6…
Author: Lars Michelsen <lm(a)mathias-kettner.de>
Date: Fri Jul 27 22:11:54 2018 +0200
Moved code to helper functions
Change-Id: Ie1f23d11120592925c432e10d276f5e6e839e914
---
cmk/tty.py | 36 ++++++++++++++++++++----------------
1 file changed, 20 insertions(+), 16 deletions(-)
diff --git a/cmk/tty.py b/cmk/tty.py
index df0fa97..d054122 100644
--- a/cmk/tty.py
+++ b/cmk/tty.py
@@ -106,32 +106,36 @@ def get_size():
return (24, 80)
-# TODO: Is this a good place?
-# TODO: Cleanup this function!
-def print_table(headers, colors, rows, indent = ""):
+def print_table(headers, colors, rows, indent=""):
num_columns = len(headers)
+ lengths = _column_lengths(headers, rows, num_columns)
+ fmt = _row_template(lengths, colors, indent)
- def make_utf8(x):
- if type(x) == unicode:
- return x.encode('utf-8')
- else:
- return "%s" % (x,)
+ for index, row in enumerate([ headers ] + rows):
+ sys.stdout.write(fmt % tuple(_make_utf8(c) for c in row[:num_columns]))
+ if index == 0:
+ sys.stdout.write(fmt % tuple("-" * l for l in lengths))
+
+def _column_lengths(headers, rows, num_columns):
lengths = [ len(h) for h in headers ]
for row in rows:
for index, column in enumerate(row[:num_columns]):
- lengths[index] = max(len(make_utf8(column)), lengths[index])
+ lengths[index] = max(len(_make_utf8(column)), lengths[index])
+ return lengths
+
+def _row_template(lengths, colors, indent):
fmt = indent
sep = ""
for l,c in zip(lengths, colors):
fmt += c + sep + "%-" + str(l) + "s" + normal
sep = " "
-
- first = True
fmt += "\n"
- for row in [ headers ] + rows:
- sys.stdout.write(fmt % tuple(make_utf8(c) for c in row[:num_columns]))
- if first:
- first = False
- sys.stdout.write(fmt % tuple("-" * l for l in lengths))
+ return fmt
+
+
+def _make_utf8(x):
+ if type(x) == unicode:
+ return x.encode('utf-8')
+ return str(x)