更新:该colprint
程序现已在applib Python库hosted in GitHub可用。
下面是对于那些你感兴趣的完整的程序:
# This function was written by Alex Martelli
# http://stackoverflow.com/questions/1396820/
def colprint(table, totwidth=None):
"""Print the table in terminal taking care of wrapping/alignment
- `table`: A table of strings. Elements must not be `None`
- `totwidth`: If None, console width is used
"""
if not table: return
if totwidth is None:
totwidth = find_console_width()
totwidth -= 1 # for not printing an extra empty line on windows
numcols = max(len(row) for row in table)
# ensure all rows have >= numcols columns, maybe empty
padded = [row+numcols*('',) for row in table]
# compute col widths, including separating space (except for last one)
widths = [ 1 + max(len(x) for x in column) for column in zip(*padded)]
widths[-1] -= 1
# drop or truncate columns from the right in order to fit
while sum(widths) > totwidth:
mustlose = sum(widths) - totwidth
if widths[-1] <= mustlose:
del widths[-1]
else:
widths[-1] -= mustlose
break
# and finally, the output phase!
for row in padded:
print(''.join([u'%*s' % (-w, i[:w])
for w, i in zip(widths, row)]))
def find_console_width():
if sys.platform.startswith('win'):
return _find_windows_console_width()
else:
return _find_unix_console_width()
def _find_unix_console_width():
"""Return the width of the Unix terminal
If `stdout` is not a real terminal, return the default value (80)
"""
import termios, fcntl, struct, sys
# fcntl.ioctl will fail if stdout is not a tty
if not sys.stdout.isatty():
return 80
s = struct.pack("HHHH", 0, 0, 0, 0)
fd_stdout = sys.stdout.fileno()
size = fcntl.ioctl(fd_stdout, termios.TIOCGWINSZ, s)
height, width = struct.unpack("HHHH", size)[:2]
return width
def _find_windows_console_width():
"""Return the width of the Windows console
If the width cannot be determined, return the default value (80)
"""
# http://code.activestate.com/recipes/440694/
from ctypes import windll, create_string_buffer
STDIN, STDOUT, STDERR = -10, -11, -12
h = windll.kernel32.GetStdHandle(STDERR)
csbi = create_string_buffer(22)
res = windll.kernel32.GetConsoleScreenBufferInfo(h, csbi)
if res:
import struct
(bufx, bufy, curx, cury, wattr,
left, top, right, bottom,
maxx, maxy) = struct.unpack("hhhhHhhhhhh", csbi.raw)
sizex = right - left + 1
sizey = bottom - top + 1
else:
sizex, sizey = 80, 25
return sizex
我想它使用诅咒/为什么ncurses的需要在所有的容易的ncurses – tonfa 2009-09-08 23:17:31
? apt,在Linux上,只需要查看'COLUMNS'环境变量来获取终端的宽度。但是,我正在寻找一种跨平台(* nix和Windows)解决方案。 – 2009-09-08 23:23:28
当我进入Linux(来自Terminal.App,但我认为不重要)时,我的环境中没有COLUMNS。不过,'aptitude search'显示正常。所以,我认为在你断言“你只需要查看COLUMNS环境变量”的说法中,你不可能是任何人,它不是一个可用的解决方案,也不是什么才能。 – 2009-09-09 02:21:40