From 3d35f6866330dfd3e4c02ae78a0b077dbf4d401e Mon Sep 17 00:00:00 2001 From: Griatch Date: Thu, 30 Jan 2014 23:47:13 +0100 Subject: [PATCH] Adding static enforcement of cell size (crop instead of expand vertically) --- src/utils/mudtable.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/src/utils/mudtable.py b/src/utils/mudtable.py index aa4e43dc92..4d052bca69 100644 --- a/src/utils/mudtable.py +++ b/src/utils/mudtable.py @@ -56,7 +56,7 @@ and then added the extra column and row, the result would be When adding new rows/columns their data can have its own alignments (left/center/right, top/center/bottom). -Contrary to prettytable, Evtable does not allow +Contrary to prettytable, Mudtable does not allow for importing from files. It is intended to be used with ANSIString for supporting @@ -89,6 +89,7 @@ class Cell(object): to this size. height - desired height of cell. it will pad to this size + pad_left - number of extra pad characters on the left pad_right - extra pad characters on the right pad_top - extra pad lines top (will pad with vpad_char) @@ -119,6 +120,11 @@ class Cell(object): corner_top_right corner_bottom_left corner_bottom_right + + enforce_size - if true, the width/height of the + cell is strictly enforced and + extra text will be cropped rather + than the cell growing vertically. """ self.pad_left = int(kwargs.get("pad_left", 1)) @@ -126,6 +132,8 @@ class Cell(object): self.pad_top = int( kwargs.get("pad_top", 0)) self.pad_bottom = int(kwargs.get("pad_bottom", 0)) + self.enforce_size = kwargs.get("enforce_size", False) + # avoid multi-char pad_chars messing up counting pad_char = kwargs.get("pad_char", " ") self.pad_char = pad_char[0] if pad_char else " " @@ -200,6 +208,18 @@ class Cell(object): adjusted_data.extend(wrap(line, width=width, drop_whitespace=False)) else: adjusted_data.append(line) + if self.enforce_size: + # don't allow too high cells + excess = len(adjusted_data) - self.height + if excess > 0: + # too many lines. Crop and mark last line with ... + adjusted_data = adjusted_data[:-excess] + if len(adjusted_data[-1]) > 3: + adjusted_data[-1] = adjusted_data[-1][:-2] + ".." + elif excess < 0: + # too few lines. Fill to height. + adjusted_data.extend(["" for i in range(excess)]) + return adjusted_data def _center(self, text, width, pad_char):