##// END OF EJS Templates
Add spread option, to allow columns to fill space.
naught101 -
Show More
@@ -50,6 +50,15 b' def test_columnize():'
50 50 out = text.columnize(items, row_first=True, displaywidth=10)
51 51 nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n')
52 52
53 out = text.columnize(items, displaywidth=40, spread=True)
54 nt.assert_equal(out, 'aaaaa bbbbb ccccc ddddd\n')
55 out = text.columnize(items, displaywidth=20, spread=True)
56 nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n')
57 out = text.columnize(items, displaywidth=12, spread=True)
58 nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n')
59 out = text.columnize(items, displaywidth=10, spread=True)
60 nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n')
61
53 62
54 63 def test_columnize_random():
55 64 """Test with random input to hopfully catch edge case """
@@ -717,7 +717,7 b' def compute_item_matrix(items, row_first=False, empty=None, *args, **kwargs) :'
717 717 return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info)
718 718
719 719
720 def columnize(items, row_first=False, separator=' ', displaywidth=80):
720 def columnize(items, row_first=False, separator=' ', displaywidth=80, spread=False):
721 721 """ Transform a list of strings into a single string with columns.
722 722
723 723 Parameters
@@ -742,6 +742,8 b" def columnize(items, row_first=False, separator=' ', displaywidth=80):"
742 742 if not items:
743 743 return '\n'
744 744 matrix, info = compute_item_matrix(items, row_first=row_first, separator_size=len(separator), displaywidth=displaywidth)
745 if spread:
746 separator = separator.ljust(int(info['optimal_separator_width']))
745 747 fmatrix = [filter(None, x) for x in matrix]
746 748 sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column_widths'])])
747 749 return '\n'.join(map(sjoin, fmatrix))+'\n'
General Comments 0
You need to be logged in to leave comments. Login now