From cc6206db939f705ef4d80a64d50508f26413fecf Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 6 Nov 2022 10:52:24 +0100 Subject: [PATCH] Fix issue with EvTable crop. Resolve #2761 --- evennia/utils/evtable.py | 23 +++++-------------- evennia/utils/tests/test_evtable.py | 34 ++++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/evennia/utils/evtable.py b/evennia/utils/evtable.py index 6d16a30f83..f6837a79a0 100644 --- a/evennia/utils/evtable.py +++ b/evennia/utils/evtable.py @@ -471,20 +471,6 @@ class EvCell: else: self.height = self.raw_height - def _crop(self, text, width): - """ - Apply cropping of text. - - Args: - text (str): The text to crop. - width (int): The width to crop `text` to. - - """ - if d_len(text) > width: - crop_string = self.crop_string - return text[: width - d_len(crop_string)] + crop_string - return text - def _reformat(self): """ Apply all EvCells' formatting operations. @@ -541,11 +527,14 @@ class EvCell: # too many lines. Crop and mark last line with crop_string crop_string = self.crop_string adjusted_data = adjusted_data[:-excess] + adjusted_data_length = len(adjusted_data[-1]) crop_string_length = len(crop_string) - if len(adjusted_data[-1]) > crop_string_length: + if adjusted_data_length >= crop_string_length: + # replace with data[...] + # (note that if adjusted data is shorter than the crop-string, + # we skip the crop-string and just pass the cropped data.) adjusted_data[-1] = adjusted_data[-1][:-crop_string_length] + crop_string - else: - adjusted_data[-1] += crop_string + elif excess < 0: # too few lines. Fill to height. adjusted_data.extend(["" for _ in range(excess)]) diff --git a/evennia/utils/tests/test_evtable.py b/evennia/utils/tests/test_evtable.py index 6c383cb75a..0bd2c8c521 100644 --- a/evennia/utils/tests/test_evtable.py +++ b/evennia/utils/tests/test_evtable.py @@ -224,7 +224,7 @@ class TestEvTable(EvenniaTestCase): self._validate(expected, str(table)) - def test_2762(self): + def test_direct_evcolumn_adds(self): """ Testing https://github.com/evennia/evennia/issues/2762 @@ -276,3 +276,35 @@ class TestEvTable(EvenniaTestCase): """ self._validate(expected, str(table)) + + def test_width_enforcement(self): + """ + Testing https://github.com/evennia/evennia/issues/2761 + + EvTable enforces width kwarg, expanding the wrong column + + """ + # simple crop + table = evtable.EvTable(table=[["column"]], width=7, enforce_size=True) + expected = """ ++-----+ +| col | ++-----+ + """ + + self._validate(expected, str(table)) + + colA = evtable.EvColumn("it", "is", "a", "column", width=6, enforce_size=True) + colB = evtable.EvColumn("and", "another", "column", "here") + table = evtable.EvTable(table=[colA, colB], width=40) + + expected = """ ++----+---------------------------------+ +| it | and | +| is | another | +| a | column | +| co | here | ++----+---------------------------------+ + """ + + self._validate(expected, str(table))