Add elide()

This commit is contained in:
Florian Bruhin 2014-05-16 07:46:56 +02:00
parent 9b2b7f99dd
commit 70214bfedf
2 changed files with 30 additions and 0 deletions

View File

@ -34,6 +34,26 @@ import qutebrowser.utils.misc as utils
from qutebrowser.test.helpers import environ_set_temp
class ElidingTests(TestCase):
"""Test elide."""
ELLIPSIS = '\u2026'
def test_too_small(self):
with self.assertRaises(ValueError):
utils.elide('foo', 0)
def test_length_one(self):
self.assertEqual(utils.elide('foo', 1), self.ELLIPSIS)
def test_fits(self):
self.assertEqual(utils.elide('foo', 3), 'foo')
def test_elided(self):
self.assertEqual(utils.elide('foobar', 3), 'fo' + self.ELLIPSIS)
class CheckOverflowTests(TestCase):
"""Test check_overflow."""

View File

@ -47,6 +47,16 @@ MINVALS = {
}
def elide(text, length):
"""Elide text so it uses a maximum of length chars."""
if length < 1:
raise ValueError("length must be >= 1!")
if len(text) <= length:
return text
else:
return text[:length-1] + '\u2026'
def check_overflow(arg, ctype, fatal=True):
"""Check if the given argument is in bounds for the given type.