diff --git a/IPython/utils/tests/test_text.py b/IPython/utils/tests/test_text.py index d5f3a3c..0fa23cb 100644 --- a/IPython/utils/tests/test_text.py +++ b/IPython/utils/tests/test_text.py @@ -26,5 +26,19 @@ from IPython.utils import text #----------------------------------------------------------------------------- def test_columnize(): - """Test columnize with very long inputs""" - text.columnize(['a'*180, 'b'*180]) + """Basic columnize tests.""" + size = 5 + items = [l*size for l in 'abc'] + out = text.columnize(items, displaywidth=80) + nt.assert_equals(out, 'aaaaa bbbbb ccccc\n') + out = text.columnize(items, displaywidth=10) + nt.assert_equals(out, 'aaaaa ccccc\nbbbbb\n') + + +def test_columnize_long(): + """Test columnize with inputs longer than the display window""" + text.columnize(['a'*81, 'b'*81], displaywidth=80) + size = 11 + items = [l*size for l in 'abc'] + out = text.columnize(items, displaywidth=size-1) + nt.assert_equals(out, '\n'.join(items+[''])) diff --git a/IPython/utils/text.py b/IPython/utils/text.py index a1f7f28..a23905f 100644 --- a/IPython/utils/text.py +++ b/IPython/utils/text.py @@ -639,6 +639,13 @@ def columnize(items, separator=' ', displaywidth=80): elif size == 1: return '%s\n' % items[0] + # Special case: if any item is longer than the maximum width, there's no + # point in triggering the logic below... + item_len = map(len, items) # save these, we can reuse them below + longest = max(item_len) + if longest >= displaywidth: + return '\n'.join(items+['']) + # Try every row count from 1 upwards array_index = lambda nrows, row, col: nrows*col + row for nrows in range(1, size): @@ -651,8 +658,8 @@ def columnize(items, separator=' ', displaywidth=80): for row in range(nrows): i = array_index(nrows, row, col) if i >= size: break - x = items[i] - colwidth = max(colwidth, len(x)) + x, len_x = items[i], item_len[i] + colwidth = max(colwidth, len_x) colwidths.append(colwidth) totwidth += colwidth + len(separator) if totwidth > displaywidth: