tools.py 文件源码

python
阅读 32 收藏 0 点赞 0 评论 0

项目:git-repo 作者: guyzmo 项目源码 文件源码
def columnize(lines, indent=0, pad=2):
    term_width = shutil.get_terminal_size((80, 20)).columns
    # prints a list of items in a fashion similar to the dir command
    # borrowed from https://gist.github.com/critiqjo/2ca84db26daaeb1715e1
    n_lines = len(lines)
    if n_lines == 0:
        return
    col_width = max(len(line) for line in lines)
    n_cols = int((term_width + pad - indent)/(col_width + pad))
    n_cols = min(n_lines, max(1, n_cols))
    col_len = int(n_lines/n_cols) + (0 if n_lines % n_cols == 0 else 1)
    if (n_cols - 1) * col_len >= n_lines:
        n_cols -= 1
    cols = [lines[i*col_len : i*col_len + col_len] for i in range(n_cols)]
    rows = list(zip(*cols))
    rows_missed = zip(*[col[len(rows):] for col in cols[:-1]])
    rows.extend(rows_missed)
    for row in rows:
        yield [" "*indent + (" "*pad).join(line.ljust(col_width) for line in row)]
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号