##// 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 out = text.columnize(items, row_first=True, displaywidth=10)
50 out = text.columnize(items, row_first=True, displaywidth=10)
51 nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n')
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 def test_columnize_random():
63 def test_columnize_random():
55 """Test with random input to hopfully catch edge case """
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 return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info)
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 """ Transform a list of strings into a single string with columns.
721 """ Transform a list of strings into a single string with columns.
722
722
723 Parameters
723 Parameters
@@ -742,6 +742,8 b" def columnize(items, row_first=False, separator=' ', displaywidth=80):"
742 if not items:
742 if not items:
743 return '\n'
743 return '\n'
744 matrix, info = compute_item_matrix(items, row_first=row_first, separator_size=len(separator), displaywidth=displaywidth)
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 fmatrix = [filter(None, x) for x in matrix]
747 fmatrix = [filter(None, x) for x in matrix]
746 sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column_widths'])])
748 sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column_widths'])])
747 return '\n'.join(map(sjoin, fmatrix))+'\n'
749 return '\n'.join(map(sjoin, fmatrix))+'\n'
General Comments 0
You need to be logged in to leave comments. Login now