def columnate(l, prefix): """Format elements of 'l' in columns with 'prefix' leading each line. The number of columns is determined automatically based on the string lengths. """ binary = isinstance(prefix, bytes) nothing = b'' if binary else '' nl = b'\n' if binary else '\n' if not l: return nothing l = l[:] clen = max(len(s) for s in l) ncols = (tty_width() - len(prefix)) // (clen + 2) if ncols <= 1: ncols = 1 clen = 0 cols = [] while len(l) % ncols: l.append(nothing) rows = len(l) // ncols for s in range(0, len(l), rows): cols.append(l[s:s + rows]) out = nothing fmt = b'%-*s' if binary else '%-*s' for row in zip(*cols): out += prefix + nothing.join((fmt % (clen + 2, s)) for s in row) + nl return out
def columnate(l, prefix): """Format elements of 'l' in columns with 'prefix' leading each line. The number of columns is determined automatically based on the string lengths. """ if not l: return "" l = l[:] clen = max(len(s) for s in l) ncols = (tty_width() - len(prefix)) // (clen + 2) if ncols <= 1: ncols = 1 clen = 0 cols = [] while len(l) % ncols: l.append('') rows = len(l) // ncols for s in compat.range(0, len(l), rows): cols.append(l[s:s + rows]) out = '' for row in zip(*cols): out += prefix + ''.join(('%-*s' % (clen + 2, s)) for s in row) + '\n' return out
def columnate(l, prefix): """Format elements of 'l' in columns with 'prefix' leading each line. The number of columns is determined automatically based on the string lengths. """ if not l: return "" l = l[:] clen = max(len(s) for s in l) ncols = (tty_width() - len(prefix)) // (clen + 2) if ncols <= 1: ncols = 1 clen = 0 cols = [] while len(l) % ncols: l.append('') rows = len(l) // ncols for s in compat.range(0, len(l), rows): cols.append(l[s:s+rows]) out = '' for row in zip(*cols): out += prefix + ''.join(('%-*s' % (clen+2, s)) for s in row) + '\n' return out