Show More
@@ -26,5 +26,19 b' from IPython.utils import text' | |||||
26 | #----------------------------------------------------------------------------- |
|
26 | #----------------------------------------------------------------------------- | |
27 |
|
27 | |||
28 | def test_columnize(): |
|
28 | def test_columnize(): | |
29 |
""" |
|
29 | """Basic columnize tests.""" | |
30 | text.columnize(['a'*180, 'b'*180]) |
|
30 | size = 5 | |
|
31 | items = [l*size for l in 'abc'] | |||
|
32 | out = text.columnize(items, displaywidth=80) | |||
|
33 | nt.assert_equals(out, 'aaaaa bbbbb ccccc\n') | |||
|
34 | out = text.columnize(items, displaywidth=10) | |||
|
35 | nt.assert_equals(out, 'aaaaa ccccc\nbbbbb\n') | |||
|
36 | ||||
|
37 | ||||
|
38 | def test_columnize_long(): | |||
|
39 | """Test columnize with inputs longer than the display window""" | |||
|
40 | text.columnize(['a'*81, 'b'*81], displaywidth=80) | |||
|
41 | size = 11 | |||
|
42 | items = [l*size for l in 'abc'] | |||
|
43 | out = text.columnize(items, displaywidth=size-1) | |||
|
44 | nt.assert_equals(out, '\n'.join(items+[''])) |
@@ -639,6 +639,13 b" def columnize(items, separator=' ', displaywidth=80):" | |||||
639 | elif size == 1: |
|
639 | elif size == 1: | |
640 | return '%s\n' % items[0] |
|
640 | return '%s\n' % items[0] | |
641 |
|
641 | |||
|
642 | # Special case: if any item is longer than the maximum width, there's no | |||
|
643 | # point in triggering the logic below... | |||
|
644 | item_len = map(len, items) # save these, we can reuse them below | |||
|
645 | longest = max(item_len) | |||
|
646 | if longest >= displaywidth: | |||
|
647 | return '\n'.join(items+['']) | |||
|
648 | ||||
642 | # Try every row count from 1 upwards |
|
649 | # Try every row count from 1 upwards | |
643 | array_index = lambda nrows, row, col: nrows*col + row |
|
650 | array_index = lambda nrows, row, col: nrows*col + row | |
644 | for nrows in range(1, size): |
|
651 | for nrows in range(1, size): | |
@@ -651,8 +658,8 b" def columnize(items, separator=' ', displaywidth=80):" | |||||
651 | for row in range(nrows): |
|
658 | for row in range(nrows): | |
652 | i = array_index(nrows, row, col) |
|
659 | i = array_index(nrows, row, col) | |
653 | if i >= size: break |
|
660 | if i >= size: break | |
654 | x = items[i] |
|
661 | x, len_x = items[i], item_len[i] | |
655 |
colwidth = max(colwidth, len |
|
662 | colwidth = max(colwidth, len_x) | |
656 | colwidths.append(colwidth) |
|
663 | colwidths.append(colwidth) | |
657 | totwidth += colwidth + len(separator) |
|
664 | totwidth += colwidth + len(separator) | |
658 | if totwidth > displaywidth: |
|
665 | if totwidth > displaywidth: |
General Comments 0
You need to be logged in to leave comments.
Login now