Changed crop functionality slightly, inspired by asechrest's unittest.
This commit is contained in:
parent
bb854384bd
commit
12c2402dac
2 changed files with 6 additions and 3 deletions
|
|
@ -18,7 +18,9 @@ class TestCrop(unittest.TestCase):
|
||||||
self.assertEqual("0123[...]", utils.crop("0123456789", width=9, suffix="[...]"))
|
self.assertEqual("0123[...]", utils.crop("0123456789", width=9, suffix="[...]"))
|
||||||
# Input length less than desired width, no crop
|
# Input length less than desired width, no crop
|
||||||
self.assertEqual("0123", utils.crop("0123", width=9, suffix="[...]"))
|
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):
|
class TestDedent(unittest.TestCase):
|
||||||
def test_dedent(self):
|
def test_dedent(self):
|
||||||
|
|
|
||||||
|
|
@ -72,14 +72,15 @@ def crop(text, width=78, suffix="[...]"):
|
||||||
"""
|
"""
|
||||||
Crop text to a certain width, adding suffix to show the line
|
Crop text to a certain width, adding suffix to show the line
|
||||||
continues. Cropping will be done so that the suffix will also fit
|
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))
|
ltext = len(to_str(text))
|
||||||
if ltext <= width:
|
if ltext <= width:
|
||||||
return text
|
return text
|
||||||
else:
|
else:
|
||||||
lsuffix = len(suffix)
|
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):
|
def dedent(text):
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue