From 12c2402dac026f1c41754d9a1c6df58bff588cd8 Mon Sep 17 00:00:00 2001 From: Griatch Date: Mon, 5 May 2014 09:19:03 +0200 Subject: [PATCH] Changed crop functionality slightly, inspired by asechrest's unittest. --- src/tests/test_utils_utils.py | 4 +++- src/utils/utils.py | 5 +++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/tests/test_utils_utils.py b/src/tests/test_utils_utils.py index 86aaf7271..7c3b98bb8 100644 --- a/src/tests/test_utils_utils.py +++ b/src/tests/test_utils_utils.py @@ -18,7 +18,9 @@ class TestCrop(unittest.TestCase): self.assertEqual("0123[...]", utils.crop("0123456789", width=9, suffix="[...]")) # Input length less than desired width, no crop self.assertEqual("0123", utils.crop("0123", width=9, suffix="[...]")) - self.assertEqual("[..", utils.crop("0123", width=3, suffix="[...]")) + # Width too small or equal to width of suffix + self.assertEqual("012", utils.crop("0123", width=3, suffix="[...]")) + self.assertEqual("01234", utils.crop("0123456", width=5, suffix="[...]")) class TestDedent(unittest.TestCase): def test_dedent(self): diff --git a/src/utils/utils.py b/src/utils/utils.py index 10cae0a87..2cd86cfc9 100644 --- a/src/utils/utils.py +++ b/src/utils/utils.py @@ -72,14 +72,15 @@ def crop(text, width=78, suffix="[...]"): """ Crop text to a certain width, adding suffix to show the line continues. Cropping will be done so that the suffix will also fit - within the given width. + within the given width. If width is too small to fit both crop + and suffix, crop without the suffix. """ ltext = len(to_str(text)) if ltext <= width: return text else: lsuffix = len(suffix) - return "%s%s" % (text[:width - lsuffix], suffix) + return text[:width] if lsuffix >= width else "%s%s" % (text[:width - lsuffix], suffix) def dedent(text):