implement L<maxlen>/<replacement>/ format option

The L option allows for the contents of a format field to be replaced
with <replacement> if its length is greater than <maxlen>.

Example:
{f:L5/too long/} -> "foo"      (if "f" is "foo")
                 -> "too long" (if "f" is "foobar")

(#92) (#94)
This commit is contained in:
Mike Fährmann
2018-07-29 13:52:07 +02:00
parent 5f27cfeff6
commit e0dd8dff5f
2 changed files with 19 additions and 0 deletions

View File

@@ -210,6 +210,14 @@ class TestFormatter(unittest.TestCase):
self._run_test("{a[:50]}", v[:50])
self._run_test("{a[:]}" , v)
def test_maxlen(self):
v = self.kwdict["a"]
self._run_test("{a:L5/foo/}" , "foo")
self._run_test("{a:L50/foo/}", v)
self._run_test("{a:L50/foo/>50}", " " * 39 + v)
self._run_test("{a:L50/foo/>51}", "foo")
self._run_test("{a:Lab/foo/}", "foo")
def _run_test(self, format_string, result, default=None):
formatter = util.Formatter(default)
output = formatter.vformat(format_string, self.kwdict)