diff --git a/py/_io/terminalwriter.py b/py/_io/terminalwriter.py index 817bf2d8..be559867 100644 --- a/py/_io/terminalwriter.py +++ b/py/_io/terminalwriter.py @@ -227,7 +227,7 @@ def sep(self, sepchar, title=None, fullwidth=None, **kw): # i.e. 2 + 2*len(sepchar)*N + len(title) <= fullwidth # 2*len(sepchar)*N <= fullwidth - len(title) - 2 # N <= (fullwidth - len(title) - 2) // (2*len(sepchar)) - N = (fullwidth - len(title) - 2) // (2*len(sepchar)) + N = max((fullwidth - len(title) - 2) // (2*len(sepchar)), 1) fill = sepchar * N line = "%s %s %s" % (fill, title, fill) else: diff --git a/testing/io_/test_terminalwriter.py b/testing/io_/test_terminalwriter.py index 64b07568..1eef7f7d 100644 --- a/testing/io_/test_terminalwriter.py +++ b/testing/io_/test_terminalwriter.py @@ -165,6 +165,12 @@ def test_sep_with_title(self, tw): assert len(l) == 1 assert l[0] == "-" * 26 + " hello " + "-" * (27-win32) + "\n" + def test_sep_longer_than_width(self, tw): + tw.sep('-', 'a' * 10, fullwidth=5) + line, = tw.getlines() + # even though the string is wider than the line, still have a separator + assert line == '- aaaaaaaaaa -\n' + @py.test.mark.skipif("sys.platform == 'win32'") def test__escaped(self, tw): text2 = tw._escaped("hello", (31))