<s> from __future__ import division , print_function <EOL> import os <EOL> from math import ceil <EOL> try : <EOL> from itertools import zip_longest <EOL> except ImportError : <EOL> from itertools import izip_longest as zip_longest <EOL> try : <EOL> from shutil import get_terminal_size <EOL> except ImportError : <EOL> from backports . shutil_get_terminal_size import get_terminal_size <EOL> SEP = '<STR_LIT:U+0020>' <EOL> L = len ( SEP ) <EOL> def get_rows ( venvs , columns_number ) : <EOL> lines_number = int ( ceil ( len ( venvs ) / columns_number ) ) <EOL> for i in range ( lines_number ) : <EOL> yield venvs [ i : : lines_number ] <EOL> def row_len ( names ) : <EOL> return sum ( map ( len , names ) ) + L * len ( names ) - L <EOL> def get_best_columns_number ( venvs ) : <EOL> max_width , _ = get_terminal_size ( ) <EOL> columns_number = <NUM_LIT:1> <EOL> for columns_number in range ( <NUM_LIT:1> , len ( venvs ) + <NUM_LIT:1> ) : <EOL> rows = get_rows ( venvs , columns_number ) <EOL> if max ( map ( row_len , rows ) ) > max_width : <EOL> return ( columns_number - <NUM_LIT:1> ) or <NUM_LIT:1> <EOL> else : <EOL> return columns_number <EOL> def align_column ( column ) : <EOL> m = max ( map ( len , column ) ) <EOL> return [ name . ljust ( m ) for name in column ] <EOL> def columnize ( venvs ) : <EOL> columns_n = get_best_columns_number ( venvs ) <EOL> columns = map ( align_column , zip_longest ( * get_rows ( venvs , columns_n ) , fillvalue = '<STR_LIT>' ) ) <EOL> return map ( SEP . join , zip ( * columns ) ) <EOL> def print_virtualenvs ( * venvs ) : <EOL> venvs = sorted ( venvs ) <EOL> if os . isatty ( <NUM_LIT:1> ) : <EOL> print ( * columnize ( venvs ) , sep = '<STR_LIT:\n>' ) <EOL> else : <EOL> print ( * venvs , sep = '<STR_LIT:U+0020>' ) </s>
