From 804d6ec37e0ef21ff494f3376206d58099de9628 2015-08-21 23:53:18 From: naught101 Date: 2015-08-21 23:53:18 Subject: [PATCH] Add spread option, to allow columns to fill space. --- diff --git a/IPython/utils/tests/test_text.py b/IPython/utils/tests/test_text.py index 303e23a..fd2c64f 100644 --- a/IPython/utils/tests/test_text.py +++ b/IPython/utils/tests/test_text.py @@ -50,6 +50,15 @@ def test_columnize(): out = text.columnize(items, row_first=True, displaywidth=10) nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n') + out = text.columnize(items, displaywidth=40, spread=True) + nt.assert_equal(out, 'aaaaa bbbbb ccccc ddddd\n') + out = text.columnize(items, displaywidth=20, spread=True) + nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n') + out = text.columnize(items, displaywidth=12, spread=True) + nt.assert_equal(out, 'aaaaa ccccc\nbbbbb ddddd\n') + out = text.columnize(items, displaywidth=10, spread=True) + nt.assert_equal(out, 'aaaaa\nbbbbb\nccccc\nddddd\n') + def test_columnize_random(): """Test with random input to hopfully catch edge case """ diff --git a/IPython/utils/text.py b/IPython/utils/text.py index 2cbaa20..724fe91 100644 --- a/IPython/utils/text.py +++ b/IPython/utils/text.py @@ -717,7 +717,7 @@ def compute_item_matrix(items, row_first=False, empty=None, *args, **kwargs) : return ([[_get_or_default(items, c * nrow + r, default=empty) for c in range(ncol)] for r in range(nrow)], info) -def columnize(items, row_first=False, separator=' ', displaywidth=80): +def columnize(items, row_first=False, separator=' ', displaywidth=80, spread=False): """ Transform a list of strings into a single string with columns. Parameters @@ -742,6 +742,8 @@ def columnize(items, row_first=False, separator=' ', displaywidth=80): if not items: return '\n' matrix, info = compute_item_matrix(items, row_first=row_first, separator_size=len(separator), displaywidth=displaywidth) + if spread: + separator = separator.ljust(int(info['optimal_separator_width'])) fmatrix = [filter(None, x) for x in matrix] sjoin = lambda x : separator.join([ y.ljust(w, ' ') for y, w in zip(x, info['column_widths'])]) return '\n'.join(map(sjoin, fmatrix))+'\n'