add maxlen to table text stuff (not finalized)

This commit is contained in:
Mahmoud Hashemi 2014-03-01 15:24:19 -08:00
parent 8d022898d7
commit ffd6abd1d2
1 changed files with 14 additions and 9 deletions

View File

@ -60,7 +60,7 @@ Some idle thoughts:
""" """
def to_text(obj): def to_text(obj, maxlen=None):
try: try:
text = unicode(obj) text = unicode(obj)
except: except:
@ -68,11 +68,14 @@ def to_text(obj):
text = unicode(repr(obj)) text = unicode(repr(obj))
except: except:
text = unicode(object.__repr__(obj)) text = unicode(object.__repr__(obj))
if maxlen and len(text) > maxlen:
text = text[:maxlen - 3] + '...'
# TODO: inverse of ljust/rjust/center
return text return text
def escape_html(obj): def escape_html(obj, maxlen=None):
text = to_text(obj) text = to_text(obj, maxlen=maxlen)
return cgi.escape(text, quote=True) return cgi.escape(text, quote=True)
@ -392,23 +395,25 @@ class Table(object):
line_parts.extend([td, _tdtd.join(_fill_parts), _td_tr]) line_parts.extend([td, _tdtd.join(_fill_parts), _td_tr])
lines.append(''.join(line_parts)) lines.append(''.join(line_parts))
def to_text(self, with_headers=True): def to_text(self, with_headers=True, maxlen=None):
# TODO: verify this works for markdown # TODO: verify this works for markdown
lines = [] lines = []
widths = [] widths = []
headers = self.headers headers = self.headers
text_data = [[to_text(cell, maxlen=maxlen) for cell in row]
for row in self._data]
for idx in range(self._width): for idx in range(self._width):
cur_widths = [len(unicode(cur[idx])) for cur in self._data] cur_widths = [len(cur) for cur in text_data]
if with_headers: if with_headers:
cur_widths.append(len(headers[idx])) cur_widths.append(len(to_text(headers[idx], maxlen=maxlen)))
widths.append(max(cur_widths)) widths.append(max(cur_widths))
if with_headers: if with_headers:
lines.append(' | '.join([h.center(widths[i]) lines.append(' | '.join([h.center(widths[i])
for i, h in enumerate(headers)])) for i, h in enumerate(headers)]))
lines.append('-+-'.join(['-' * w for w in widths])) lines.append('-+-'.join(['-' * w for w in widths]))
for row in self._data: for row in text_data:
lines.append(' | '.join([unicode(col).center(widths[j]) lines.append(' | '.join([cell.center(widths[j])
for j, col in enumerate(row)])) for j, cell in enumerate(row)]))
return '\n'.join(lines) return '\n'.join(lines)